如何使用RecyclerView里面NestedScrollView? [英] How to use RecyclerView inside NestedScrollView?

查看:804
本文介绍了如何使用RecyclerView里面NestedScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用RecyclerView里面NestedScrollView? RecyclerView内容设置适配器之后不可见的。

更新布局code更新。

 < android.support.v4.widget.NestedScrollView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=@扪/ keyline_1>

    < / RelativeLayout的>

    <查看
        机器人:ID =@ + ID /分离器
        机器人:layout_width =match_parent
        机器人:layout_height =1DP
        机器人:后台=#e5e5e5/>

    < android.support.v7.widget.RecyclerView
        机器人:ID =@ + ID /对话
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>

< /android.support.v4.widget.NestedScrollView>
 

解决方案

您可以使用 RecyclerView NestedScrollView 。 首先,你应该实现自己的自定义 LinearLayoutManager ,它使你的 RecyclerView 来包装其内容。 例如:

 公共类WrappingLinearLayoutManager扩展LinearLayoutManager
{

    公共WrappingLinearLayoutManager(上下文的背景下){
        超(上下文);
    }

    私人INT [] mMeasuredDimension =新INT [2];

    @覆盖
    公共布尔canScrollVertically(){
        返回false;
    }

    @覆盖
    公共无效onMeasure(RecyclerView.Recycler回收,RecyclerView.State状态,
            INT widthSpec,诠释heightSpec){
        最终诠释widthMode = View.MeasureSpec.getMode(widthSpec);
        最终诠释heightMode = View.MeasureSpec.getMode(heightSpec);

        最终诠释widthSize = View.MeasureSpec.getSize(widthSpec);
        最终诠释heightSize = View.MeasureSpec.getSize(heightSpec);

        INT宽度= 0;
        INT高= 0;
        的for(int i = 0; I< getItemCount();我++){
            如果(getOrientation()==水平){
                measureScrapChild(回收,我,
                        View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                宽度=宽度+ mMeasuredDimension [0];
                如果(我== 0){
                    高度= mMeasuredDimension [1];
                }
            } 其他 {
                measureScrapChild(回收,我,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);

                高度=身高+ mMeasuredDimension [1];
                如果(我== 0){
                    宽度= mMeasuredDimension [0];
                }
            }
        }

        开关(widthMode){
            案例View.MeasureSpec.EXACTLY:
                宽度= widthSize;
            案例View.MeasureSpec.AT_MOST:
            案例View.MeasureSpec.UNSPECIFIED:
        }

        开关(heightMode){
            案例View.MeasureSpec.EXACTLY:
                身高= heightSize;
            案例View.MeasureSpec.AT_MOST:
            案例View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(宽度,高度);
    }

    私人无效measureScrapChild(RecyclerView.Recycler回收,INT地位,诠释widthSpec,
            INT heightSpec,INT [] measuredDimension){

        查看查看= recycler.getViewForPosition(位置);
        如果(view.getVisibility()== View.GONE){
            measuredDimension [0] = 0;
            measuredDimension [1] = 0;
            返回;
        }
        //要添加项目装饰插图查看
        super.measureChildWithMargins(视图,0,0);
        RecyclerView.LayoutParams P =(RecyclerView.LayoutParams)view.getLayoutParams();
        INT childWidthSpec = ViewGroup.getChildMeasureSpec(
                widthSpec,
                getPaddingLeft()+ getPaddingRight()+ getDecoratedLeft(视图)+ getDecoratedRight(视图),
                p.width);
        INT childHeightSpec = ViewGroup.getChildMeasureSpec(
                heightSpec,
                getPaddingTop()+ getPaddingBottom()+ getDecoratedTop(视图)+ getDecoratedBottom(视图),
                p.height);
        view.measure(childWidthSpec,childHeightSpec);

        //获取饰测量
        measuredDimension [0] = getDecoratedMeasuredWidth(视图)+ p.leftMargin + p.rightMargin;
        measuredDimension [1] = getDecoratedMeasuredHeight(视图)+ p.bottomMargin + p.topMargin;
        recycler.recycleView(视图);
    }
}
 

这是使用后该布局管理 RecyclerView

  recyclerView.setLayoutManager(新WrappingLinearLayoutManager(的getContext()));
 

但你也应该调用这两种方法:

  recyclerView.setNestedScrollingEnabled(假);
recyclerView.setHasFixedSize(假);
 

下面 setNestedScrollingEnabled(假)禁用滚动的 RecyclerView ,所以它不会拦截从滚动事件 NestedScrollView 。而 setHasFixedSize(假)确定更改适配器的内容可以改变的大小 RecyclerView

希望它可以帮助你。

更新:

这个解决方案是小马车在某些情况下,并有问题性能比较,因此,如果你有很多的项目在 RecyclerView 我建议你自定义<$使用C $ C>的LinearLayout 实施基于列表视图中创建适配器的模拟它,使其行为像的ListView RecyclerView

How to use RecyclerView inside NestedScrollView? RecyclerView content not visible after setting adapter.

UPDATE layout code updated.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/keyline_1">

    </RelativeLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e5e5e5" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/conversation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

解决方案

You can use RecyclerView inside NestedScrollView. First of all you should implement your own custom LinearLayoutManager, it makes your RecyclerView to wrap its content. For example:

public class WrappingLinearLayoutManager extends LinearLayoutManager
{

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

    private int[] mMeasuredDimension = new int[2];

    @Override
    public boolean canScrollVertically() {
        return false;
    }

    @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++) {
            if (getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);

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

        View view = recycler.getViewForPosition(position);
        if (view.getVisibility() == View.GONE) {
            measuredDimension[0] = 0;
            measuredDimension[1] = 0;
            return;
        }
        // For adding Item Decor Insets to view
        super.measureChildWithMargins(view, 0, 0);
        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                widthSpec,
                getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),
                p.width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(
                heightSpec,
                getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view),
                p.height);
        view.measure(childWidthSpec, childHeightSpec);

        // Get decorated measurements
        measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
        measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
        recycler.recycleView(view);
    }
}

After that use this LayoutManager for your RecyclerView

recyclerView.setLayoutManager(new WrappingLinearLayoutManager(getContext()));

But you also should call those two methods:

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

Here setNestedScrollingEnabled(false) disable scrolling for RecyclerView, so it doesn't intercept scrolling event from NestedScrollView. And setHasFixedSize(false) determine that changes in adapter content can change the size of the RecyclerView

Hope it helps you

Update:

This solution is little buggy in some cases and has problems with perfomance, so if you have a lot of items in your RecyclerView I'd recommend to use custom LinearLayout-based implementation of list view, create analogue of Adapter for it and make it behave like ListView or RecyclerView

这篇关于如何使用RecyclerView里面NestedScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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