如何在 ExpandableListView 中制作两个或三个文本视图?

我正在为 android 创建一个程序,在应用程序中,我使用 ExpandableListview 和 BaseExpandableListViewAdapter,默认情况下此适配器使用一个 TextView,我想在子 listItem 上创建一些(两个或三个)TextView,请帮助我编写代码,该怎么办写在活动中?


XML 布局列表项子文件:


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

        }


心有法竹
浏览 62回答 1
1回答

慕田峪9158850

最简单的方法是让适配器占用更多列表。因此,像这样准备 datalists 和 setAdapter:private ExpandableListAdapter listAdapter;private ExpandableListView expListView;private List<String> listDataHeader;private HashMap<String, List<String>> listDataChild1, listDataChild2;@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; View cpf = inflater.inflate(R.layout.checklist_bstart, container, false);&nbsp; &nbsp; expListView = cpf.findViewById(R.id.lv_bstart);&nbsp; &nbsp; prepareListData();&nbsp; &nbsp; listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild1, listDataChild2);&nbsp; &nbsp; expListView.setAdapter(listAdapter);&nbsp; &nbsp; return cpf;}private void prepareListData() {&nbsp; &nbsp; listDataHeader = new ArrayList<>();&nbsp; &nbsp; listDataChild1 = new HashMap<>();&nbsp; &nbsp; listDataHeader.add(getResources().getString(R.string.bstart_ver));&nbsp; &nbsp; listDataHeader.add(getResources().getString(R.string.bstart_op));&nbsp; &nbsp; List<String> Header1 = new ArrayList<>();&nbsp; &nbsp; Header1.add(getResources().getString(R.string.bstart_string1));&nbsp; &nbsp; Header1.add(getResources().getString(R.string.bstart_string2));&nbsp; &nbsp; Header1.add(getResources().getString(R.string.bstart_string3));&nbsp; &nbsp; List<String> Header12 = new ArrayList<>();&nbsp; &nbsp; Header12.add(getResources().getString(R.string.bstart_string4));&nbsp; &nbsp; Header12.add(getResources().getString(R.string.bstart_string5));&nbsp; &nbsp; Header12.add(getResources().getString(R.string.bstart_string6));&nbsp; &nbsp; List<String> Header2 = new ArrayList<>();&nbsp; &nbsp; Header2.add(getResources().getString(R.string.bstart_string4));&nbsp; &nbsp; Header2.add(getResources().getString(R.string.bstart_string5));&nbsp; &nbsp; Header2.add(getResources().getString(R.string.bstart_string6));&nbsp; &nbsp; Header2.add(getResources().getString(R.string.bstart_string7));&nbsp; &nbsp; Header2.add(getResources().getString(R.string.bstart_string8));&nbsp; &nbsp; List<String> Header22 = new ArrayList<>();&nbsp; &nbsp; Header22.add(getResources().getString(R.string.bstart_string1));&nbsp; &nbsp; Header22.add(getResources().getString(R.string.bstart_string2));&nbsp; &nbsp; Header22.add(getResources().getString(R.string.bstart_string3));&nbsp; &nbsp; Header22.add(getResources().getString(R.string.bstart_string7));&nbsp; &nbsp; Header22.add(getResources().getString(R.string.bstart_string8));&nbsp; &nbsp; listDataChild1.put(listDataHeader.get(0), Header1);&nbsp; &nbsp; listDataChild1.put(listDataHeader.get(1), Header2);&nbsp; &nbsp; listDataChild2.put(listDataHeader.get(0), Header1);&nbsp; &nbsp; 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,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HashMap<String, List<String>> listDataChild2) {&nbsp; &nbsp; _context = context;&nbsp; &nbsp; this.listDataHeader = listDataHeader;&nbsp; &nbsp; this.listDataChild1 = listDataChild1;&nbsp; &nbsp; this.listDataChild2 = listDataChild2;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {&nbsp; &nbsp; String childText = (String) getChild(groupPosition, childPosition);&nbsp; &nbsp; String childText2 = listDataChild2.get(getGroup(groupPosition)).get(childPosition);&nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater infalInflater = (LayoutInflater) this._context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getSystemService(Context.LAYOUT_INFLATER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; convertView = infalInflater.inflate(R.layout.list_item, null);&nbsp; &nbsp; }&nbsp; &nbsp; TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);&nbsp; &nbsp; txtListChild.setText(childText);&nbsp; &nbsp; TextView txtListChild2 = (TextView) convertView.findViewById(R.id.lblListItem2);&nbsp; &nbsp; txtListChild2.setText(childText2);&nbsp; &nbsp; return convertView;}您可以执行类似的操作来制作第三个列表。要记住的一件非常重要的事情是,第二个/第三个列表的大小决不能小于主列表,否则会导致 INDEX_OUT_OF_BOUND 错误 [listDataChild2.get(getGroup(groupPosition)).get(childPosition) 其中 childPosition 的边界为主列表的大小]。对于我在上面评论中提到的链接,使用 POJO 类而不是 List 和 HashMap。这可能是一个更好的方法。希望有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java