Skip to content

Commit

Permalink
feat: support wrap view
Browse files Browse the repository at this point in the history
  • Loading branch information
nukc committed Apr 20, 2018
1 parent f4bcd90 commit 702ea83
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ StateView is an invisible, zero-sized View that can be used to lazily inflate lo
add the dependency to your build.gradle:

```groovy
compile 'com.github.nukc.stateview:library:1.3.4'
compile 'com.github.nukc.stateview:library:1.5.0'
// animator providers
compile 'com.github.nukc.stateview:animations:1.0.1'
Expand All @@ -42,6 +42,9 @@ Can be directly used in java.
mStateView = StateView.inject(View view, boolean hasActionBar);
```

```java
mStateView = StateView.wrap(View view);
```

Or include the StateView widget in your layout.

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ StateView 一个轻量级的控件, 继承自 `View`, 吸收了 `ViewStub` 的


```groovy
compile 'com.github.nukc.stateview:library:1.3.4'
compile 'com.github.nukc.stateview:library:1.5.0'
// animator providers
compile 'com.github.nukc.stateview:animations:1.0.1'
Expand Down Expand Up @@ -42,6 +42,11 @@ StateView 一个轻量级的控件, 继承自 `View`, 吸收了 `ViewStub` 的
mStateView = StateView.inject(View view, boolean hasActionBar);
```

- 包裹指定的 View,这个会增加层次
```java
mStateView = StateView.wrap(View view);
```

或添加到布局:

```xml
Expand Down
2 changes: 1 addition & 1 deletion animations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:appcompat-v7:27.1.0'
testCompile 'junit:junit:4.12'
compile project(':library')
}
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
}

ext {
supportLibVersion = '27.0.2'
supportLibVersion = '27.1.0'
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
android:name=".DrawerLayoutActivity"
android:label="@string/title_activity_drawer_layout"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity android:name=".WrapActivity" />
</application>

</manifest>
7 changes: 7 additions & 0 deletions app/src/main/java/com/github/nukc/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,12 @@ public void onClick(View view) {
startActivity(new Intent(MainActivity.this, DrawerLayoutActivity.class));
}
});

findViewById(R.id.btn_wrap_view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, WrapActivity.class));
}
});
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/com/github/nukc/sample/WrapActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.nukc.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.github.nukc.stateview.StateView;
import com.github.nukc.stateview.animations.FadeScaleAnimatorProvider;
import com.github.nukc.stateview.animations.SlideAnimatorProvider;

public class WrapActivity extends AppCompatActivity {

private static final String TAG = WrapActivity.class.getSimpleName();

private StateView mStateView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inject);

mStateView = StateView.wrap(findViewById(R.id.text_view));
mStateView.setAnimatorProvider(new FadeScaleAnimatorProvider());
mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
@Override
public void onRetryClick() {
//do something
mStateView.showRetry();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_inject, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.show_empty:
mStateView.showEmpty();
break;
case R.id.show_retry:
mStateView.showRetry();
break;
case R.id.show_loading:
mStateView.showLoading();
break;
case R.id.show_content:
mStateView.showContent();
break;
}
return super.onOptionsItemSelected(item);
}
}
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_inject.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
tools:context="com.github.nukc.sample.InjectActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:text="Content"/>

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Inject DrawerLayout"/>

<Button
android:id="@+id/btn_wrap_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wrap View"/>
</LinearLayout>
2 changes: 1 addition & 1 deletion bintray.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def siteUrl = 'https://github.com/nukc/StateView' // 项目的主页
def gitUrl = 'https://github.com/nukc/StateView.git' // Git仓库的url

group = "com.github.nukc.stateview"
version = "1.3.4"
version = "1.5.0"

install {
repositories.mavenInstaller {
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 27
versionCode 21
versionName "1.3.3"
versionName "1.5.0"
}
buildTypes {
release {
Expand All @@ -24,7 +24,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:appcompat-v7:27.1.0'
}

apply from: '../bintray.gradle'
16 changes: 16 additions & 0 deletions library/src/main/java/com/github/nukc/stateview/StateView.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ public static StateView inject(@NonNull View view, boolean hasActionBar) {
}
}

/**
* 包裹 view
* @param view target view
* @return StateView
*/
public static StateView wrap(@NonNull View view) {
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
FrameLayout wrap = new FrameLayout(view.getContext());
wrap.addView(view);
StateView stateView = new StateView(view.getContext());
wrap.addView(stateView, view.getLayoutParams());
parent.addView(wrap);
return stateView;
}

public StateView(Context context) {
this(context, null);
}
Expand Down

0 comments on commit 702ea83

Please sign in to comment.