简体   繁体   English

如何在 NestedScrollView 中使用 RecyclerView?

[英]How to use RecyclerView inside NestedScrollView?

How to use RecyclerView inside NestedScrollView ?如何在NestedScrollView中使用RecyclerView RecyclerView content is not visible after setting adapter.设置适配器后RecyclerView内容不可见。

UPDATE layout code updated. UPDATE布局代码已更新。

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

Replace your recyclerView with,将您的 recyclerView 替换为,

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

here,这里,

app:layout_behavior="@string/appbar_scrolling_view_behavior"

will manage the rest of things.将管理其余的事情。

One more thing, no need to put your recyclerView inside NestedScrollView还有一件事,无需将您的 recyclerView 放在 NestedScrollView 中

UPDATE 1更新 1

Since Android Support Library 23.2.0 there were added method setAutoMeasureEnabled(true) for LayoutManagers.从 Android 支持库 23.2.0 开始,为 LayoutManager 添加了setAutoMeasureEnabled(true)方法。 It makes RecyclerView to wrap it's content and works like a charm.它使 RecyclerView 包装它的内容并且像魅力一样工作。
http://android-developers.blogspot.ru/2016/02/android-support-library-232.html http://android-developers.blogspot.ru/2016/02/android-support-library-232.html

So just add something like this:所以只需添加如下内容:

    LayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setAutoMeasureEnabled(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setNestedScrollingEnabled(false);


UPDATE 2更新 2

Since 27.1.0 setAutoMeasureEnabled is deprecated, so you should provide custom implementation of LayoutManager with overridden method isAutoMeasureEnabled()由于 27.1.0 setAutoMeasureEnabled已弃用,因此您应该使用重写方法isAutoMeasureEnabled()提供 LayoutManager 的自定义实现

But after many cases of usage RecyclerView I strongly recommend not to use it in wrapping mode , cause this is not what it is intended for.但是在多次使用 RecyclerView 之后,我强烈建议不要在包装模式下使用它,因为这不是它的用途。 Try to refactor whole your layout using normal single RecyclerView with several items' types.尝试使用具有多个项目类型的普通单个 RecyclerView 重构整个布局。 Or use approach with LinearLayout that I described below as last resort或者使用我在下面描述的作为最后手段的 LinearLayout 的方法


Old answer (not recommended)旧答案(不推荐)

You can use RecyclerView inside NestedScrollView .您可以在NestedScrollView使用RecyclerView First of all you should implement your own custom LinearLayoutManager , it makes your RecyclerView to wrap its content.首先,您应该实现自己的自定义LinearLayoutManager ,它使您的RecyclerView包装其内容。 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之后,将此LayoutManager用于您的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 .这里setNestedScrollingEnabled(false)禁用RecyclerView滚动,因此它不会拦截来自NestedScrollView滚动事件。 And setHasFixedSize(false) determine that changes in adapter content can change the size of the RecyclerView并且setHasFixedSize(false)确定适配器内容的变化可以改变RecyclerView的大小

Important note: 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有很多项目,我建议您使用基于自定义LinearLayout的列表视图实现,为它创建 Adapter 的模拟,然后让它表现得像ListViewRecyclerView

  1. You need to use support library 23.2.0 (or) above您需要使用支持库 23.2.0(或)以上

  2. and RecyclerView height will be wrap_content .RecyclerView高度将为wrap_content

  3. recyclerView.setNestedScrollingEnabled(false)

But by doing this the recycler pattern doesn't work .但是这样做回收器模式不起作用 (ie all the views will be loaded at once because wrap_content needs the height of complete RecyclerView so it will draw all child Views at once. No view will be recycled). (即所有视图将一次加载,因为wrap_content需要完整RecyclerView的高度,因此它将一次绘制所有子Views 。不会回收任何视图)。 Try not to use this pattern unless it is really required.除非真的需要,否则尽量不要使用这种模式。 Try to use viewType and add all other views that need to scroll to RecyclerView rather than using RecyclerView in Scrollview .尝试使用viewType并添加所有其他需要滚动到RecyclerView视图,而不是在Scrollview使用RecyclerView The performance impact will be very high.性能影响将非常高。

To make it simple "it just acts as LinearLayout with all the child views"为了简单LinearLayout ,“它只是作为带有所有子视图的LinearLayout

You can use android:fillViewport="true" to make NestedScrollView measure the RecyclerView .您可以使用android:fillViewport="true"使NestedScrollView测量RecyclerView The RecyclerView will fill the remaining height. RecyclerView将填充剩余的高度。 so if you want to scroll the NestScrollView , you can set the RecyclerView 's minHeight .所以如果你想滚动NestScrollView ,你可以设置RecyclerViewminHeight

Simply adding recyclerView.setNestedScrollingEnabled(false);只需添加recyclerView.setNestedScrollingEnabled(false); before setAdapter itself worked for me.setAdapter本身为我工作之前。 I didn't add app:layout_behavior="@string/appbar_scrolling_view_behavior" anywhere & didn't set any custom layout manager我没有在任何地方添加app:layout_behavior="@string/appbar_scrolling_view_behavior"并且没有设置任何自定义布局管理器

<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:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Some Text..."
                android:padding="15dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:padding="15dp"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Quick Links"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:textAllCaps="true"
                android:paddingLeft="20dp"
                android:drawableLeft="@drawable/ic_trending_up_black_24dp"
                android:drawablePadding="10dp"
                android:layout_marginBottom="10dp"
                android:textSize="16sp"/>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#efefef"/>

            <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </LinearLayout>

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

This is what working for me这对我有用

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>

For androidx it's called androidx.core.widget.NestedScrollView - and it scrolls alike butter with properties isScrollContainer and measureAllChildren enabled:对于androidx它被称为androidx.core.widget.NestedScrollView - 它像黄油一样滚动,并启用了属性isScrollContainermeasureAllChildren

<!-- Scrolling Content -->
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:isScrollContainer="true"
    android:measureAllChildren="true"

    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fastScrollEnabled="true"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:splitMotionEvents="false"
        android:verticalScrollbarPosition="right"/>

</androidx.core.widget.NestedScrollView>

There is a simple and testing code u may check有一个简单的测试代码你可以检查

<android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.RecyclerView
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
   </android.support.v4.widget.NestedScrollView>

Try to use this library - https://github.com/serso/android-linear-layout-manager .尝试使用这个库 - https://github.com/serso/android-linear-layout-manager

LayoutManager of the library makes RecyclerView wraps its contents.库的 LayoutManager 使 RecyclerView 包装其内容。 In this case RecyclerView will be "as big as inner views", so it will not have a scrollbars and user will use scrolling abilities of NestedScrollView.在这种情况下,RecyclerView 将“与内部视图一样大”,因此它不会有滚动条,用户将使用 NestedScrollView 的滚动功能。 Therefore, it will not be ambiguous like "scrollable inside scrollable".因此,它不会像“scrollable inside scrollable”那样含糊不清。

I used RecyclerView inside a NestedScrollView and it worked for me.我在NestedScrollView 中使用了RecyclerView ,它对我有用 The only gotcha I had to keep in mind was that a NestedScrollView takes only one child view.我必须记住的唯一问题是 NestedScrollView 只需要一个子视图。 So in my case I used of LienearLayout viewgroup which was housing my RecyclerView plus a number of other views that I needed.所以在我的例子中,我使用了 LienearLayout 视图组,它包含我的 RecyclerView 以及我需要的一些其他视图。

I experience one issue putting my RecyclerView inside the NestedScrollView.我遇到一个问题,将我的 RecyclerView 放在 NestedScrollView 中。 I realized that scrolling the content of my RecyclerView slacked.我意识到滚动我的 RecyclerView 的内容松懈了。

I later realized that my RecyclerView was receiving the scrolling event and therefore was conflicting with the scrolling behavior of the NestedScrollView.后来我意识到我的 RecyclerView 正在接收滚动事件,因此与 NestedScrollView 的滚动行为发生冲突。

So to solve that problem, I had to disable the scroll functionality of my RecyclerView with this method movieListNewRecyclerView.setNestedScrollingEnabled(false);所以为了解决这个问题,我不得不用这个方法movieListNewRecyclerView.setNestedScrollingEnabled(false);禁用我的 RecyclerView 的滚动功能movieListNewRecyclerView.setNestedScrollingEnabled(false);

You can checkout my Instagram for a short video of what I actually did.你可以在我的 Instagram 上查看我实际所做的短视频。 This is my instagram handle ofelix03这是我的 instagram 句柄 ofelix03

点击这张图片看看我做了什么

Here is the code that I'm using to avoid scrolling issues:这是我用来避免滚动问题的代码:

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

I have Viewpager and RecyclerView inside the NestedScrollView.我在 NestedScrollView 中有 Viewpager 和 RecyclerView。 After adding below lines添加以下行后

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

I solved slow scroll and scroll lag issue.我解决了慢速滚动和滚动延迟问题。

if you want to use RecyclerView in NestedScrollView this is a simple tricky, just set :如果你想在NestedScrollView 中使用RecyclerView这是一个简单的技巧,只需设置:

RecyclerView回收器视图

  • recyclerView.setHasFixedSize(false) (java/kt) recyclerView.setHasFixedSize(false) (java/kt)

  • android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"

  • android:layout_height="wrap_content" android:layout_height="wrap_content"

  • android:overScrollMode="never" android:overScrollMode="从不"

NestedScrollView嵌套滚动视图

  • android:fillViewport="true"机器人:fillViewport =“真”

this is work for me, and you can use many RecyclerView in NestedScrollView with this too.这对我有用,你也可以在 NestedScrollView 中使用许多 RecyclerView 。

If you are using RecyclerView-23.2.1 or later.如果您使用RecyclerView-23.2.1或更高版本。 Following solution will work just fine:以下解决方案将正常工作:

In your layout add RecyclerView like this:在你的布局中添加 RecyclerView 像这样:

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

And in your java file:在你的java文件中:

RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
layoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(new YourListAdapter(getContext()));

Here layoutManager.setAutoMeasureEnabled(true);这里layoutManager.setAutoMeasureEnabled(true); will do the trick.会做的伎俩。

Check out this issue and this developer blog for more information.查看此问题此开发人员博客以获取更多信息。

If you are using RecyclerView ScrollListener inside NestedScrollView , addOnScrollListener listener not working properly if you are used both.如果您在NestedScrollView中使用RecyclerView ScrollListener ,如果您同时使用这两个, addOnScrollListener 侦听器将无法正常工作。

Use this code.使用此代码。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState); 
              ......
        }
    });

