@@ -45,25 +45,41 @@ public ReplaceLayoutHelper(@NonNull View contentLayout) {
45
45
private void getContentLayoutParams () {
46
46
this .params = contentLayout .getLayoutParams ();
47
47
if (contentLayout .getParent () != null ) {
48
+ // 有直接的父控件
48
49
this .parentLayout = (ViewGroup ) contentLayout .getParent ();
49
50
} else {
51
+ // 认为 contentLayout 是 activity 的跟布局
52
+ // 所以它的父控件就是 android.R.id.content
50
53
this .parentLayout = contentLayout .getRootView ().findViewById (android .R .id .content );
51
54
}
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
+ }
58
75
}
59
76
}
60
77
this .currentLayout = this .contentLayout ;
61
78
}
62
79
63
- public boolean showStatusLayout (View view ) {
64
- // 如果当前显示的布局不是 view,才进行替换
80
+ public void showStatusLayout (View view ) {
65
81
if (view == null ) {
66
- return false ;
82
+ return ;
67
83
}
68
84
if (currentLayout != view ) {
69
85
currentLayout = view ;
@@ -75,13 +91,11 @@ public boolean showStatusLayout(View view) {
75
91
// 替换 = 移除 + 添加
76
92
parentLayout .removeViewAt (viewIndex );
77
93
parentLayout .addView (view , viewIndex , params );
78
- return true ;
79
94
}
80
- return false ;
81
95
}
82
96
83
- public boolean restoreLayout () {
84
- return showStatusLayout (contentLayout );
97
+ public void restoreLayout () {
98
+ showStatusLayout (contentLayout );
85
99
}
86
100
87
101
}
0 commit comments