xiangcman / LayoutManager-FlowLayout

快速利用RecyclerView的LayoutManager搭建流式布局
674 stars 157 forks source link

wrap_content又失效了,RecyclerView高度设置成wrap_content时显示不出来 #23

Open xiaofeidev opened 6 years ago

xiaofeidev commented 6 years ago

wrap_content又失效了,RecyclerView高度设置成wrap_content时显示不出来

Mr-Zheng-yz commented 5 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);
}

}