查了资料可以有三种实现方式:
第一种:直接在代码中判断,当recycler元素大于某个个数时,就将recycler的高度固定;
private fun setRecyclerMaxHeigh(view: RecyclerView, maxHeight: Float) {
val lp: ViewGroup.LayoutParams = view.getLayoutParams()
if (view.childCount> 4) {
Log.d("myLog", view.childCount.toString())
lp.height = DensityUtil.dip2px(maxHeight)
} else {
Log.d("myLog", view.childCount.toString())
lp.height = DensityUtil.dip2px(0f)
}
view.layoutParams = lp
}
第二种:重写RecyclerView加一个最大高度的属性MaxHeightRecyclerView:
public class MaxHeightRecyclerView extends RecyclerView {
private int mMaxHeight;
public MaxHeightRecyclerView(Context context) {
super(context);
}
public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public MaxHeightRecyclerVi