admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
package com.lcjian.library.widget;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ListView;
 
import com.lcjian.lcjianlibrary.R;
 
public class AdaptiveListView extends ListView {
    
    private int mMaxHeight;
 
    public AdaptiveListView(Context context) {
        this(context, null);
    }
    
    public AdaptiveListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public AdaptiveListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AdaptiveListView, defStyleAttr, 0);
        mMaxHeight = a.getDimensionPixelSize(R.styleable.AdaptiveListView_maxHeight, 0);
        a.recycle();
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mMaxHeight !=0 && getMeasuredHeight() > mMaxHeight) {
            setMeasuredDimension(getMeasuredWidth(), mMaxHeight);
        }
    }
 
    public void setMaxHeight(int maxHeight) {
        this.mMaxHeight = maxHeight;
        invalidate();
    }
}