如何把RecyclerView里面NestedScrollView?里面、RecyclerView、NestedScrollView

2023-09-04 12:28:45 作者:何必強顏歡笑

通过创建 NestedScrollView 你可以把滚动鉴于在另一个滚动视图,只要这些实施 NestedScrollingChild 和 NestedScrollingParent 正确。

(这是不坏的设计模式伊恩湖(从谷歌)实际上是建议把一个 RecyclerView 这里nestedscrollview内:plus.google.com/u/0/ + AndroidDevelopers /职位/ 9kZ3SsXdT2T)

我希望把 RecyclerView 在 NestedScrollView ,幸运的是RecyclerView工具的 NestedScrollingChild 所以你可以把它里面的 NestedScrollView 。

 公共类RecyclerView扩展的ViewGroup实现ScrollingView,NestedScrollingChild
 

我看了这些帖子:

How使用RecyclerView里面NestedScrollView?

NestedScrolling与NestedScrollView,RecyclerView(水平),内CoordinatorLayout

不过,这个问题最为表决的解决方案是,它要求所有的 RecyclerView 的项目,因此,例如,如果它是一个永无止境的RecyclerView当用户到达结束在列表中选择要进行网络请求,然后使用该解决方案的 RecyclerView 调用服务器多次因为它会自动达到 RecyclerView 。

总之,如何设置参数,这样我就可以把 RecyclerView NestedScrollView 。(其实我想提出一个类似的ViewGroup或的FrameLayout作为RelativeLayout的nestedscrollview单公子,然后我想把recyclerview内的FrameLayout或RelativeLayout的)

RecyclerView 体验优化及入坑总结

当我把 RecyclerView NestedScrollView 没有任何显示。

为了创建一个示例项目,您可以使用 cheesesquare 并修改 CheeseDetailActivity 有RecyclerView。

虽然BNK的答案是不正确的,但BNK已经尝试了很多。所以,我要奖励他的奖金。还在寻找很好的解决方案......

解决方案

所以把RecyclerView内NestedScrollView直接将不幸显示任何内容。然而,有一种方法可以把NestedScrollView内recyclerview间接的 - 只是用的FrameLayout作为第三方来保存您的recyclerview

这是其中包含嵌套recyclerview中的FrameLayout您的活动类:

 <的FrameLayout
        机器人:ID =@ + ID /容器
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:fitsSystemWindows =真
        工具:上下文=。ExampleFragment
        应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior>

        < android.support.v4.widget.NestedScrollView
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:fillViewport =真
            >
        < /android.support.v4.widget.NestedScrollView>
   < /的FrameLayout>
 

把你的片段插入的FrameLayout (code在活动类中的坐)

  ExampleFragment exampleFragment =新ExampleFragment();

    FragmentManager FM = getSupportFragmentManager();
    FragmentTransaction英尺= fm.beginTransaction();
    ft.add(R.id.container,exampleFragment);
    ft.commit();
 

在您的 exampleFragment ,然后你可以把你的recyclerview。

 < XML版本=1.0编码=UTF-8&GT?;
    < RelativeLayout的
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / post_container
        机器人:后台=#E0E0E0>

            < android.support.v7.widget.RecyclerView
                机器人:ID =@ + ID / my_recycler_view
                机器人:滚动条=垂直

                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>
            < /android.support.v7.widget.RecyclerView>


    < / RelativeLayout的>
 

