Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题

原创 2015年07月16日 17:24:37

scrollview 嵌套recyclerview 时,recyclerview不显示,这就需要我们自己计算recyclerview的高度,比如:

 ViewGroup.LayoutParams mParams = recyclerView.getLayoutParams();
        mParams.height = (CommonUtils.getScreenWidthPX(getActivity()) * 480 / 720 + CommonUtils.dipToPixels(40)) * num + CommonUtils.dipToPixels(8);
        mParams.width = CommonUtils.getScreenWidthPX(getActivity());
        recyclerView.setLayoutParams(mParams);

这中方法适合item高度比较好计算的情形,但要遇到里面的item高度不一定这就需要我们重写recyclerview的高度了,以前嵌套listview的时候我们只需重写listview 然后重写

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

但是这种方法在recyclerview重写不管用。
我们此时要重写的的是LinearLayoutManager或GridLayoutManager

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 {
        }
    }
}

=======

public class FullyGridLayoutManager extends GridLayoutManager {

    private int mwidth = 0;
    private int mheight = 0;

    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];

    public int getMwidth() {
        return mwidth;
    }

    public void setMwidth(int mwidth) {
        this.mwidth = mwidth;
    }

    public int getMheight() {
        return mheight;
    }

    public void setMheight(int mheight) {
        this.mheight = mheight;
    }

    @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:
        }
        setMheight(height);
        setMwidth(width);
        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();
            }
        }
    }
}

重写完之后,用就好说了,在adapter的onBindview和平常一样用就可以了

 final FullyGridLayoutManager manager = new FullyGridLayoutManager(context.getActivity(), 3);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        manager.setSmoothScrollbarEnabled(true);
        viewHolder.recyclerView.setLayoutManager(manager);

此种方法在4.x系统上好用,能显示滑动也流畅,但是在5.x上虽然显示正常,但是滑动的时候好像被粘住了,没有惯性效果。。。。然后郁闷了一下午。。。。
最后解决方法是重写最外层的Scrollview

**
 * 屏蔽 滑动事件
 * Created by fc on 2015/7/16.
 */
public class MyScrollview extends ScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;

    public MyScrollview(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}

这样就可以了,暴力屏蔽。。。。5以上的事件直接传递给了内层的recyclerview,所以我们把滑动事件拦截就好了。。。这里写图片描述
这里写图片描述
这里写图片描述

scrollview嵌套recyclerview时滑动没有惯性的解决

1、建一个类,继承scrollview,重写里面的方法: /** * 屏蔽 滑动事件 * * Created by 潘洲涛 on 2017/1/1. */ class MyScrollv...
  • qq_34698126
  • qq_34698126
  • 2017年01月01日 13:23
  • 2496

RecyclerView在5.0以上版本滑动不流畅(没有惯性效果)问题的处理

项目中发现,RecyclerView在5.0以上版本滑动不流畅,滑动的手指一离开屏幕,列表就不动了,没有惯性效果。调查原因,发现应该是与scroll bar的有关,据说是recyclerView 无法...
  • u010696826
  • u010696826
  • 2016年10月13日 17:08
  • 2620

Android6.0 ScrollView与RecyclerView滑动冲突的问题

前记:我有个这样的需求,在一个LinearLayout中Vertical展示俩个不同的recycleview,但是要求俩个recycleview都全部展示出来;在Android5.0的系统中,我的做法...
  • u014434080
  • u014434080
  • 2017年04月20日 10:59
  • 3529

解决Scrollview 嵌套recyclerview不能显示,高度不正常的问题

我们先看一个效果,问题说的就是中间的Grid效果在Scrollview 嵌套recyclerview显示问题,在Android Api 24是好的,不过在5,1,1版本(api 22)缺出现了问题最近...
  • xiangzhihong8
  • xiangzhihong8
  • 2016年10月29日 18:18
  • 3866

【Android】ScrollView嵌套RecyclerView,RecyclerView总是把它上面的控件顶出页面

ScrollView嵌套RecyclerView,当我离开当前页面,然后又回来时,RecyclerView就会把它上边的控件都挤出页面,它显示在页面最上边。原因应该是RecyclerView抢了焦点,...
  • yingpaixiaochuan
  • yingpaixiaochuan
  • 2016年11月16日 19:27
  • 6343

【Android】ListView、RecyclerView、ScrollView里嵌套ListView 相对优雅的解决方案:NestFullListView

一 背景概述:ScrollView里嵌套ListView,一直是Android开发者(反正至少是我们组)最讨厌的设计之一,完美打破ListView(RecyclerView)的复用机制,成功的将Nat...
  • zxt0601
  • zxt0601
  • 2016年09月10日 21:24
  • 17677

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

前言做 Android开发我们经常会遇到这种ScrollView嵌套Listview或者 嵌套RecyclerView的需求,当我们搜索解决方案时经常会遇到这句话you should never pu...
  • dreamsever
  • dreamsever
  • 2017年02月27日 16:09
  • 3076

Android开发中Scrollview嵌套recyclerview时,recyclerview不显示数据

这个问题和ScrollView嵌套ListView是一个起因,是因为在scrollview嵌套的空间中无法正确计算recyclerview的高度造成的。 解决办法:重写recyclerview的布局...
  • u012758803
  • u012758803
  • 2016年10月13日 20:22
  • 1700

Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决

一个比较长的界面一般都是Scrollview嵌套RecyclerView来解决.不过这样的UI并不是我们开发人员想看到的,实际上嵌套之后.因为Scrollview和RecyclerView都是滑动控件...
  • u010399316
  • u010399316
  • 2016年10月08日 10:14
  • 4272

scrollView嵌套recyclerView 显示不全(已解决)

毕竟recycleView 是新的控件,所以在最近的项目中我尽可能 的用recyclerView而不使用ListView,所以今天就碰见了一个跟listView的一个通用的问题,就是嵌套之后显示不全,...
  • p1522780457
  • p1522780457
  • 2017年04月18日 14:59
  • 4449
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题
举报原因:
原因补充:

(最多只允许输入30个字)