ExpandableListView

(一)

1.效果图:,实现点击水果下拉水果列表,点击苹果弹出苹果

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.hello.MainActivity"
    android:orientation="vertical">

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

3..新建list_c.xml    list_p.xml

list.p.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"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv"
        android:layout_marginLeft="50dp"
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <TextView
        android:id="@+id/tv_p"
        android:textSize="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

list_c.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_c"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

4.MainActivity.java

package com.example.administrator.hello;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private List<String> list_p;
    private List<String> list_c1,list_c2,list_c3;
    private Map<String,List<String>> map;
    private int[] pics={R.drawable.e1,R.drawable.e2,R.drawable.e3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView=(ExpandableListView)findViewById(R.id.elv);

        //新建数据源
        list_p=new ArrayList<>();
        list_p.add("水果");
        list_p.add("动物");
        list_p.add("书籍");

        list_c1=new ArrayList<>();
        list_c1.add("苹果");
        list_c1.add("荔枝");
        list_c1.add("橘子");

        list_c2=new ArrayList<>();
        list_c2.add("猴子");
        list_c2.add("大象");
        list_c2.add("熊猫");

        list_c3=new ArrayList<>();
        list_c3.add("Android");
        list_c3.add("xml");
        list_c3.add("java");

        map=new HashMap<>();
        map.put("水果",list_c1);
        map.put("动物",list_c2);
        map.put("书籍",list_c3);

        //新建适配器
        MyAdapter myAdapter = new MyAdapter(MainActivity.this,list_p,map,pics);

        //视图加载适配器
        expandableListView.setAdapter(myAdapter);

        //绑定事件
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                String key  = list_p.get(groupPosition);
                List<String> list_c = map.get(key);
                Toast.makeText(MainActivity.this,list_c.get(childPosition),Toast.LENGTH_SHORT).show();
                return false;
            }
        });





    }
}

5. 新建适配器MyAdapter.java

  1 package com.example.administrator.hello;
  2 
  3 import android.content.Context;
  4 import android.util.Log;
  5 import android.view.LayoutInflater;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  8 import android.widget.BaseExpandableListAdapter;
  9 import android.widget.ImageView;
 10 import android.widget.TextView;
 11 
 12 import java.util.List;
 13 import java.util.Map;
 14 
 15 /**
 16  * Created by Administrator on 2018/5/21.
 17  */
 18 public class MyAdapter extends BaseExpandableListAdapter {
 19     private Context context;
 20     private List<String> list_p;
 21     private Map<String,List<String>> map;
 22     private int[] pics;
 23 
 24  /*   public MyAdapter(Context context, List<String> list_p, Map<String, List<String>> map) {
 25         this.context = context;
 26         this.list_p = list_p;
 27         this.map = map;
 28     }*/
 29 
 30     public MyAdapter(Context context, List<String> list_p, Map<String, List<String>> map, int[] pics) {
 31         this.context = context;
 32         this.list_p = list_p;
 33         this.map = map;
 34         this.pics = pics;
 35     }
 36 
 37     @Override
 38     public int getGroupCount() {
 39         return list_p.size();
 40     }
 41 
 42     @Override
 43     public int getChildrenCount(int groupPosition) {
 44         String key  = list_p.get(groupPosition);
 45         List<String> list_c = map.get(key);
 46         return list_c.size();
 47     }
 48 
 49     @Override
 50     public Object getGroup(int groupPosition) {
 51 
 52         return list_p.get(groupPosition);
 53     }
 54 
 55     @Override
 56     public Object getChild(int groupPosition, int childPosition) {
 57         String key  = list_p.get(groupPosition);
 58         List<String> list_c = map.get(key);
 59         return list_c;
 60     }
 61 
 62     @Override
 63     public long getGroupId(int groupPosition) {
 64         return groupPosition;
 65     }
 66 
 67     @Override
 68     public long getChildId(int groupPosition, int childPosition) {
 69         return childPosition;
 70     }
 71 
 72     @Override
 73     public boolean hasStableIds() {
 74         return true;
 75     }
 76 
 77     @Override
 78     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 79         ViewH_p viewH_p;
 80         if (convertView==null){
 81             viewH_p = new ViewH_p();
 82             convertView= LayoutInflater.from(context).inflate(R.layout.list_p,null);
 83             viewH_p.tv_p=(TextView)convertView.findViewById(R.id.tv_p);
 84             viewH_p.iv=(ImageView)convertView.findViewById(R.id.iv);
 85             convertView.setTag(viewH_p);
 86         }else {
 87           viewH_p=(ViewH_p) convertView.getTag();
 88         }
 89         viewH_p.iv.setImageResource(pics[groupPosition]);
 90         viewH_p.tv_p.setText(list_p.get(groupPosition));
 91         return convertView;
 92     }
 93 
 94     @Override
 95     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
 96         ViewH_c viewH_c;
 97         if (convertView==null){
 98             viewH_c = new ViewH_c();
 99             convertView= LayoutInflater.from(context).inflate(R.layout.list_c,null);
100             viewH_c.tv_c=(TextView)convertView.findViewById(R.id.tv_c);
101             convertView.setTag(viewH_c);
102         }else {
103             viewH_c=(ViewH_c) convertView.getTag();
104         }
105         String key  = list_p.get(groupPosition);
106         List<String> list_c = map.get(key);
107         viewH_c.tv_c.setText(list_c.get(childPosition));
108         return convertView;
109     }
110 
111     @Override
112     public boolean isChildSelectable(int groupPosition, int childPosition) {
113         return true;
114     }
115 }
116 class ViewH_p{
117     ImageView iv;
118     TextView tv_p;
119 }
120 class ViewH_c{
121     TextView tv_c;
122 }
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9067531.html