首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在一个布局中彼此下的两个RecyclerViews

在一个布局中彼此下的两个RecyclerViews
EN

Stack Overflow用户
提问于 2015-05-06 03:43:37
回答 6查看 47.9K关注 0票数 37

如何在一个布局中将两个RecyclerViews放在彼此下面?我不希望所有的项目都有一个RecyclerView。我的代码:

代码语言:javascript
复制
<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>
EN

回答 6

Stack Overflow用户

发布于 2018-04-15 20:55:18

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

代码语言:javascript
复制
<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)

代码语言:javascript
复制
RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
RecyclerView stepsList = findViewById(R.id.steps_list);

ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);
票数 31
EN

Stack Overflow用户

发布于 2016-01-17 17:27:47

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

Gradle依赖来试用(需要jcenter repo):

代码语言:javascript
复制
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now

将xml更改为使用与父元素匹配的单个RecyclerView:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
    //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());

您可以查看this link了解更多库的详细信息和解释。希望能有所帮助。

票数 9
EN

Stack Overflow用户

发布于 2017-11-22 19:41:20

如果底部的回收视图不能滚动显示主要内容,请将LinearLayout (参见alan_derua的答案)更改为ConstraintLayout,并将两个RecyclerViews包含在内。请看下面的代码:

代码语言:javascript
复制
<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>

这对我很有效!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30061897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档