一. 问题描述:
项目中在使用RecycleView的时候,为它设置了高度 wrap_content 或 match_parent, 但是RecycleView中的Item 没有显示。
二. 我的解决方式:
简单粗暴的解决方法, 我把RecycleView的高度限定死了,给它一个固定的值(比如: 100dp)。这样RecycleView中的Item就会显示出来。 但是这样的方式不能解决根本问题。
三.探索根本原因,找到解决方法:
在使用RecycleView的时候我们都会注意到
RecyclerView recyclerView = findViewById(R.id.recycle_view);
RecyclerView.LayoutManager lmanager = new LinearLayoutManager(this, RecyclerView.VERTICAL, true);
//GridLayoutManager gmanager = new GridLayoutManager(this, 3);
ManageAddressAdapter adapter = new ManageAddressAdapter(this, allAddresses);
recyclerView.setLayoutManager(lmanager);
recyclerView.setAdapter(adapter);
我们经常使用系统提供的 LinearLayoutManager,GridLayoutManager
,StaggeredGridLayoutManager 布局管理器。它们是管理RecycleView中Item子布局显示测量,定位等。根本原因就是管理器导致的(源码还没有具体研究)
解决方法:写一个自己的MyLayoutManager 重写onMeasure()测量方法
public class MyLinearLayoutManager extends LinearLayoutManager {
private static final String TAG = MyLinearLayoutManager.class.getSimpleName();
public MyLinearLayoutManager(Context context) {
super(context);
}
public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
//宽度模式, 高度模式
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
// 宽高的测量尺寸
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
//三种测量模式;
case View.MeasureSpec.EXACTLY: // 相当于match_parent
width = widthSize;
case View.MeasureSpec.AT_MOST: // 相当于wrap_content
case View.MeasureSpec.UNSPECIFIED: //未指明的;未详细说明的
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
try {
View view = recycler.getViewForPosition(0);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
为什么你的自定义View wrap_content不起作用?
更多了解:https://www.jianshu.com/p/ca118d704b5e 可以从中找找灵感。
最后感谢wyz上善若水 https://blog.csdn.net/wyz745322878/article/details/52062549
网友评论