设置内部RecyclerView的高度wrap_content [英] Set Height of Inner RecyclerView wrap_content

查看:831
本文介绍了设置内部RecyclerView的高度wrap_content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个recyclerview中有一个recyclerview.我想将内部recyclerview的高度设为"warp_content".

I have a recyclerview inside another recyclerview. I want to make height of inner recyclerview "warp_content".

我知道下面提到的新版本的recyclerview是可能的.

I came to know it is possible with new version of recyclerview which is mentioned below.

'com.android.support:recyclerview-v7:23.2.0'

但是问题是,通过将height设置为wrap_content,我只能看到第一项.

But the problem is, by setting height = wrap_content I can see only first item.

任何帮助都会很棒.

推荐答案

由于我使用的是RecyclerView,因此无法正常工作,请参见:

Because I am using a RecyclerView this does not work see:

https://code.google.com/p/android/issues/detail?id = 74772

嵌套回收站的视图高度不包含其内容

在这两个页面上,人们都建议扩展LinearLayoutManager并覆盖onMeasure()

On both of these pages people suggest to extend LinearLayoutManager and to override onMeasure()

RecyclerView默认情况下不包含wrap_content.您必须将RecyclerView与wrap_content布局管理器(例如WrapLinearLayoutManager,例如CustomLinearLayoutManager)一起使用.

RecyclerView doesn't have the wrap_content by default. You have to use the RecyclerView with wrap_content Layout Manager such as, WrapLinearLayoutManager e.g CustomLinearLayoutManager .

CustomLinearLayoutManager.java

CustomLinearLayoutManager.java

    public class CustomLinearLayoutManager extends LinearLayoutManager {

private static final String TAG = CustomLinearLayoutManager.class.getSimpleName();

public CustomLinearLayoutManager(Context context) {
    super(context);
}

public CustomLinearLayoutManager(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:
            width = widthSize;
        case View.MeasureSpec.AT_MOST:
        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);//fix 动态添加时报IndexOutOfBoundsException

        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();
    } 
}

}

这篇关于设置内部RecyclerView的高度wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