How made two or three textviews in ExpandableListView?

浪尽此生 提交于 2021-01-29 08:32:51

问题


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 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>

Some code from adapter:

@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;
    }

Prepare datalists at this moment:

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);

    }

Set adapter:

    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;
    }

回答1:


The simplest way is to make the adapter takes more lists.

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);
}

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;
}

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].

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.

Hope that helps!



来源:https://stackoverflow.com/questions/58228561/how-made-two-or-three-textviews-in-expandablelistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!