这是在片段 code:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                         捆绑savedInstanceState){

    super.onCreateView(充气,容器,savedInstanceState);
            llLayout =(RelativeLayout的)inflater.inflate(R.layout.example_fragment_layout,集装箱,假);
                    mRecyclerView =(RecyclerView)llLayout.findViewById(R.id.my_recycler_view);
 

以下是 CheeseSquare XML布局你应该有:

 < android.support.design.widget.CoordinatorLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID / main_content
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真正的>

    < android.support.design.widget.AppBarLayout
        机器人:ID =@ + ID / appbar
        机器人:layout_width =match_parent
        机器人:layout_height =@扪/ detail_backdrop_height
        机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
        机器人:fitsSystemWindows =真正的>

        < android.support.design.widget.CollapsingToolbarLayout
            机器人:ID =@ + ID / collapsing_toolbar
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            应用程序:layout_scrollFlags =滚动| exitUntilCollapsed
            机器人:fitsSystemWindows =真
            应用程序:contentScrim =?ATTR / colorPrimary
            应用程序:expandedTitleMarginStart =48dp
            应用程序:expandedTitleMarginEnd =64dp>

            < ImageView的
                机器人:ID =@ + ID /背景
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:scaleType =centerCrop
                机器人:fitsSystemWindows =真
                应用程序:layout_collapseMode =视差/>

            < android.support.v7.widget.Toolbar
                机器人:ID =@ + ID /工具栏
                机器人:layout_width =match_parent
                机器人:layout_height =?ATTR / actionBarSize
                应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
                应用程序:layout_collapseMode =针/>

        < /android.support.design.widget.CollapsingToolbarLayout>

    < /android.support.design.widget.AppBarLayout>

   <的FrameLayout
        机器人:ID =@ + ID /容器
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:fitsSystemWindows =真
        工具:上下文=。ExampleFragment
        应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior>

        < android.support.v4.widget.NestedScrollView
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:fillViewport =真
            >
        < /android.support.v4.widget.NestedScrollView>
   < /的FrameLayout>

    < android.support.design.widget.FloatingActionButton
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        应用程序:layout_anchor =@ ID / appbar
        应用程序:layout_anchorGravity =底部|右|结束
        机器人:SRC =@可绘制/ ic_discuss
        机器人:layout_margin =@扪/ fab_margin
        机器人:可点击=真/>

< /android.support.design.widget.CoordinatorLayout>
 

With creation of NestedScrollView you can put scrolling view inside another scrolling view as long as those implement NestedScrollingChild and NestedScrollingParent correctly.

(This is not bad design pattern "Ian Lake (from Google) actually recommends putting a RecyclerView inside a nestedscrollview here: plus.google.com/u/0/+AndroidDevelopers/posts/9kZ3SsXdT2T")

I want to put RecyclerView inside NestedScrollView and fortunately RecyclerView implements NestedScrollingChild so you can put it inside NestedScrollView.

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild

I have read these posts:

How to use RecyclerView inside NestedScrollView?

NestedScrolling with NestedScrollView, RecyclerView (Horizontal), inside a CoordinatorLayout

But the problem with most voted solution is, it calls all of the items of RecyclerView so for example if it is an endless RecyclerView and when the user reaches the end of the list you want to make a network request then with that solution the RecyclerView calls server repeatedly because it automatically reaches the last item of RecyclerView.

Anyway, how to set parameter so I can put RecyclerView inside NestedScrollView.(actually I want to put a viewgroup like framelayout or relativelayout as a single childe of nestedscrollview and then I want to put recyclerview inside framelayout or relativelayout)

When I put RecyclerView inside NestedScrollView there is nothing to display.

In order to create a sample project you can use cheesesquare and change the CheeseDetailActivity to have a RecyclerView.

Although the answer of BNK is not correct but BNK has tried a lot. So I award him the bounty. Still looking for nice solution....

解决方案

So put RecyclerView inside NestedScrollView directly will unfortunately display nothing. However, there is a way to put the recyclerview inside the NestedScrollView indirectly - just use a frameLayout as the third party to hold your recyclerview.

This is the framelayout which holds the nested recyclerview in your activity class:

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".ExampleFragment"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            >
        </android.support.v4.widget.NestedScrollView>
   </FrameLayout>

Put your fragment into the framelayout (code sits within the activity class):

 ExampleFragment exampleFragment = new ExampleFragment();

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.container, exampleFragment);
    ft.commit();

In your exampleFragment, you can then put your recyclerview.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/post_container"
        android:background="#E0E0E0">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"

                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </android.support.v7.widget.RecyclerView>


    </RelativeLayout>

This is the fragment code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
            llLayout = (RelativeLayout) inflater.inflate(R.layout.example_fragment_layout, container, false);
                    mRecyclerView = (RecyclerView) llLayout.findViewById(R.id.my_recycler_view);

The following is the CheeseSquare XML layout you should have:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

   <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".ExampleFragment"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            >
        </android.support.v4.widget.NestedScrollView>
   </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

</android.support.design.widget.CoordinatorLayout>