MaxLimitRecyclerView.java
2.36 KB
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
package com.prws.common.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.prws.common.R;
/**
* 设置最大高度的RecyclerView
*/
public class MaxLimitRecyclerView extends RecyclerView {
private int mMaxHeight;
private int mMaxWidth;
public MaxLimitRecyclerView(Context context) {
this(context, null);
}
public MaxLimitRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MaxLimitRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inti(attrs);
}
private void inti(AttributeSet attrs) {
if (getContext() != null && attrs != null) {
TypedArray typedArray = null;
try {
typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MaxLimitRecyclerView);
if (typedArray.hasValue(R.styleable.MaxLimitRecyclerView_limit_maxHeight)) {
mMaxHeight = typedArray.getDimensionPixelOffset(R.styleable.MaxLimitRecyclerView_limit_maxHeight, -1);
}
if (typedArray.hasValue(R.styleable.MaxLimitRecyclerView_limit_maxWidth)) {
mMaxWidth = typedArray.getDimensionPixelOffset(R.styleable.MaxLimitRecyclerView_limit_maxWidth, -1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (typedArray != null) {
typedArray.recycle();
}
}
}
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
boolean needLimit = false;
if (mMaxHeight >= 0 || mMaxWidth >= 0) {
needLimit = true;
}
if (needLimit) {
int limitHeight = getMeasuredHeight();
// int limitWith = getMeasuredWidth();
if (getMeasuredHeight() > mMaxHeight) {
limitHeight = mMaxHeight;
}
// if (getMeasuredWidth() > mMaxWidth) {
// limitWith = mMaxWidth;
// }
setMeasuredDimension(getMeasuredWidth(), limitHeight);
}
}
}