【问题标题】:Displaying Multiple RecyclerView with same type Adapters显示具有相同类型适配器的多个 RecyclerView
【发布时间】:2021-07-16 01:42:59
【问题描述】:

我试图在同一个 Fragment 中显示 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 个不同的匹配项:

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

编辑:发布片段 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>

【问题讨论】:

  • 你也可以发布你的布局吗?
  • @Jameido 如果您的意思是片段布局,那么是的,只是发布了它。
  • 这种情况下xml代码更重要
  • @Jameido 发布
  • 检查从您的 RecyclerViews 中删除 .setHasFixedSize(true); 是否显示缺失的列表?

标签: java android android-studio android-fragments android-recyclerview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多