美文网首页
ScrollView与RecycleView的嵌套

ScrollView与RecycleView的嵌套

作者: 忘尘And | 来源:发表于2017-10-10 14:15 被阅读0次

最近由于项目需求,有个ScrollView与RecycleView的嵌套问题,当然首选是能不嵌套就不嵌套,考虑的是多条目布局,但是由于里面的item的随机的可以动态add或者remove,这就很是烦人,于是考虑嵌套,对变化的recycleview进行嵌套,对于以前的ScrollView与listview嵌套的习惯,首先就是想到的重写recycleview。。。。百度下,得知重写manager即可,下面记录下方法
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);

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

}
在manager重写后发现个问题,scrollView互动不流畅,于是又百度知。。还得重写ScrollView。。。见如下链接
http://blog.csdn.net/xuwei1213/article/details/53994679
,觉得太麻烦了,而且也达不到想要的效果,下面给出最终解决办法。。。蛮简单,在RecyclerView外面加了一层RelativeLayout,问题解决。如果你在API 23 & 24上也遇到这个问题,可以参考一下。

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/rv_fragment_find_tips_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollV
。。。如此就解决了。。。。至于滑动问题,设置 mRecycleView.setNestedScrollingEnabled(false)即可

相关文章

网友评论

      本文标题:ScrollView与RecycleView的嵌套

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