查了资料可以有三种实现方式:
第一种:直接在代码中判断,当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);
&nb

当RecyclerView中的item数量过多时,可以采用三种方法设置其固定高度:1) 在代码中判断并设定;2) 重写RecyclerView创建MaxHeightRecyclerView;3) 如果父布局为ConstraintLayout,可以通过调整布局约束实现。
9389

被折叠的 条评论
为什么被折叠?



