1、问题
ListView的item中嵌套了RecyclerView实现水平方向列表,导致RecyclerView高度不能正常显示。
2、传统解决方案
嵌套问题最基本的解决方法是重写onMeasure方法,手动测量ViewGroup的高度或者宽度。这里,因为我们的RecyclerView是水平的,而且每一个item的高度是相同的,所以只需要测出一个item的所占用高度(包括父控件的上下padding以及item的上下margin)作为RecyclerView的高度即可。代码如下:
public class MeasuredRecyclerView extends RecyclerView {
public MeasuredRecyclerView(Context context) {
this(context, null);
}
public MeasuredRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MeasuredRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle

本文探讨了ListView中嵌套RecyclerView时遇到的显示高度问题,以及两种解决方案:1) 重写onMeasure方法手动测量高度,2) 通过自定义LinearLayoutManager优化,确保在数据变更时正确布局。注意,对于高度不一致的item,需要遍历并设置最大高度。
最低0.47元/天 解锁文章
2万+

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



