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

ala*_*rua 30 android android-recyclerview

如何在一个布局中相互获得两个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>
Run Code Online (Sandbox Code Playgroud)

ala*_*rua 30

我自己找到了答案.

您需要将LinearLayout放入ScrollViewwrap_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>
Run Code Online (Sandbox Code Playgroud)

还有与RecyclerView有关的错误,wrap_content因此您必须使用自定义布局管理器.看看这篇文章:如何让WRAP_CONTENT在RecyclerView上运行

  • 您应该只使用一个具有不同项目视图类型的`RecyclerView`.`RecyclerView.Adapter`有一个`getItemViewType(int position)`,它将被传递到`onCreateViewHolder(ViewGroup parent,int itemtype)`,在那里你可以打开类型并创建正确的`RecyclerView.ViewHolder`. (9认同)
  • 当我必须在recyclerviews中使用不同的模型时,我该怎么办? (7认同)
  • 为了平滑滚动,最好在每个回收站视图上设置recyclerView.setNestedScrollingEnabled(false) (4认同)
  • `wrap_content` 现在**固定**在最新的 Android 支持库中**无需**编写自定义布局管理器 (2认同)
  • 最好的解决方案是使用嵌套的scrollview (2认同)

小智 20

您应该这样创建一个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>
Run Code Online (Sandbox Code Playgroud)

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

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

ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)


j2e*_*esu 9

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

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

compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

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

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


Mik*_*iki 5

如果底部 recyclerview 不随主要内容滚动,请将 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>
Run Code Online (Sandbox Code Playgroud)

这对我有用!