首页 > 解决方案 > 显示具有相同类型适配器的多个 RecyclerView

问题描述

我试图在同一个片段中显示 3 个 RecyclerView,每个 RecyclerView 显示不同类型的匹配(已完成匹配、本周匹配和尚未匹配)。

我已经实例化了 3 个 RV 和 3 个适配器,如下面的代码所示:

matchesList_rv_finished = view.findViewById(R.id.finished_upcoming_matches_rv);
    matchesList_rv_finished.setHasFixedSize(true);
    LinearLayoutManager layoutManager1 = new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false);
    matchesList_rv_finished.setLayoutManager(layoutManager1);

    matchesList_rv_Week = view.findViewById(R.id.Week_upcoming_matches_rv);
    matchesList_rv_Week.setHasFixedSize(true);
    LinearLayoutManager layoutManager2 = new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false);
    matchesList_rv_Week.setLayoutManager(layoutManager2);

    matchesList_rv_NotYet = view.findViewById(R.id.NotYet_upcoming_matches_rv);
    matchesList_rv_NotYet.setHasFixedSize(true);
    LinearLayoutManager layoutManager3 = new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false);
    matchesList_rv_NotYet.setLayoutManager(layoutManager3);

    matchAdapter1 = new MatchAdapter();
    matchAdapter2 = new MatchAdapter();
    matchAdapter3 = new MatchAdapter();

这就是我接收 3 个匹配列表并设置 3 个适配器的方式:

Model.instance.getUpComingMatches(new Model.MatchListener() {
        @Override
        public void onComplete(ArrayList<Match> finishedMatches,
                               ArrayList<Match> thisWeekMatches,
                               ArrayList<Match> notYetMatches) {
            finishedMatchList = finishedMatches;
            matchAdapter1.setMatchesData(finishedMatchList);
            matchesList_rv_finished.setAdapter(matchAdapter1);
            
            ThisWeekMatchList = thisWeekMatches;
            matchAdapter2.setMatchesData(ThisWeekMatchList);
            matchesList_rv_Week.setAdapter(matchAdapter2);
            
            NotYetMatchList = notYetMatches;
            matchAdapter3.setMatchesData(NotYetMatchList);
            matchesList_rv_NotYet.setAdapter(matchAdapter3);
            
        }
    });

我不确定我做错了什么,但似乎最后一个适配器覆盖了前 2 个适配器的数据,而不是在每个 RV 中显示 3 个不同的匹配项:

我希望它显示 3 个不同的匹配项

我想我在这里缺少一些基本信息,或者也许只有一种更简单的方法来实现这一点?谢谢您的帮助!

编辑:发布片段 xml 布局:在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/upcoming_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UpcomingMatchesFragment" >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/Week_upcoming_matches_rv"
        android:layout_width="382dp"
        android:layout_height="167dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/finished_upcoming_matches_rv"
        android:layout_width="382dp"
        android:layout_height="167dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/NotYet_upcoming_matches_rv" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/NotYet_upcoming_matches_rv"
        android:layout_width="382dp"
        android:layout_height="167dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Week_upcoming_matches_rv" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroidandroid-studioandroid-fragmentsandroid-recyclerview

解决方案


这项技术已经存在于 Sport365 和 onefootball 等应用程序中。我有同样的问题


推荐阅读