首页 > 代码库 > AutoFollowBox

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
packagecom.phodev.andtools.widget;
 
importandroid.content.Context;
importandroid.graphics.Rect;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.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
 *
 */
publicclass AutoFollowBox extendsViewGroup {
 
    publicAutoFollowBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    publicAutoFollowBox(Context context) {
        super(context);
    }
 
    privateRect marginInfo = newRect();
 
    @Override
    protectedvoid onLayout(booleanchanged, intl, intt, intr, intb) {
        intchildCount = getChildCount();
        intmaxW = r - l;
        intcl, ct, cr, cb;
        intheightOffset = 0;
        for(inti = 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
    protectedvoid onMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
        intchildCount = getChildCount();
        if(childCount != 2) {
            thrownew RuntimeException(
                    "mast only 2 views is AutoFoolowAdapterViewBox");
        }
        //
        View stretchView = getChildAt(0);
        View followView = getChildAt(1);
        // start measure
        intoffsetBottom = 0;
        intfollowViewHeihgt = 0;
        if(followView.getVisibility() == View.VISIBLE) {
            LayoutParams lp = followView.getLayoutParams();
            measureChildWithMargins(followView, widthMeasureSpec, 0,
                    heightMeasureSpec,0);
            followViewHeihgt = followView.getMeasuredHeight();
            if(lp instanceofMarginLayoutParams) {
                MarginLayoutParams mlp = (MarginLayoutParams) lp;
                offsetBottom = mlp.topMargin + mlp.bottomMargin
                        + followViewHeihgt;
            }
        }
        //
        if(stretchView.getVisibility() == View.VISIBLE) {
            measureChildWithMargins(stretchView, widthMeasureSpec, 0,
                    heightMeasureSpec, offsetBottom);
        }
        //
        intmaxWantWitdh = Math.max(getMeasuerWidthWithMarging(followView),
                getMeasuerWidthWithMarging(stretchView));
        intmaxWantHeight = getMeasuerHeightWithMarging(stretchView)
                + getMeasuerHeightWithMarging(followView);
        //
        intmeasuredWidth = resolveSize(maxWantWitdh, widthMeasureSpec);
        intmeasuredHeight = resolveSize(maxWantHeight, heightMeasureSpec);
        setMeasuredDimension(measuredWidth, measuredHeight);
    }
 
    privateint getMeasuerHeightWithMarging(View v) {
        LayoutParams lp = v.getLayoutParams();
        inthAdd = 0;
        if(lp instanceofMarginLayoutParams) {
            MarginLayoutParams mlp = (MarginLayoutParams) lp;
            hAdd = mlp.topMargin + mlp.bottomMargin;
        }
        returnv.getMeasuredHeight() + hAdd;
    }
 
    privateint getMeasuerWidthWithMarging(View v) {
        LayoutParams lp = v.getLayoutParams();
        intwAdd = 0;
        if(lp instanceofMarginLayoutParams) {
            MarginLayoutParams mlp = (MarginLayoutParams) lp;
            wAdd = mlp.leftMargin + mlp.rightMargin;
        }
        returnv.getMeasuredWidth() + wAdd;
    }
 
    privatevoid getMargingInfo(View v, Rect out) {
        if(v != null&& v.getLayoutParams() instanceofMarginLayoutParams) {
            MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
            //
            out.set(mlp.leftMargin,//
                    mlp.topMargin,//
                    mlp.rightMargin,//
                    mlp.bottomMargin);
        }else{
            out.set(0,0,0,0);
        }
    }
 
    @Override
    protectedLayoutParams generateDefaultLayoutParams() {
        returnnew MarginLayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
    }
 
    @Override
    publicLayoutParams generateLayoutParams(AttributeSet attrs) {
        returnnew MarginLayoutParams(getContext(), attrs);
    }
}

AutoFollowBox