如何在ExpandableListView中创建两个或三个textview?
我正在为Android创建一个程序,在该应用中,我将ExpandableListview与BaseExpandableListViewAdapter一起使用,默认情况下此适配器使用一个TextView,我想在子listItem上创建一些(两个或三个)TextView,请帮助我提供代码,在活动中写些什么?
I'm creating a program for android, in the app, I used ExpandableListview with BaseExpandableListViewAdapter, on default this adapter uses one TextView, I want to create some (two or three) TextViews on child listItem, please help me with code, what do write in the activity?
XML布局listitem子文件:
XML layout listitem child file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/lblListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textSize="15dip" />
<TextView
android:id="@+id/lblListItem2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textSize="15dip" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
适配器的某些代码:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, 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;
}
此时准备数据列表:
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = 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> 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));
listDataChild.put(listDataHeader.get(0), Header1);
listDataChild.put(listDataHeader.get(1), Header2);
}
设置适配器:
private ExpandableListAdapter listAdapter;
private ExpandableListView expListView;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 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, listDataChild);
expListView.setAdapter(listAdapter);
return cpf;
}
最简单的方法是使适配器接收更多列表.
The simplest way is to make the adapter takes more lists.
因此,像这样准备数据列表和setAdapter:
So prepare the datalists and setAdapter like this:
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:
and modify the adapter constructor and getChildView like this:
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受主列表的大小限制.
You can do something similar to make the third list. One very important thing to remember is that the size of the second/third lists should never be smaller than the primary list, otherswise it causes INDEX_OUT_OF_BOUND error [listDataChild2.get(getGroup(groupPosition)).get(childPosition) where childPosition is bounded by the size of primary list].
对于我在上面的注释中提到的链接,使用POJO类代替List和HashMaps.那可能是一个更好的方法.
For the link that I memtioned in the above comment, POJO class is used instead of List and HashMaps. That may be a better approach.
希望有帮助!