当前位置: 代码迷 >> 综合 >> ExpandableListView 二级列表
  详细解决方案

ExpandableListView 二级列表

热度:57   发布时间:2023-12-29 10:35:38.0

先看效果:

代码:

MainActivity:

package com.example.administrator.expland;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private ExpandableListView expandListview;private String[] groups = {
  "A", "B", "C", "D", "E"};//注意,字符数组不要写成{
   {"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"}, {
  "A1", "A2", "A3", "C4"}, {
  "A1", "A2", "A3", "C4"}};@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);expandListview = (ExpandableListView) findViewById(R.id.expandListview);expandListview.setAdapter(new MyExpandableListView());}//为ExpandableListView自定义适配器
    class MyExpandableListView extends BaseExpandableListAdapter {//返回一级列表的个数
        @Override
        public int getGroupCount() {return groups.length;}//返回每个二级列表的个数
        @Override
        public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
            return childs[groupPosition].length;}//返回一级列表的单个item(返回的是对象)
        @Override
        public Object getGroup(int groupPosition) {return groups[groupPosition];}@Override
        public Object getChild(int groupPosition, int childPosition) {return childs[groupPosition][childPosition];}@Override
        public long getGroupId(int groupPosition) {return groupPosition;}@Override
        public long getChildId(int groupPosition, int childPosition) {return childPosition;}@Override
        public boolean hasStableIds() {return true;}@Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {GroupViewHolder groupViewHolder;if (convertView == null) {convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_group, parent, false);groupViewHolder = new GroupViewHolder();groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_group);convertView.setTag(groupViewHolder);} else {groupViewHolder = (GroupViewHolder) convertView.getTag();}groupViewHolder.tvTitle.setText(groups[groupPosition]);return convertView;}@Override
        public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup parent) {ChildViewHolder childViewHolder;if (convertView == null) {convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_child, parent, false);childViewHolder = new ChildViewHolder();childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_child);convertView.setTag(childViewHolder);} else {childViewHolder = (ChildViewHolder) convertView.getTag();}childViewHolder.tvTitle.setText(childs[groupPosition][childPosition]);return convertView;}@Override
        public boolean isChildSelectable(int i, int i1) {return true;}}static class GroupViewHolder {TextView tvTitle;}static class ChildViewHolder {TextView tvTitle;}
}
布局:

1.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.expland.MainActivity"><ExpandableListView
        android:id="@+id/expandListview"
        android:layout_width="690px"
        android:layout_height="544px"></ExpandableListView></android.support.constraint.ConstraintLayout>
2.item_group:

<?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"
        android:layout_marginLeft="50dp"
        />
</LinearLayout>
3.item_child:

<?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:layout_marginLeft="50dp"
        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>