this code working fine RecyclerView ScrollListener inside NestedScrollView .此代码在NestedScrollView内工作正常RecyclerView ScrollListener

thanks谢谢

You can't use a recycler view within a nested scroll view.您不能在嵌套滚动视图中使用回收器视图。 It's not intended to contain further scrollable views but it's because it's a child of a scrolling layout itself that you need the nested scrolling view.它不打算包含更多可滚动视图,但因为它是滚动布局本身的子项,所以您需要嵌套滚动视图。 I had the same issue but in the end I moved my textview to be a headerview within the recyclerview, made the recyclerview a direct child of the coordinator layout and deleted the nested scroll view.我有同样的问题,但最后我将我的 textview 移动到 recyclerview 中的 headerview,使 recyclerview 成为协调器布局的直接子项,并删除了嵌套的滚动视图。 Then all my problems were gone.然后我所有的问题都消失了。

One solution to keep the recycling feature of the recyclerview and avoiding the recyclerview to load all your data is setting a fix height in the recyclerview itself.保持 recyclerview 的回收功能并避免 recyclerview 加载所有数据的一种解决方案是在 recyclerview 本身中设置固定高度。 By doing this the recyclerview is limited only to load as much as its height can show the user thus recycling its element if ever you scroll to the bottom/top.通过这样做,recyclerview 只能加载尽可能多的高度可以向用户显示,因此如果您滚动到底部/顶部,则回收其元素。

