Skip to content

Commit 056e866

Browse files
committed
当内容体View只有一个的时候,优化嵌套布局
1 parent 45fa053 commit 056e866

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

library/src/main/java/com/think/tlr/TLRLinearLayout.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class TLRLinearLayout extends ViewGroup {
6060
* 需要向ContentLayout中添加的子view
6161
*/
6262
private List<View> mContentChilds;
63-
private LinearLayout mContentLayout;
63+
private View mContentView;
6464
private View mFooterView;
6565
private TLRCalculator mCalculator;
6666
private TLRUiHandlerWrapper mUiHandlerWrapper;
@@ -152,8 +152,6 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
152152
protected void onFinishInflate() {
153153
super.onFinishInflate();
154154
int count = getChildCount();
155-
mContentLayout = new LinearLayout(getContext());
156-
mContentLayout.setOrientation(LinearLayout.VERTICAL);
157155
for (int i = 0; i < count; i++) {
158156
View child = getChildAt(i);
159157
LayoutParams params = (LayoutParams) child.getLayoutParams();
@@ -193,19 +191,27 @@ protected void onFinishInflate() {
193191
}
194192
}
195193

196-
if (mContentChilds.size() == 0) {
194+
int contentChildSize = mContentChilds.size();
195+
if (contentChildSize == 0) {
197196
throw new RuntimeException("must have content view !!!");
197+
} else if (contentChildSize == 1) {
198+
removeView(mContentChilds.get(0));
199+
mContentView = mContentChilds.get(0);
200+
} else {
201+
LinearLayout layout = new LinearLayout(getContext());
202+
layout.setOrientation(LinearLayout.VERTICAL);
203+
for (View view : mContentChilds) {
204+
removeView(view);
205+
ViewGroup.MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
206+
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(params);
207+
layout.addView(view, llp);
208+
}
209+
TLRLog.d("ContentLayout child count:" + contentChildSize);
210+
mContentView = layout;
198211
}
199212

200-
for (View view : mContentChilds) {
201-
removeView(view);
202-
ViewGroup.MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
203-
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(params);
204-
mContentLayout.addView(view, llp);
205-
}
206-
addSelfView(mContentLayout, 0,
213+
addSelfView(mContentView, 0,
207214
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
208-
TLRLog.i("ContentLayout count:" + mContentLayout.getChildCount());
209215
}
210216

211217
@Override
@@ -268,7 +274,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
268274
if (child.getVisibility() != GONE) {
269275
// Measure the child.
270276
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
271-
if (child.equals(mContentLayout)) {
277+
if (child.equals(mContentView)) {
272278
width = child.getMeasuredWidth();
273279
height = child.getMeasuredHeight();
274280
} else if (child.equals(mHeaderView)) {
@@ -293,26 +299,28 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
293299
int parentTop = getPaddingTop();
294300
int parentBottom = parentTop + getMeasuredHeight();
295301

296-
if (mContentLayout != null && mContentLayout.getVisibility() != GONE) {
297-
LayoutParams lp = (LayoutParams) mContentLayout.getLayoutParams();
302+
if (mContentView != null && mContentView.getVisibility() != GONE) {
303+
LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
298304
int left = parentLeft + lp.leftMargin;
299305
int top = parentTop + lp.topMargin;
300306

301-
if (mCalculator.getTotalOffsetY() > 0 && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH) == 0) {
307+
if (mCalculator.getTotalOffsetY() > 0
308+
&& (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH) == 0) {
302309
top += totalOffsetY;
303-
} else if (mCalculator.getTotalOffsetY() < 0 && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_LOAD) == 0) {
310+
} else if (mCalculator.getTotalOffsetY() < 0
311+
&& (keepContentLayoutFlag & FLAG_KEEP_CONTENT_LOAD) == 0) {
304312
top += totalOffsetY;
305313
}
306314

307-
int right = left + mContentLayout.getMeasuredWidth();
308-
int bottom = top + mContentLayout.getMeasuredHeight();
315+
int right = left + mContentView.getMeasuredWidth();
316+
int bottom = top + mContentView.getMeasuredHeight();
309317

310318
if (DEBUG) {
311319
TLRLog.i("ContentLayout left:" + left + " right:" + right + " top:" + top
312320
+ " bottom:" + bottom);
313321
}
314322

315-
mContentLayout.layout(left, top, right, bottom);
323+
mContentView.layout(left, top, right, bottom);
316324
}
317325

318326
if (mHeaderView != null && mHeaderView.getVisibility() != GONE) {
@@ -359,10 +367,12 @@ void move(int y) {
359367
if (isEnableLoad() && isCanMoveFootByTLR() && mFooterView != null) {
360368
mFooterView.offsetTopAndBottom(y);
361369
}
362-
if (mCalculator.getTotalOffsetY() > 0 && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH) == 0) {
363-
mContentLayout.offsetTopAndBottom(y);
364-
} else if (mCalculator.getTotalOffsetY() < 0 && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_LOAD) == 0) {
365-
mContentLayout.offsetTopAndBottom(y);
370+
if (mCalculator.getTotalOffsetY() > 0
371+
&& (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH) == 0) {
372+
mContentView.offsetTopAndBottom(y);
373+
} else if (mCalculator.getTotalOffsetY() < 0
374+
&& (keepContentLayoutFlag & FLAG_KEEP_CONTENT_LOAD) == 0) {
375+
mContentView.offsetTopAndBottom(y);
366376
}
367377
}
368378

@@ -893,7 +903,7 @@ public void onLoadStatusChanged(View target, LoadStatus status) {
893903

894904
@Override
895905
public void onOffsetChanged(View target, boolean isRefresh, int totalOffsetY,
896-
int totalThresholdY, int offsetY, float threshOffset) {
906+
int totalThresholdY, int offsetY, float threshOffset) {
897907
if (DEBUG) {
898908
String name = target == null ? null : target.getClass().getSimpleName();
899909
TLRLog.v("onOffsetChanged target:" + name + " isRefresh:" + isRefresh

0 commit comments

Comments
 (0)