0

我正在做一个项目,我需要 Expandable RecyclerView。我做了所有事情,但是每个“父母”的“孩子”(行)都有一些问题。

这是我的“父母”代码。回收视图上的第一项将是“选项 1”,第二项是“选项 2”,依此类推。

 public TitleCreator(Context context) {
    _titleParents = new ArrayList<>();


        TitleParent title = new TitleParent(String.format("Option 1"));
        _titleParents.add(title);
        TitleParent title1 = new TitleParent(String.format("Option 2"));
        _titleParents.add(title1);
        TitleParent title2 = new TitleParent(String.format("Option 3"));
        _titleParents.add(title2);
        TitleParent title3 = new TitleParent(String.format("Option 4"));
        _titleParents.add(title3);
        TitleParent title4 = new TitleParent(String.format("Option 5"));
        _titleParents.add(title4);

}

它工作得很好,但是当我想为每个父母的孩子做同样的事情时,我遇到了问题。最后,我在 MainActivity 中编写了一些代码,以使每个父母的每个孩子都相同。这是代码:

 private List<ParentObject> initData() {
    TitleCreator titleCreator = TitleCreator.get(this);
    List<TitleParent> titles = titleCreator.getAll();
    List<ParentObject> parentObject = new ArrayList<>();
    for(TitleParent title:titles)
    {
        List<Object> childList = new ArrayList<>();
        childList.add(new TitleChild("Combe","Send message"));
        title.setChildObjectList(childList);
        parentObject.add(title);
    }
    return parentObject;

}

我需要为每个父母提供不同的孩子/行,该怎么做?

4

1 回答 1

0

每个父母都有不同的孩子/行

public TitleCreator(Context context) {
    _titleParents = new ArrayList<>();

    TitleParent title = new TitleParent(String.format("Option 1"));
    _titleParents.add(title);
    List<Object> childList = new ArrayList<>();
    childList.add(new TitleChild("Combe1","Send message"));
    title.setChildObjectList(childList);

    TitleParent title1 = new TitleParent(String.format("Option 2"));
    _titleParents.add(title1);
    childList = new ArrayList<>();
    childList.add(new TitleChild("Combe2","Send message"));
    title1.setChildObjectList(childList);

    TitleParent title2 = new TitleParent(String.format("Option 3"));
    _titleParents.add(title2);
    childList = new ArrayList<>();
    childList.add(new TitleChild("Combe3","Send message"));
    title2.setChildObjectList(childList);

    TitleParent title3 = new TitleParent(String.format("Option 4"));
    _titleParents.add(title3);
    childList = new ArrayList<>();
    childList.add(new TitleChild("Combe4","Send message"));
    title3.setChildObjectList(childList);

    TitleParent title4 = new TitleParent(String.format("Option 5"));
    _titleParents.add(title4);
    childList = new ArrayList<>();
    childList.add(new TitleChild("Combe5","Send message"));
    title4.setChildObjectList(childList);
}
于 2018-05-13T14:11:13.240 回答