There are a lot of good answers.有很多很好的答案。 The key is that you must set nestedScrollingEnabled to false .关键是您必须将nestedScrollingEnabled设置为false As mentioned above you can do it in java code:如上所述,您可以在 Java 代码中执行此操作:

mRecyclerView.setNestedScrollingEnabled(false);

But also you have an opportunity to set the same property in xml code ( android:nestedScrollingEnabled="false" ):但您也有机会在 xml 代码中设置相同的属性( android:nestedScrollingEnabled="false" ):

 <android.support.v7.widget.RecyclerView
     android:id="@+id/recyclerview"
     android:nestedScrollingEnabled="false"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
nestedScrollView.setNestedScrollingEnabled(true);

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //...
        }
    });


<androidx.core.widget.NestedScrollView
    android:id="@+id/nested"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_below="@id/appBarLayout_orders"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <androidx.constraintlayout.widget.ConstraintLayout ...

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

I had to implement CoordinatorLayout with toolbar scrolling and it just took me all the day messing around this.我不得不使用工具栏滚动来实现 CoordinatorLayout,这让我花了一整天的时间来解决这个问题。 I've got it working by removing NestedScrollView at all.我已经通过删除 NestedScrollView 让它工作了。 So I'm just using RelativeLayout at the root.所以我只是在根上使用RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_nearby"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

