如何在Android中使用同一RecyclerView填充具有相同字段或属性的不同对象

9

我将要开发足球应用程序,并且拥有以下json响应。(链接

我有两个不同名称的类,但具有相同属性或字段,并希望显示一个相同的单一RecyclerView

在这里,我拥有worldmatch和world,它们具有与matchTimestartTimeendTime相同的字段。我使用jsontopojo插件创建了pojo类。

这里是主要内容,我想在位置0显示worldmatch,其余位置基于俱乐部的world。您可以在图片中看到更多细节。

输入图像描述

输入图像描述

这必须是第一个选项卡(world),类似于其他选项卡(EuropeAsia),具有相应的类似模式。

选项卡1(世界)

---------------position 0 (All match)------------------
|
| 930   1100   1130  and so on ( horizontal recyclerview)
|
-------------------------------------------
---------------position 1(Barcelona)------------------
|
| 1130   1230   1330   1430  and so on (  horizontal recyclerview)
|
-------------------------------------------
---------------position 2(Chelsea)------------------
|
| 1300   1400   1500  and so on (  horizontal recyclerview)
|
-------------------------------------------
                     .
                     .
                     .
                   so on
        (vertical recyclerview)

详细解释图片视图:

输入图片描述

我有两个RecyclerView适配器,第一个显示clubname并将相应的数据传递给其他水平视图显示相应的matchTime的recyclerview适配器。

在外部RecyclerView位置0填充worldmatch数据,并反映所有其他数据。如何在同一RecyclerView中传递填充的worldmatchworld数据,以及如何过滤所有选项卡。

仅从位置0的WorldMatch列表和下面0的World列表(水平RecyclerView)中显示matchTime字段。

任何人有任何想法都对我非常有帮助,谢谢! 提前致谢。


soccer里的数组是静态的吗? - Santanu Sur
不,它是动态的。 - Horrorgoogle
1
这很复杂,JSON 数据本可以以更好的方式形成。 - Santanu Sur
是的,但不幸的是,这是最终的回复。 - Horrorgoogle
我知道如何在RecyclerView上显示具有共同匹配时间属性的WorldMatch列表和World列表。 - Horrorgoogle
所以,你有3个标签在顶部。根据所选的标签,您将更改垂直回收站的第一项?所以,如果选择了亚洲,垂直回收站的第一项将显示与亚洲匹配的横向回收站中的项目? - denvercoder9
4个回答

4
我建议您创建一个TabData类,用于每个选项卡。
public class TabData {
    private List<TimeData> regionTimes;
    private List<Pair<String, List<TimeData>>> clubTimes = new ArrayList<>();

    public void setRegionTimes(List<TimeData> list) {
        this.regionTimes = list;
    }

    public void putClubTimes(String clubName, List<TimeData> times) {
        clubTimes.add(new Pair<>(clubName, times);
    }

    // Getters
}

这将是TimeData类。
public class TimeData {
    int matchTime;
    String startTime;
    String endTime;
}

例如,要显示第一个标签(世界标签),您需要执行以下操作:

TabData worldTabData = new TabData();
worldTabData.setRegionTimes(jsonData.getSoccer().getWorldMatch());

for (ClubData clubData : jsonData.getSoccer().getClubs()) {
    String clubName = clubData.getClubProfile().getClubName();
    List<TimeData> times = clubData.getWorld();
    worldTabData.putClubTimes(clubName, times);
}

regionAdapter = new RegionAdapter(worldTabData);

这是外部RecyclerView(垂直)的适配器:
public class RegionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private TabData data;

    public RegionAdapter(TabData data) {
        this.data = data;
    }

    public int getItemCount() {
        return 1 + data.getClubTimes().size(); // One row per club plus another one for position 0
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            // Set adapter for horizontal recyclerview with data.getRegionTimes()
        } else {
            Pair<String, List<TimeData>> clubTime = data.getClubTimes(position - 1);
            // Set textview with club.first
            // Set adapter for horizontal recyclerview with clubTime.second
        }
    }
}

希望你能理解这个想法!请告诉我。
谢谢。

3
您可以将worldMatch作为垂直RecyclerView的标题。在您的情况下,您不需要为RecyclerView使用两个不同的适配器。只需在RecyclerView中添加带有worldMatch类的标题即可。
如果您想了解如何在RecyclerView中添加标题或页脚,可以查看这里的答案
一旦您完成将worldMatch作为垂直RecyclerView的标题,您将在RecyclerView的适配器中传递world数据并相应地显示它们。
如果您有任何疑问,请告诉我。

3

这是可以实现的。现在您可以在响应解析时间或设置适配器时间将值设置为另一个类。我也是这样做的。 例如:

   setmatches(List<club.wordlWild> ww){
   List<WorldWild> list = new ArrayList<>();
   for(club.worlWide cw : ww){
     WorldWild w = new WorldWild ();
     w.setMatchTime(cw.getMatchtimie);
     ..
     ...// set values
    list.add(w);
    }
   }

现在您可以将club.worldwide的值添加到WorldWild中。如果您想要相反的变化,请执行相同的操作。

谢谢,但我还是不明白你的意思。 - Horrorgoogle

2

我曾通过一个父类在适配器中使用相似的方法,但在你的情况下,可能可以为每个匹配项使用同一类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接