hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
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
package com.mm.android.deviceaddmodule.mobilecommon.widget.sticky.stickylistheaders;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
 
class WrapperView extends ViewGroup {
 
    View item;
    Drawable divider;
    int dividerHeight;
    View header;
 
    public WrapperView(Context c) {
        super(c);
    }
 
    void update(View item, View header, Drawable divider, int dividerHeight) {
        if (item == null) {
            throw new NullPointerException("List view item must not be null.");
        }
 
        if (this.item != item) {
            removeView(this.item);
            this.item = item;
            final ViewParent parent = item.getParent();
            if(parent != null && parent != this) {
                if(parent instanceof ViewGroup) {
                    ((ViewGroup) parent).removeView(item);
                }
            }
            addView(item);
        }
 
        if (this.header != header) {
            if (this.header != null) {
                removeView(this.header);
            }
            this.header = header;
            if (header != null) {
                addView(header);
            }
        }
 
        if (this.divider != divider) {
            this.divider = divider;
            this.dividerHeight = dividerHeight;
            invalidate();
        }
    }
 
    boolean hasHeader() {
        return header != null;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth,
                MeasureSpec.EXACTLY);
        int measuredHeight = 0;
        if (header != null) {
            LayoutParams params = header.getLayoutParams();
            if (params != null && params.height > 0) {
                header.measure(childWidthMeasureSpec,
                        MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
            } else {
                header.measure(childWidthMeasureSpec,
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            }
            measuredHeight += header.getMeasuredHeight();
        } else if (divider != null) {
            measuredHeight += dividerHeight;
        }
        LayoutParams params = item.getLayoutParams();
        if (params != null && params.height > 0) {
            item.measure(childWidthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
        } else {
            item.measure(childWidthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        }
        measuredHeight += item.getMeasuredHeight();
 
        setMeasuredDimension(measuredWidth, measuredHeight);
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
 
        // don't really know why these values aren't what i want them to be from
        // the start
        l = 0;
        t = 0;
        r = getWidth();
        b = getHeight();
 
        if (header != null) {
            header.layout(l, t, r, header.getMeasuredHeight());
            item.layout(l, header.getMeasuredHeight(), r, b);
        } else if (divider != null) {
            divider.setBounds(l, t, r, dividerHeight);
            item.layout(l, dividerHeight, r, b);
        } else {
            item.layout(l, t, r, b);
        }
    }
 
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (header == null && divider != null) {
            // Drawable.setBounds() does not seem to work pre-honeycomb. So have
            // to do this instead
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                canvas.clipRect(0, 0, getWidth(), dividerHeight);
            }
            divider.draw(canvas);
        }
    }
}