@@ -60,7 +60,7 @@ public class TLRLinearLayout extends ViewGroup {
60
60
* 需要向ContentLayout中添加的子view
61
61
*/
62
62
private List <View > mContentChilds ;
63
- private LinearLayout mContentLayout ;
63
+ private View mContentView ;
64
64
private View mFooterView ;
65
65
private TLRCalculator mCalculator ;
66
66
private TLRUiHandlerWrapper mUiHandlerWrapper ;
@@ -152,8 +152,6 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
152
152
protected void onFinishInflate () {
153
153
super .onFinishInflate ();
154
154
int count = getChildCount ();
155
- mContentLayout = new LinearLayout (getContext ());
156
- mContentLayout .setOrientation (LinearLayout .VERTICAL );
157
155
for (int i = 0 ; i < count ; i ++) {
158
156
View child = getChildAt (i );
159
157
LayoutParams params = (LayoutParams ) child .getLayoutParams ();
@@ -193,19 +191,27 @@ protected void onFinishInflate() {
193
191
}
194
192
}
195
193
196
- if (mContentChilds .size () == 0 ) {
194
+ int contentChildSize = mContentChilds .size ();
195
+ if (contentChildSize == 0 ) {
197
196
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 ;
198
211
}
199
212
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 ,
207
214
new LayoutParams (LayoutParams .MATCH_PARENT , LayoutParams .MATCH_PARENT ));
208
- TLRLog .i ("ContentLayout count:" + mContentLayout .getChildCount ());
209
215
}
210
216
211
217
@ Override
@@ -268,7 +274,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
268
274
if (child .getVisibility () != GONE ) {
269
275
// Measure the child.
270
276
measureChildWithMargins (child , widthMeasureSpec , 0 , heightMeasureSpec , 0 );
271
- if (child .equals (mContentLayout )) {
277
+ if (child .equals (mContentView )) {
272
278
width = child .getMeasuredWidth ();
273
279
height = child .getMeasuredHeight ();
274
280
} else if (child .equals (mHeaderView )) {
@@ -293,26 +299,28 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
293
299
int parentTop = getPaddingTop ();
294
300
int parentBottom = parentTop + getMeasuredHeight ();
295
301
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 ();
298
304
int left = parentLeft + lp .leftMargin ;
299
305
int top = parentTop + lp .topMargin ;
300
306
301
- if (mCalculator .getTotalOffsetY () > 0 && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH ) == 0 ) {
307
+ if (mCalculator .getTotalOffsetY () > 0
308
+ && (keepContentLayoutFlag & FLAG_KEEP_CONTENT_REFRESH ) == 0 ) {
302
309
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 ) {
304
312
top += totalOffsetY ;
305
313
}
306
314
307
- int right = left + mContentLayout .getMeasuredWidth ();
308
- int bottom = top + mContentLayout .getMeasuredHeight ();
315
+ int right = left + mContentView .getMeasuredWidth ();
316
+ int bottom = top + mContentView .getMeasuredHeight ();
309
317
310
318
if (DEBUG ) {
311
319
TLRLog .i ("ContentLayout left:" + left + " right:" + right + " top:" + top
312
320
+ " bottom:" + bottom );
313
321
}
314
322
315
- mContentLayout .layout (left , top , right , bottom );
323
+ mContentView .layout (left , top , right , bottom );
316
324
}
317
325
318
326
if (mHeaderView != null && mHeaderView .getVisibility () != GONE ) {
@@ -359,10 +367,12 @@ void move(int y) {
359
367
if (isEnableLoad () && isCanMoveFootByTLR () && mFooterView != null ) {
360
368
mFooterView .offsetTopAndBottom (y );
361
369
}
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 );
366
376
}
367
377
}
368
378
@@ -893,7 +903,7 @@ public void onLoadStatusChanged(View target, LoadStatus status) {
893
903
894
904
@ Override
895
905
public void onOffsetChanged (View target , boolean isRefresh , int totalOffsetY ,
896
- int totalThresholdY , int offsetY , float threshOffset ) {
906
+ int totalThresholdY , int offsetY , float threshOffset ) {
897
907
if (DEBUG ) {
898
908
String name = target == null ? null : target .getClass ().getSimpleName ();
899
909
TLRLog .v ("onOffsetChanged target:" + name + " isRefresh:" + isRefresh
0 commit comments