ExpandableListView二级列表

MainActivity(xml文件):

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    tools:context="com.guo.da2_2jliebiao.MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


LinearLayout>

一级列表(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="match_parent">

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="group text"
        />


LinearLayout>

二级列表(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="match_parent">


    <ImageView
        android:id="@+id/iv_child"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/ic_launcher" />
    <TextView
        android:id="@+id/tv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="item text" />


LinearLayout>


MainActivity(主代码):

package com.guo.da2_2jliebiao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView listView;

    //Model:定义的数据
    private String[] groups = {"一级列表A", "一级列表B", "一级列表C"};
    //注意,字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}*/
    private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initDate();

    }

    private void initDate() {
        listView = findViewById(R.id.expandableListView);
        listView.setAdapter(new MyExpandableListView());

    }

    class MyExpandableListView extends BaseExpandableListAdapter {


        //返回一级列表的个数
        @Override
        public int getGroupCount() {
            return groups.length;
        }

        //返回每个二级列表的个数
        //参数i表示第几个一级列表
        @Override
        public int getChildrenCount(int i) {
            return childs[i].length;
        }

        //返回一级列表的单个item(返回的是对象)
        @Override
        public Object getGroup(int i) {
            return groups[i];
        }

        //返回二级列表中的单个item(返回的是对象)
        @Override
        public Object getChild(int i, int i1) {
            return childs[i][i1];
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        //每个item的id是否是固定?一般为true
        @Override
        public boolean hasStableIds() {
            return true;
        }

        //【重要】填充一级列表
        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.item_group, null);
            }
            TextView textView = view.findViewById(R.id.tv_group);
            textView.setText(groups[i]);
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(MainActivity.this,R.layout.item_child,null);
            }
            ImageView imageView  = view.findViewById(R.id.iv_child);
            imageView.setImageResource(R.drawable.ic_launcher_background);
            TextView textView = view.findViewById(R.id.tv_child);
            textView.setText(childs[i][i1]);
            return view;
        }

        //二级列表中的item是否能够被选中?可以改为true
        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }


}

你可能感兴趣的:(Andriod开发)