Android 6.0 解决recyclerview 在 scr

作者: android难民 | 来源:发表于2016-09-30 14:48 被阅读5906次

recycleview和scrollive的配合使用,在6.0以下正常使用。在6.0上高度显示不全了。
查了好久的资料,用过以下的方法都没用。遇到这问题的你,可尝试以下方法解决。

  1. 重写GridLayoutManager
public class FullyGridLayoutManager extends GridLayoutManager {
    public FullyGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, 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;
        int count = getItemCount();
        int span = getSpanCount();
        for (int i = 0; i < count; i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {
                if (i % span == 0) {
                    width = width + mMeasuredDimension[0];
                }
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                if (i % span == 0) {
                    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) {
        if (position < getItemCount()) {
            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();
            }
        }
    }
}
  1. 重写recycleview的onMeasure方法
     @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();

        setNestedScrollingEnabled(false);

    }

前面两种我都试了没用。最后一种是直接在recycleview加一层相对布局就可以了。反正我的问题解决了。

  <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/menuRv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/margin_16"
                    android:layout_marginRight="@dimen/margin_16">

                </android.support.v7.widget.RecyclerView>
                </RelativeLayout>

相关文章

网友评论

  • 225679e1fd46:稳哦。
  • roczheng:谢谢楼主,刚遇到这种情况,现在解决了
  • c338a43e7cf5:好厉害!问题解决!完美!
  • 59daa515dded:额,我没试过,但是很好奇,你这方法是怎么做到的
  • a6f721ee6bd8:感谢,找了一晚上都没解决,在你这一下好了
  • 彩色浪花之子:太巧,刚好用到
  • 一起哈啤:老哥,太稳了!超级感谢
  • 68234991c767:我是把适配器中view根布局layout_height用的match_parent改成wrap_content就好了
  • MidTse:谢谢楼主,完美解决我的问题
  • meteos:6.0的系统,ScrollView嵌套RecyclerView显示不全,按最后的方法加了一层相对布局,没有加android:descendantFocusability="blocksDescendants"这句,也能显示完全了。
  • 逆水行舟丶:可以的
  • 明明可以不写代码:马克,完美解决了我的问题,但是如果要做自动分页加载,就不能使用以上的方法。这样做的弊端是默认加载渲染了所有的item,失去了最重要的复用性,同时监听不到recycler的onScroll,自动分页的时候,会触发每一次分页加载。以后做滑动页面,还是写到一个recycler里面,设置不同的type
  • FTE:谢谢楼主的分享,今天刚好用到!
  • 菜鸟_一枚:谢谢,也尝试了好多,但是没有解决,看到了你的这个方法,确实解决了,感谢楼主 :blush:
  • f9f223630444:谢谢,终于解决了问题了,关键代码是android:descendantFocusability="blocksDescendants"

    该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

    属性的值有三种:

    beforeDescendants:viewgroup会优先其子类控件而获取到焦点

    afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

    blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
    jasonkxs:android:descendantFocusability="blocksDescendants" 你这个参数是设置在recyclerview上面还是设置在scrollview上面呢

本文标题:Android 6.0 解决recyclerview 在 scr

本文链接:https://www.haomeiwen.com/subject/lealyttx.html