Skip to content

Commit 5ffaf4d

Browse files
committed
fix #4
1 parent 5fb53c7 commit 5ffaf4d

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

library/src/main/java/me/bakumon/statuslayoutmanager/library/ReplaceLayoutHelper.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,41 @@ public ReplaceLayoutHelper(@NonNull View contentLayout) {
4545
private void getContentLayoutParams() {
4646
this.params = contentLayout.getLayoutParams();
4747
if (contentLayout.getParent() != null) {
48+
// 有直接的父控件
4849
this.parentLayout = (ViewGroup) contentLayout.getParent();
4950
} else {
51+
// 认为 contentLayout 是 activity 的跟布局
52+
// 所以它的父控件就是 android.R.id.content
5053
this.parentLayout = contentLayout.getRootView().findViewById(android.R.id.content);
5154
}
52-
int count = parentLayout.getChildCount();
53-
for (int index = 0; index < count; index++) {
54-
if (contentLayout == parentLayout.getChildAt(index)) {
55-
// 获取 contentLayout 在 parentLayout 中的位置
56-
this.viewIndex = index;
57-
break;
55+
if (parentLayout == null) {
56+
// 以上两种方法还没有获取到父控件
57+
// contentLayout 非 activity 的跟布局
58+
// 父控件就是自己
59+
if (contentLayout instanceof ViewGroup) {
60+
parentLayout = (ViewGroup) contentLayout;
61+
this.viewIndex = 0;
62+
} else {
63+
// 否则,contentLayout 是一个非 ViewGroup 的跟布局
64+
// 该情况,没有办法替换布局,因此不支持
65+
throw new IllegalStateException("参数错误:StatusLayoutManager#Build#with() 方法,不能传如一个非 ViewGroup 的跟布局");
66+
}
67+
} else {
68+
int count = parentLayout.getChildCount();
69+
for (int index = 0; index < count; index++) {
70+
if (contentLayout == parentLayout.getChildAt(index)) {
71+
// 获取 contentLayout 在 parentLayout 中的位置
72+
this.viewIndex = index;
73+
break;
74+
}
5875
}
5976
}
6077
this.currentLayout = this.contentLayout;
6178
}
6279

63-
public boolean showStatusLayout(View view) {
64-
// 如果当前显示的布局不是 view,才进行替换
80+
public void showStatusLayout(View view) {
6581
if (view == null) {
66-
return false;
82+
return;
6783
}
6884
if (currentLayout != view) {
6985
currentLayout = view;
@@ -75,13 +91,11 @@ public boolean showStatusLayout(View view) {
7591
// 替换 = 移除 + 添加
7692
parentLayout.removeViewAt(viewIndex);
7793
parentLayout.addView(view, viewIndex, params);
78-
return true;
7994
}
80-
return false;
8195
}
8296

83-
public boolean restoreLayout() {
84-
return showStatusLayout(contentLayout);
97+
public void restoreLayout() {
98+
showStatusLayout(contentLayout);
8599
}
86100

87101
}

0 commit comments

Comments
 (0)