Skip to content

Commit

Permalink
fix: #6
Browse files Browse the repository at this point in the history
  • Loading branch information
nukc committed Mar 2, 2017
1 parent 7c7a183 commit 8ed36fb
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 26 deletions.
3 changes: 1 addition & 2 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ it will be added to the layout.
add the dependency to your build.gradle:

```groovy
compile 'com.github.nukc.stateview:library:1.0.1'
compile 'com.github.nukc.stateview:library:1.1.0'
```

##Usage
Expand All @@ -34,7 +34,6 @@ Can be directly used in java.
```

```java
//can be use in onCreateView of Fragment
mStateView = StateView.inject(View view);

mStateView = StateView.inject(View view, boolean hasActionBar);
Expand Down
6 changes: 4 additions & 2 deletions 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.0.1'
compile 'com.github.nukc.stateview:library:1.1.0'
```

## 使用方法
Expand All @@ -33,7 +33,6 @@ StateView 一个轻量级的控件, 继承自 `View`, 吸收了 `ViewStub` 的
```

```java
//可用于在Fragment的onCreateView中
mStateView = StateView.inject(View view);

mStateView = StateView.inject(View view, boolean hasActionBar);
Expand Down Expand Up @@ -97,6 +96,9 @@ setLoadingResource(@LayoutRes int loadingResource)

## ChangeLog

#### Version 1.1.0
fix [issues #6](https://github.com/nukc/StateView/issues/6)

#### Version 1.0.0
删除上版本 Deprecated 的方法;
修改 inject(ViewGroup parent) 方法
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/res/layout/fragment_inject.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.github.nukc.sample.InjectFragment">

<android.support.v7.widget.Toolbar
Expand All @@ -11,8 +12,10 @@

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/app_name" />

</RelativeLayout>
</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.0.1"
version = "1.1.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 @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 14
versionName "1.0.1"
versionCode 15
versionName "1.1.0"
}
buildTypes {
release {
Expand Down
85 changes: 70 additions & 15 deletions library/src/main/java/com/github/nukc/stateview/StateView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -82,27 +84,80 @@ public static StateView inject(@NonNull ViewGroup parent) {
public static StateView inject(@NonNull ViewGroup parent, boolean hasActionBar) {
// 因为 LinearLayout/ScrollView/AdapterView 的特性
// 为了 StateView 能正常显示,自动再套一层(开发的时候就不用额外的工作量了)
int screenHeight = 0;
if (parent instanceof LinearLayout ||
parent instanceof ScrollView ||
parent instanceof AdapterView) {
FrameLayout root = new FrameLayout(parent.getContext());
root.setLayoutParams(parent.getLayoutParams());
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
parent.setLayoutParams(layoutParams);
ViewParent viewParent = parent.getParent();
if (viewParent instanceof ViewGroup) {
ViewGroup rootGroup = (ViewGroup) viewParent;
// 把 parent 从它自己的父容器中移除
rootGroup.removeView(parent);
// 然后替换成新的
rootGroup.addView(root);
if (viewParent == null) {
// create a new FrameLayout to wrap StateView and parent's childView
FrameLayout wrapper = new FrameLayout(parent.getContext());
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wrapper.setLayoutParams(layoutParams);

if (parent instanceof LinearLayout) {
// create a new LinearLayout to wrap parent's childView
LinearLayout wrapLayout = new LinearLayout(parent.getContext());
wrapLayout.setLayoutParams(parent.getLayoutParams());
wrapLayout.setOrientation(((LinearLayout) parent).getOrientation());

for (int i = 0, childCount = parent.getChildCount(); i < childCount; i++) {
View childView = parent.getChildAt(0);
parent.removeView(childView);
wrapLayout.addView(childView);
}
wrapper.addView(wrapLayout);
} else if (parent instanceof ScrollView) {
// not recommended to inject scrollview
if (parent.getChildCount() != 1) {
throw new IllegalStateException("the scrollView does not have one direct child");
}
View directView = parent.getChildAt(0);
parent.removeView(directView);
wrapper.addView(directView);

WindowManager wm = (WindowManager) parent.getContext()
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
} else {
throw new IllegalStateException("the view does not have parent, view = "
+ parent.toString());
}
// parent add wrapper
parent.addView(wrapper);
// StateView will be added to wrapper
parent = wrapper;
} else {
FrameLayout root = new FrameLayout(parent.getContext());
root.setLayoutParams(parent.getLayoutParams());
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
parent.setLayoutParams(layoutParams);

if (viewParent instanceof ViewGroup) {
ViewGroup rootGroup = (ViewGroup) viewParent;
// 把 parent 从它自己的父容器中移除
rootGroup.removeView(parent);
// 然后替换成新的
rootGroup.addView(root);
}
root.addView(parent);
parent = root;
}
root.addView(parent);
parent = root;
}
StateView stateView = new StateView(parent.getContext());
parent.addView(stateView);
if (screenHeight > 0) {
// let StateView be shown in the center
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
hasActionBar ? screenHeight - stateView.getActionBarHeight() : screenHeight);
parent.addView(stateView, params);
} else {
parent.addView(stateView);
}
if (hasActionBar) {
stateView.setTopMargin();
}
Expand Down Expand Up @@ -318,7 +373,7 @@ public void setTopMargin() {
/**
* @return actionBarSize
*/
private int getActionBarHeight() {
public int getActionBarHeight() {
int height = 0;
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
Expand Down

0 comments on commit 8ed36fb

Please sign in to comment.