一个布局中如何垂直放置两个RecyclerView?

40

如何在一个布局中垂直放置两个RecyclerView?我不想使用一个单独的RecyclerView来显示所有项目。

我的代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/main__item_background"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
    android:text="@string/find_friends__already_playing"
    android:background="@color/header"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_header"
    android:visibility="visible"/>

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

<TextView
    android:text="@string/find_friends__invite_friends"
    android:background="@color/find_friends__header"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_header" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/friends_to_invite"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />
</LinearLayout>
7个回答

42

我已经自己找到了答案。

你需要将LinearLayout放入一个ScrollView中,并使用wrap_content作为RecyclerView的layout_height

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_header"
        android:background="@color/header"
        android:gravity="center"
        android:text="@string/find_friends__already_playing"
        android:visibility="visible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/in_app_friends"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_header"
        android:background="@color/find_friends__header"
        android:gravity="center"
        android:text="@string/find_friends__invite_friends" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/friends_to_invite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"/>
</LinearLayout>
</ScrollView>

同时,RecyclerView存在一个与wrap_content相关的bug,因此您需要使用自定义布局管理器。请查看此帖子:如何使RecyclerView中的WRAP_CONTENT起作用


10
你只需要使用一个RecyclerView,但是需要使用不同的item类型。 RecyclerView.Adapter有一个getItemViewType(int position)方法,该方法将传递给onCreateViewHolder(ViewGroup parent, int itemtype),在该方法中可以根据类型切换并创建正确的RecyclerView.ViewHolder - Charles Durham
7
当我需要在RecyclerView中使用不同的模型时,我该怎么办? - alan_derua
2
wrap_content在最新的Android支持库中已被修复,无需编写自定义布局管理器。 - André Delgado
5
为了实现流畅的滚动,最好在每个Recycler View上设置recyclerView.setNestedScrollingEnabled(false)。 - Presen
2
最佳解决方案是使用嵌套的ScrollView。 - SAndroidD
显示剩余2条评论

36

您应该创建一个像这样的XML布局文件

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

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

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/steps_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

并且在代码中,您应该调用setNestedScrollingEnabled(false)

RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
RecyclerView stepsList = findViewById(R.id.steps_list);

ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);

我很感激,我整天都被卡住了。 - Ayaz Muhammad
ingredientsList.setNestedScrollingEnabled(false); stepsList.setNestedScrollingEnabled(false)。工作正常。 - Ayaz Muhammad

9
我也曾遇到相同的问题,因此编写了一个库,通过连接适配器和布局来帮助解决这个问题。
尝试使用Gradle依赖项(需要jcenter存储库):
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now

将xml更改为使用与父级匹配的单个RecyclerView:
<android.support.v7.widget.RecyclerView
    android:id="@+id/joined_friends_rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:background="@color/white"/>

然后在代码中像这样初始化RecyclerView:

    //init your RecyclerView as usual
    RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    //construct a joiner
    RvJoiner rvJoiner = new RvJoiner();
    rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
    rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
    rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
    rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));

    //set join adapter to your RecyclerView
    rv.setAdapter(rvJoiner.getAdapter());

您可以查看此链接以获取更多库的详细信息和说明。希望能对您有所帮助。

通过 build.gradle 添加此库会在同步时创建错误。 - Reaz Murshed
嘿@ReazMurshed,你可以帮我看看这个问题吗?https://dev59.com/oqrka4cB1Zd3GeqPjMuw?noredirect=1#comment87836903_49952965 - user9550968

5
如果底部的RecyclerView无法与主内容一起滚动,请将LinearLayout(请参见alan_derua的答案)更改为ConstraintLayout,并将两个RecyclerView包装在内。请查看以下代码:
<ScrollView 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"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:id="@+id/task_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/first_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:gravity="left"
        android:paddingTop="0dp"
        android:text="@string/my_tasks"
        app:layout_constraintBottom_toTopOf="@+id/second_list_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/first_list_view" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/second_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>
</ScrollView>

这对我很有帮助!


3
你可以将每个RecycleView的高度设置为0dp,权重设置为1:
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"

3
我不希望它们两个都占据屏幕的一半。我希望它们能够一个接一个地滚动。 - alan_derua

1

使用NestedScrollView作为父布局,它应该有

android:weightSum="2"

并提供

android:layout_weight="1"

对于您的每个RecyclerView,它们应该一个接一个地滚动。


0

只需使用:

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:scrollbars="vertical"
    android:layout_below="@+id/your_first_recycler"/>

最后一行是为了解决你的问题。使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接