关于Scrollview嵌套RecyclerView高度显示不全

前言

转载请注明出处:http://blog.csdn.net/dreamsever/article/details/58117072

做 Android开发我们经常会遇到这种ScrollView嵌套Listview或者 嵌套RecyclerView的需求,当我们搜索解决方案时经常会遇到这句话

you should never put a scrollable view inside another scrollable view

虽然很多人不推荐这种方法,但是总有一些人(比如我)面对产品设计的一些奇葩界面不得不采用这种方案,今天我就把我找到的Scrollview嵌套Recyclerview高度显示不全解决办法总结一下。(ScrollView嵌套Listview的情况也是老生常谈的话题,网上太多就不再赘述)

首先

网上已经有前人给我们写出了一个自定义的LinearLayoutManager,我相信你也可以搜索到,下面我先粘贴出来,感兴趣建议好好看看这个类的源码,不要只做搬运工,知其然也要知其所以然,但是使用中会遇到一些小问题,下面讲解

/**
 * 工具类 用于Scrollview嵌套recyclerview
 * 2017 2 17加入
 */
public class FullyLinearLayoutManager extends LinearLayoutManager {

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

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

    public FullyLinearLayoutManager(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);

        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                + " \nheightMode " + heightSpec
                + " \nwidthSize " + widthSize
                + " \nheightSize " + heightSize
                + " \ngetItemCount() " + getItemCount());

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

小问题,值得注意的地方

1,

给scrollview加入一个属性

android:fillViewport="true"

2,

处理滑动冲突情况,如下设置

recyclerView.setNestedScrollingEnabled(false);

3,

我开始用的scrollview,测试设备:小米note黑 adnroid6.0,由于我的里面有两个RecyclerView我发现下面的超出屏幕的部分就不显示了,其他的机型是可以的。网上搜索建议将Scrollview换成android.support.v4.widget.NestedScrollView,我试了一下确实正常工作了

总结

you should never put a scrollable view inside another scrollable view

如果这个界面确实要求不高,这样写确实省了不少功夫,那就这样干吧!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值