do not use recyclerView inside NestedScrollView.不要在 NestedScrollView 中使用 recyclerView。 it may cause cascading problems!它可能会导致级联问题! I suggest using ItemViewTypes in RecyclerView for handling multiple kinds of views.我建议在 RecyclerView 中使用 ItemViewTypes 来处理多种视图。 just add a RecyclerView with match_parent width and height.只需添加一个具有 match_parent 宽度和高度的 RecyclerView 。 then in your recyclerViewAdapter override getItemViewType and use position for handling what layout to be inflated.然后在您的 recyclerViewAdapter 覆盖 getItemViewType 并使用 position 来处理要膨胀的布局。 after that you can handle your view holder by using onBindViewHolder method.之后,您可以使用 onBindViewHolder 方法处理您的视图持有者。

You can use my sample code你可以使用我的示例代码

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:id="@+id/fl_all_brand"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".view.fragment.AllBrandFragment">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <LinearLayout
                android:id="@+id/fl_all_brand1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical">


                <!--<include layout="@layout/content_home" />-->

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />


                <LinearLayout
                    android:id="@+id/recyclerLayout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_280sdp">


                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerviewobj"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="@dimen/_10sdp"
                        android:layout_marginTop="@dimen/_20sdp"
                        android:layout_marginEnd="@dimen/_10sdp"
                        android:orientation="horizontal"
                        android:nestedScrollingEnabled="false"
                        android:layout_marginBottom="@dimen/_20sdp"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

                        />
                </LinearLayout>



                <TextView
                    android:id="@+id/notfound"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="@dimen/_15sdp"
                    android:layout_marginTop="@dimen/_20sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/DISPLAY_LIGHTS"
                    android:gravity="center"
                    android:visibility="gone"
                    android:textColor="?attr/hintTextColors"
                    android:textSize="@dimen/_12ssp" />
                <TextView
                    android:id="@+id/recommendTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/addDeviceLayout"
                    android:layout_below="@+id/recyclerviewobj"
                    android:layout_marginStart="@dimen/_16sdp"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:fontFamily="@font/lexend_semibold"
                    android:text="@string/RECOMMENDATION"
                    android:textColor="@color/gray_scale_placehold"
                    android:textSize="@dimen/_16ssp" />

                <LinearLayout
                    android:id="@+id/addDeviceLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/createRoomLayout"
                    android:layout_marginTop="@dimen/_14sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtn"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/ic_thermostates_icon"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addDevice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/PROGRAM_DISPLAY_SENSOR_PLUGS"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/DISPLAY_SENSOR_SUB_TITLE"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/createRoomLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="@dimen/_9sdp"
                    android:layout_marginBottom="@dimen/_20sdp"
                    android:background="?attr/buttonTextColor"
                    android:orientation="vertical"
                    tools:visibility="visible">

                    <ImageView
                        android:id="@+id/addBtnRoom"
                        android:layout_width="@dimen/_28sdp"
                        android:layout_height="@dimen/_28sdp"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:src="@drawable/rgb_light_new"
                        app:tint="?attr/colorPrimaryDark" />

                    <TextView
                        android:id="@+id/addRooms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:fontFamily="@font/lexend_bold"
                        android:text="@string/DISPLAY_RGBW"
                        android:textColor="?attr/colorPrimaryDark"
                        android:textSize="@dimen/_12ssp" />

                    <TextView
                        android:id="@+id/summry1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_16sdp"
                        android:layout_marginTop="@dimen/_8sdp"
                        android:layout_marginBottom="@dimen/_6sdp"
                        android:fontFamily="@font/lexend_medium"
                        android:text="@string/PROGRAM_DISPLAY_RGB_MSG"
                        android:textColor="?attr/secondaryTextColor"
                        android:textSize="@dimen/_9ssp" />

                    <RelativeLayout
                        android:id="@+id/container99"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/_6sdp"
                        android:layout_marginTop="@dimen/_6sdp"
                        android:layout_marginBottom="@dimen/_10sdp">

                        <TextView
                            android:id="@+id/get_started_1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginStart="@dimen/_10sdp"
                            android:fontFamily="@font/lexend_semibold"
                            android:text="@string/RECOMMENDED_GROUP"
                            android:textAllCaps="true"
                            android:textSize="@dimen/_9ssp" />

                        <ImageView
                            android:id="@+id/forward_arrow1"
                            android:layout_width="@dimen/_16sdp"
                            android:layout_height="@dimen/_16sdp"
                            android:layout_alignParentEnd="true"
                            android:layout_marginRight="@dimen/_20sdp"
                            android:src="@drawable/ic_forward_arrow"
                            app:tint="?attr/colorPrimary" />

                    </RelativeLayout>
                </LinearLayout>




            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </LinearLayout>

