1 回答
TA贡献1794条经验 获得超7个赞
最简单的方法是让适配器占用更多列表。
因此,像这样准备 datalists 和 setAdapter:
private ExpandableListAdapter listAdapter;
private ExpandableListView expListView;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild1, listDataChild2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View cpf = inflater.inflate(R.layout.checklist_bstart, container, false);
expListView = cpf.findViewById(R.id.lv_bstart);
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild1, listDataChild2);
expListView.setAdapter(listAdapter);
return cpf;
}
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild1 = new HashMap<>();
listDataHeader.add(getResources().getString(R.string.bstart_ver));
listDataHeader.add(getResources().getString(R.string.bstart_op));
List<String> Header1 = new ArrayList<>();
Header1.add(getResources().getString(R.string.bstart_string1));
Header1.add(getResources().getString(R.string.bstart_string2));
Header1.add(getResources().getString(R.string.bstart_string3));
List<String> Header12 = new ArrayList<>();
Header12.add(getResources().getString(R.string.bstart_string4));
Header12.add(getResources().getString(R.string.bstart_string5));
Header12.add(getResources().getString(R.string.bstart_string6));
List<String> Header2 = new ArrayList<>();
Header2.add(getResources().getString(R.string.bstart_string4));
Header2.add(getResources().getString(R.string.bstart_string5));
Header2.add(getResources().getString(R.string.bstart_string6));
Header2.add(getResources().getString(R.string.bstart_string7));
Header2.add(getResources().getString(R.string.bstart_string8));
List<String> Header22 = new ArrayList<>();
Header22.add(getResources().getString(R.string.bstart_string1));
Header22.add(getResources().getString(R.string.bstart_string2));
Header22.add(getResources().getString(R.string.bstart_string3));
Header22.add(getResources().getString(R.string.bstart_string7));
Header22.add(getResources().getString(R.string.bstart_string8));
listDataChild1.put(listDataHeader.get(0), Header1);
listDataChild1.put(listDataHeader.get(1), Header2);
listDataChild2.put(listDataHeader.get(0), Header1);
listDataChild2.put(listDataHeader.get(1), Header2);
}
并像这样修改适配器构造函数和 getChildView :
Context _context;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild1, listDataChild2;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild1,
HashMap<String, List<String>> listDataChild2) {
_context = context;
this.listDataHeader = listDataHeader;
this.listDataChild1 = listDataChild1;
this.listDataChild2 = listDataChild2;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
String childText = (String) getChild(groupPosition, childPosition);
String childText2 = listDataChild2.get(getGroup(groupPosition)).get(childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
TextView txtListChild2 = (TextView) convertView.findViewById(R.id.lblListItem2);
txtListChild2.setText(childText2);
return convertView;
}
您可以执行类似的操作来制作第三个列表。要记住的一件非常重要的事情是,第二个/第三个列表的大小决不能小于主列表,否则会导致 INDEX_OUT_OF_BOUND 错误 [listDataChild2.get(getGroup(groupPosition)).get(childPosition) 其中 childPosition 的边界为主列表的大小]。
对于我在上面评论中提到的链接,使用 POJO 类而不是 List 和 HashMap。这可能是一个更好的方法。
希望有帮助!
添加回答
举报