我正在做项目,我需要可扩展的回收器视图。我做了一切,但每个"父母"的"孩子"(行(都有一些问题。
这是我对"父母"的代码。回收视图上的第一项将是"选项 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;
}
我需要为每个父母提供不同的子/行,该怎么做?
每个父级的不同子项/行
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);
}