</layout>

here use this property of recyclerview这里使用recyclerview的这个属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

and turned off recyclerview nested scrolling like this并像这样关闭recyclerview嵌套滚动

android:nestedScrollingEnabled="false"

In my case, this is what works for me就我而言,这对我有用

  • Put android:fillViewport="true" inside NestedScrollView.android:fillViewport="true"放在 NestedScrollView 中。

  • Make the height of RecyclerView to wrap_content, ie android:layout_height="wrap_content"使RecyclerView的高度为wrap_content,即android:layout_height="wrap_content"

  • Add this in RecyclerView android:nestedScrollingEnabled="false"在 RecyclerView 中添加这个android:nestedScrollingEnabled="false"

OR或者

Programmatically, in your Kotlin class以编程方式,在您的 Kotlin 课程中

recyclerView.isNestedScrollingEnabled = false

mRecyclerView.setHasFixedSize(false)

I have used this awesome extension (written in kotlin but can be also used in Java)我使用过这个很棒的扩展(用 kotlin 编写,但也可以用在 Java 中)

https://github.com/Widgetlabs/expedition-nestedscrollview https://github.com/Widgetlabs/expedition-nestedscrollview

Basically you get the NestedRecyclerView inside any package lets say utils in your project, then just create your recyclerview like基本上你在任何包中都得到了NestedRecyclerView可以说你的项目中的 utils,然后创建你的 recyclerview 就像

 <com.your_package.utils.NestedRecyclerView
      android:id="@+id/rv_test"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

Check this awesome article by Marc Knaup查看 Marc Knaup 的这篇很棒的文章

https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a

For my case the child of NestedScrollview is ConstraintLayout.就我而言,NestedScrollview 的孩子是 ConstraintLayout。 It is not working as expected i replaced it to LinearLayout.它没有按预期工作,我将其替换为 LinearLayout。 Maybe it helps someone.也许它可以帮助某人。

<androidx.core.widget.NestedScrollView 
  android:id="@+id/nestedScrollView" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent">

  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:descendantFocusability="blocksDescendants">

    <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:nestedScrollingEnabled="false" />

  </LinearLayout>

</androidx.core.widget.NestedScrollView>

At least as far back as Material Components 1.3.0-alpha03, it doesn't matter if the RecyclerView is nested (in something other than a ScrollView or NestedScrollView).至少早在 Material Components 1.3.0-alpha03 时,RecyclerView 是否嵌套(在 ScrollView 或 NestedScrollView 之外的其他东西中)都无关紧要。 Just put app:layout_behavior="@string/appbar_scrolling_view_behavior" on its top level parent that's a sibling of the AppBarLayout in the CoordinatorLayout.只需将app:layout_behavior="@string/appbar_scrolling_view_behavior"放在其顶级父级上,该父级是 CoordinatorLayout 中 AppBarLayout 的同级。

This has been working for me when using a single Activity architecture with Jetpack Naviagation, where all Fragments are sharing the same AppBar from the Activity's layout.当使用带有 Jetpack Naviagation 的单个 Activity 架构时,这对我有用,其中所有 Fragment 共享来自 Activity 布局的相同 AppBar。 I make the FragmentContainer the direct child of the CoordinatorLayout that also contains the AppBarLayout, like below.我使 FragmentContainer 成为 CoordinatorLayout 的直接子项,它也包含 AppBarLayout,如下所示。 The RecyclerViews in the various fragments are scrolling normally and the AppBar folds away and reappears as expected.各个片段中的 RecyclerViews 正常滚动,AppBar 折叠并按预期重新出现。

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation"/>

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:liftOnScroll="true">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_scrollFlags="scroll|enterAlways|snap" />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

liftOnScroll (used to for app bars to look like they have zero elevation when at the top of the page) works if each fragment passes the ID of its RecyclerView to AppBarLayout.liftOnScrollTargetViewId in Fragment.onResume .如果每个片段将其 RecyclerView 的 ID 传递给Fragment.onResume AppBarLayout.liftOnScrollTargetViewIdliftOnScroll (用于应用栏在页面顶部时看起来像它们的高度为零)起作用。 Or pass 0 if the Fragment doesn't scroll.或者,如果 Fragment 不滚动,则传递 0。

RecyclerView with NestedScrollView带有 NestedScrollView 的 RecyclerView

    <android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM