两个RecyclerViews在一个布局中相互映射

时间:2021-02-20 14:59:40

How can I get two RecyclerViews under each other in one layout? I don't want to have a single RecyclerView for all items. My code:

如何在一个布局中相互获得两个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 个解决方案

#1


I've found the answer myself.

我自己找到了答案。

You need to put the LinearLayout into a ScrollView and use wrap_content as RecyclerView's layout_height.

您需要将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>

Also there is a bug with with RecyclerView and wrap_content so you have to use a custom layout manager. Check out this post: How do I make WRAP_CONTENT work on a RecyclerView

还有一个与RecyclerView和wrap_content的错误,所以你必须使用自定义布局管理器。看看这篇文章:如何让WRAP_CONTENT在RecyclerView上运行

#2


You should create an XML layout file like this

您应该创建一个这样的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>

And in the code, you should call setNestedScrollingEnabled(false)

在代码中,你应该调用setNestedScrollingEnabled(false)

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

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

#3


I also had the same problem and wrote a library which helps to achieve this by joining adapters and layouts.

我也有同样的问题,并编写了一个库,通过连接适配器和布局来帮助实现这一目标。

Gradle dependency to try it (needs jcenter repo):

Gradle依赖尝试它(需要jcenter repo):

compile 'su.j2e:rv-joiner:1.0.3'//latest version by now

Thea change xml to use a single RecyclerView which matches parent:

Thea更改xml以使用与parent匹配的单个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"/>

Then init RecyclerView in code like this:

然后在代码中初始化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());

You can check this link for more library details and explanation. Hope it helps.

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

#4


if you get the bottom recyclerview not scrolling with the main content, change the LinearLayout (see answer from alan_derua) to ConstraintLayout and wrap the two RecyclerViews inside. See code below:

如果底部的recyclelerview没有滚动主内容,请将LinearLayout(请参阅alan_derua的答案)更改为ConstraintLayout并将两个RecyclerViews包装在里面。见下面的代码:

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

This worked for me!

这对我有用!

#5


You can give each RecycleView height equal to 0dp and weight equal 1:

您可以为每个RecycleView高度等于0dp,权重等于1:

android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"

#6


Use NestedScrollView as parent layout, it should have

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

android:weightSum="2"

and give

android:layout_weight="1"

to each RecyclerView of yours.It should be scrolled one after each other.

到你的每个RecyclerView。它应该一个接一个地滚动。

#7


Just use:

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

last line is for your problem.use it.

最后一行是你的问题。使用它。

#1


I've found the answer myself.

我自己找到了答案。

You need to put the LinearLayout into a ScrollView and use wrap_content as RecyclerView's layout_height.

您需要将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>

Also there is a bug with with RecyclerView and wrap_content so you have to use a custom layout manager. Check out this post: How do I make WRAP_CONTENT work on a RecyclerView

还有一个与RecyclerView和wrap_content的错误,所以你必须使用自定义布局管理器。看看这篇文章:如何让WRAP_CONTENT在RecyclerView上运行

#2


You should create an XML layout file like this

您应该创建一个这样的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>

And in the code, you should call setNestedScrollingEnabled(false)

在代码中,你应该调用setNestedScrollingEnabled(false)

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

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

#3


I also had the same problem and wrote a library which helps to achieve this by joining adapters and layouts.

我也有同样的问题,并编写了一个库,通过连接适配器和布局来帮助实现这一目标。

Gradle dependency to try it (needs jcenter repo):

Gradle依赖尝试它(需要jcenter repo):

compile 'su.j2e:rv-joiner:1.0.3'//latest version by now

Thea change xml to use a single RecyclerView which matches parent:

Thea更改xml以使用与parent匹配的单个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"/>

Then init RecyclerView in code like this:

然后在代码中初始化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());

You can check this link for more library details and explanation. Hope it helps.

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

#4


if you get the bottom recyclerview not scrolling with the main content, change the LinearLayout (see answer from alan_derua) to ConstraintLayout and wrap the two RecyclerViews inside. See code below:

如果底部的recyclelerview没有滚动主内容,请将LinearLayout(请参阅alan_derua的答案)更改为ConstraintLayout并将两个RecyclerViews包装在里面。见下面的代码:

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

This worked for me!

这对我有用!

#5


You can give each RecycleView height equal to 0dp and weight equal 1:

您可以为每个RecycleView高度等于0dp,权重等于1:

android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"

#6


Use NestedScrollView as parent layout, it should have

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

android:weightSum="2"

and give

android:layout_weight="1"

to each RecyclerView of yours.It should be scrolled one after each other.

到你的每个RecyclerView。它应该一个接一个地滚动。

#7


Just use:

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

last line is for your problem.use it.

最后一行是你的问题。使用它。