Open xiaofeidev opened 6 years ago
public class NestedRecyclerView extends RecyclerView { public NestedRecyclerView(Context context) { super(context); }
public NestedRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public NestedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
FlowLayoutManager layoutManager = (FlowLayoutManager) getLayoutManager();
int widthMode = View.MeasureSpec.getMode(widthSpec);
int measureWidth = View.MeasureSpec.getSize(widthSpec);
int heightMode = View.MeasureSpec.getMode(heightSpec);
int measureHeight = View.MeasureSpec.getSize(heightSpec);
int width, height;
if (widthMode == View.MeasureSpec.EXACTLY) {
width = measureWidth;
} else {
//以实际屏宽为标准
width = getContext().getResources().getDisplayMetrics().widthPixels;
}
if (heightMode == View.MeasureSpec.EXACTLY) {
height = measureHeight;
} else {
height = layoutManager.getTotalHeight() + getPaddingTop() + getPaddingBottom();
}
setMeasuredDimension(width, height);
}
}
wrap_content又失效了,RecyclerView高度设置成wrap_content时显示不出来