Skip to content

Commit

Permalink
修复部分情况下更新状态无效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
brook007 committed Apr 27, 2020
1 parent 03ce132 commit fff388f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ allprojects {
然后在需要使用的模块的build.gradle中添加以下代码
```groovy
dependencies {
implementation 'com.github.Brook007:StateLayout:1.1.1'
implementation 'com.github.Brook007:StateLayout:1.1.2'
}
```

Expand Down
111 changes: 57 additions & 54 deletions library/src/main/java/com/brook/app/android/view/StateLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import android.view.ViewGroup;
import android.widget.FrameLayout;


import com.brook.app.android.statelayout.R;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -68,8 +67,9 @@ public class StateLayout extends FrameLayout {
// 无网络
public static final int NO_NETWORK = 4;

private static final int CHANGE_STATE = 0x100;

@IntDef({EMPTY, LOADING, SUCCESS, ERROR, NO_NETWORK})
@IntDef({ EMPTY, LOADING, SUCCESS, ERROR, NO_NETWORK })
@Retention(RetentionPolicy.SOURCE)
public @interface LoadState {

Expand Down Expand Up @@ -121,48 +121,41 @@ public StateLayout(@NonNull Context context, @Nullable AttributeSet attrs, @Attr
mHandler = new Handler(Looper.getMainLooper());
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (isFirst) {
setState(mState, false);
isFirst = false;
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
ViewGroup.LayoutParams params = child.getLayoutParams();
if (params instanceof LayoutParams) {
LayoutParams layoutParams = (LayoutParams) params;
child.setVisibility(View.GONE);
switch (layoutParams.mStateFlag) {
case EMPTY:
mEmptyView = child;
break;
case LOADING:
mOnLoadingView = child;
break;
case SUCCESS:
mSuccessView = child;
break;
case ERROR:
mErrorView = child;
break;
case NO_NETWORK:
mNoNetworkView = child;
break;
if (isFirst) {
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
ViewGroup.LayoutParams params = child.getLayoutParams();
if (params instanceof LayoutParams) {
LayoutParams layoutParams = (LayoutParams) params;
child.setVisibility(View.GONE);
switch (layoutParams.mStateFlag) {
case EMPTY:
mEmptyView = child;
break;
case LOADING:
mOnLoadingView = child;
break;
case SUCCESS:
mSuccessView = child;
break;
case ERROR:
mErrorView = child;
break;
case NO_NETWORK:
mNoNetworkView = child;
break;
}
}
}
isFirst = false;
setState(mState, false);
}
setState(mState, false);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


/**
* 设置无网络时的视图
*
Expand Down Expand Up @@ -191,7 +184,8 @@ public void setNoNetworkView(View view) {
if (layoutParams != null && layoutParams instanceof LayoutParams) {
((LayoutParams) layoutParams).mStateFlag = NO_NETWORK;
} else {
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
((LayoutParams) layoutParams).mStateFlag = NO_NETWORK;
}
view.setLayoutParams(layoutParams);
Expand Down Expand Up @@ -223,7 +217,8 @@ public void setEmptyView(View view) {
if (layoutParams != null && layoutParams instanceof LayoutParams) {
((LayoutParams) layoutParams).mStateFlag = EMPTY;
} else {
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
((LayoutParams) layoutParams).mStateFlag = EMPTY;
}
view.setLayoutParams(layoutParams);
Expand All @@ -241,7 +236,6 @@ public void setErrorViewById(@IdRes int resId) {
setErrorView(findViewById(resId));
}


/**
* 设置加载错误时的视图
*
Expand All @@ -251,7 +245,6 @@ public void setErrorView(@LayoutRes int resId) {
setErrorView(LayoutInflater.from(getContext()).inflate(resId, this, false));
}


/**
* 设置错误视图
*
Expand All @@ -262,7 +255,8 @@ public void setErrorView(View view) {
if (layoutParams != null && layoutParams instanceof LayoutParams) {
((LayoutParams) layoutParams).mStateFlag = ERROR;
} else {
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
((LayoutParams) layoutParams).mStateFlag = ERROR;
}
view.setLayoutParams(layoutParams);
Expand Down Expand Up @@ -300,7 +294,8 @@ public void setOnLoadingView(View view) {
if (layoutParams != null && layoutParams instanceof LayoutParams) {
((LayoutParams) layoutParams).mStateFlag = LOADING;
} else {
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
((LayoutParams) layoutParams).mStateFlag = LOADING;
}
view.setLayoutParams(layoutParams);
Expand Down Expand Up @@ -337,7 +332,8 @@ public void setSuccessView(View view) {
if (layoutParams != null && layoutParams instanceof LayoutParams) {
((LayoutParams) layoutParams).mStateFlag = SUCCESS;
} else {
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams = new StateLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
((LayoutParams) layoutParams).mStateFlag = SUCCESS;
}
view.setLayoutParams(layoutParams);
Expand Down Expand Up @@ -365,31 +361,40 @@ public void setState(@LoadState final int newState, boolean requestLayout) {

if (viewState != null && this.mState != newState) {
viewState.clearAnimation();
// removeAllViews();
viewState.setVisibility(View.GONE);
}
newView.clearAnimation();
//addView(newView);
newView.setVisibility(View.VISIBLE);
this.mState = newState;
if (requestLayout) {
requestLayout();
}
}

/**
* 延时,并强制更新状态
*
* @param newState 新的状态
* @param delayMillis 延时毫秒数
*/
public void setStateDelayed(@LoadState final int newState, long delayMillis) {
setStateDelayed(newState, delayMillis, true);
}

/**
* 延时更新状态
*
* @param newState
* @param delayed
* @param newState 新的状态
* @param delayMillis 延时毫秒数
* @param requestLayout 强制刷新布局
*/
public void setStateDelayed(@LoadState final int newState, long delayed) {
public void setStateDelayed(@LoadState final int newState, long delayMillis, final boolean requestLayout) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
setState(newState, false);
setState(newState, requestLayout);
}
}, delayed);
}, delayMillis);
}

/**
Expand Down Expand Up @@ -433,9 +438,9 @@ protected void onRestoreInstanceState(Parcelable state) {
}

/**
* 保存View的状态,返回的对象需要实现Parcelable接口并且继承自View#AbsSavedState类,
* 并带有public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()属性
* 否则onRestoreInstanceState接受的数据无法转为对应的类型
* 保存View的状态,返回的对象需要实现Parcelable接口并且继承自View#AbsSavedState类, 并带有public static
* final Parcelable.Creator<SavedState> CREATOR = new
* Parcelable.Creator<SavedState>()属性 否则onRestoreInstanceState接受的数据无法转为对应的类型
*
* @return 返回值必须实现Parcelable接口并且继承自View#AbsSavedState类,否恢复状态异常
*/
Expand Down Expand Up @@ -490,7 +495,6 @@ public int getState() {
return this.mState;
}


@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
Expand Down Expand Up @@ -520,7 +524,6 @@ public int getState() {
}
}


@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
Expand Down

0 comments on commit fff388f

Please sign in to comment.