AutoFollowBox

如果你有个Button或者view希望跟随Listview,GridView,并且要求adapter高度自适应,达到自动跟随的效果,那这个是个不错的方法
标签: Android

[1].[文件] AutoFollowBox.java ~ 4KB    下载(8) 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.phodev.andtools.widget;
 
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
 
/**
  * 由于ListView,Gridiew,等高度设置为wrap_content的时候会有问题,如果此时下面还有其他view,
  * 当AdapterView的item很多的上海,不仅scroll有问题,底部跟随的view也会跑到可是范围外
  *
  * <pre>
  * <com.phodev.widgets.AutoFollowBox>
  *      <ListView
  *       android:layout_width="fill_parent"
  *       android:layout_height="wrap_content"// 必须是wrap content,否则没有必要使用该组建,直接布局即可
  *      />
  *      <OtherView/>//有切只有有一个OtherView如果底部是多个View,可以放到一个布局里面。总是AutoFollowBox有切职能有两个Child
  * <com.phodev.widgets.AutoFollowBox/>
  * </pre>
  *
  * @author sky
  *
  */
public class AutoFollowBox extends ViewGroup {
 
     public AutoFollowBox(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
 
     public AutoFollowBox(Context context) {
         super (context);
     }
 
     private Rect marginInfo = new Rect();
 
     @Override
     protected void onLayout( boolean changed, int l, int t, int r, int b) {
         int childCount = getChildCount();
         int maxW = r - l;
         int cl, ct, cr, cb;
         int heightOffset = 0 ;
         for ( int i = 0 ; i < childCount; i++) {
             View v = getChildAt(i);
             getMargingInfo(v, marginInfo);
             cl = marginInfo.left;
             ct = marginInfo.top + heightOffset;
             cr = maxW - marginInfo.right;
             cb = ct + v.getMeasuredHeight();
             v.layout(cl, ct, cr, cb);
             heightOffset = cb + marginInfo.bottom;
         }
     }
 
     @Override
     protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
         int childCount = getChildCount();
         if (childCount != 2 ) {
             throw new RuntimeException(
                     "mast only 2 views is AutoFoolowAdapterViewBox" );
         }
         //
         View stretchView = getChildAt( 0 );
         View followView = getChildAt( 1 );
         // start measure
         int offsetBottom = 0 ;
         int followViewHeihgt = 0 ;
         if (followView.getVisibility() == View.VISIBLE) {
             LayoutParams lp = followView.getLayoutParams();
             measureChildWithMargins(followView, widthMeasureSpec, 0 ,
                     heightMeasureSpec, 0 );
             followViewHeihgt = followView.getMeasuredHeight();
             if (lp instanceof MarginLayoutParams) {
                 MarginLayoutParams mlp = (MarginLayoutParams) lp;
                 offsetBottom = mlp.topMargin + mlp.bottomMargin
                         + followViewHeihgt;
             }
         }
         //
         if (stretchView.getVisibility() == View.VISIBLE) {
             measureChildWithMargins(stretchView, widthMeasureSpec, 0 ,
                     heightMeasureSpec, offsetBottom);
         }
         //
         int maxWantWitdh = Math.max(getMeasuerWidthWithMarging(followView),
                 getMeasuerWidthWithMarging(stretchView));
         int maxWantHeight = getMeasuerHeightWithMarging(stretchView)
                 + getMeasuerHeightWithMarging(followView);
         //
         int measuredWidth = resolveSize(maxWantWitdh, widthMeasureSpec);
         int measuredHeight = resolveSize(maxWantHeight, heightMeasureSpec);
         setMeasuredDimension(measuredWidth, measuredHeight);
     }
 
     private int getMeasuerHeightWithMarging(View v) {
         LayoutParams lp = v.getLayoutParams();
         int hAdd = 0 ;
         if (lp instanceof MarginLayoutParams) {
             MarginLayoutParams mlp = (MarginLayoutParams) lp;
             hAdd = mlp.topMargin + mlp.bottomMargin;
         }
         return v.getMeasuredHeight() + hAdd;
     }
 
     private int getMeasuerWidthWithMarging(View v) {
         LayoutParams lp = v.getLayoutParams();
         int wAdd = 0 ;
         if (lp instanceof MarginLayoutParams) {
             MarginLayoutParams mlp = (MarginLayoutParams) lp;
             wAdd = mlp.leftMargin + mlp.rightMargin;
         }
         return v.getMeasuredWidth() + wAdd;
     }
 
     private void getMargingInfo(View v, Rect out) {
         if (v != null && v.getLayoutParams() instanceof MarginLayoutParams) {
             MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
             //
             out.set(mlp.leftMargin, //
                     mlp.topMargin, //
                     mlp.rightMargin, //
                     mlp.bottomMargin);
         } else {
             out.set( 0 , 0 , 0 , 0 );
         }
     }
 
     @Override
     protected LayoutParams generateDefaultLayoutParams() {
         return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
                 LayoutParams.WRAP_CONTENT);
     }
 
     @Override
     public LayoutParams generateLayoutParams(AttributeSet attrs) {
         return new MarginLayoutParams(getContext(), attrs);
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值