ExpandableListView二级列表

@[TOC]ExpandableListView二级列表
ExpandableListView是可扩展的二级下拉列表,继承ListView
步骤
布局中定义ExpandableListView控件
初始化Group数据,Child数据(ArrayList<ArrayList>)
适配器BaseExpandaListAdapter
重写10个方法 getGroupCount(),getGroup(),getGroupId(),getGroupView()
getChildCount(),getChild(),getChildId(),getChildView()
hasStablelds()行是否具有唯一id isChildSelectable() 子列表是否可点击事件
属性
divider 父类分割线

childDivider 子类分割线

dividerHeight 分割线高度

groupIndicator 控制父类前面的小箭头

android:groupIndicator="@drawable/picture" 箭头换照片

android:groupIndicator="@null" 去掉小箭头

indicatorLeft 箭头或者自己设置的图片的右边框距离手机左边边缘的距离,类似于marginLeft

indicatorStart 箭头或者自己设置的图片的左边框距离手机左边边缘的距离,类似于marginLeft以上两个属性相结合可以使箭头或自己设置的图片变大变小或者翻转,因为控制了箭头或图片的左右边框

childIndicator 用于设置子项前显示的图标,不设置的话默认是没有图标的
主布局

<?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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</LinearLayout>

父布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/group_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="父列表"/>

</LinearLayout>

子布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/child_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp"
        android:background="#6B5E5E"
        android:text="字列表"/>

</LinearLayout>

适配器继承BaseExpandableListAdapter

package com.example.app2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseExpandableListAdapter {

    List<MainActivity.GroupBean> list;

    public MyAdapter(List<MainActivity.GroupBean> list) {
        this.list = list;
    }

    //获得父项的数量
    @Override
    public int getGroupCount() {
        return list.size();
    }

    //获得某个父项的子项数量
    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getChildBean().size();
    }

    //获得某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    //获得某个父项的某个子项
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getChildBean().get(childPosition);
    }

    //获得某个父项的ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获得某个子项的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //获得父项的View
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GHolderView holder;
        if (convertView==null){
            holder = new GHolderView();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group,null);
            holder.textView = convertView.findViewById(R.id.group_id);
            convertView.setTag(holder);
        }else {
            holder = (GHolderView) convertView.getTag();
        }
        holder.textView.setText(list.get(groupPosition).getGoodsname());
        return convertView;
    }

    //获得子项的View
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CHolderView holder = new CHolderView();
        if (convertView==null){
            holder = new CHolderView();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child,null);
            holder.textView = convertView.findViewById(R.id.child_id);
            convertView.setTag(holder);
        }else {
            holder = (CHolderView) convertView.getTag();
        }
        holder.textView.setText(list.get(groupPosition).getChildBean().get(childPosition).getName());
        return convertView;
    }

    //子项是否可选中,如果需要设置子相的点击事件,需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GHolderView{
        TextView textView;
    }
    class CHolderView{
        TextView textView;
    }
}

主Activity

package com.example.app2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ExpandableListView;

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

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;
    List<GroupBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = findViewById(R.id.expandable_id);
        //初始化数据
        initdata();
        expandableListView.setAdapter(new MyAdapter(list));
    }

    private void initdata() {
        List<ChildBean> fruit = new ArrayList<>();
        fruit.add(new ChildBean("苹果"));
        fruit.add(new ChildBean("橘子"));
        fruit.add(new ChildBean("香蕉"));
        List<ChildBean> book = new ArrayList<>();
        book.add(new ChildBean("三国演义"));
        book.add(new ChildBean("水浒传"));
        book.add(new ChildBean("红楼梦"));
        book.add(new ChildBean("西游记"));
        List<ChildBean> phone = new ArrayList<>();
        phone.add(new ChildBean("华为"));
        phone.add(new ChildBean("Vivo"));
        list = new ArrayList<>();
        list.add(new GroupBean(fruit,"水果"));
        list.add(new GroupBean(book,"书籍"));
        list.add(new GroupBean(phone,"手机"));
    }

    class GroupBean{
        private List<ChildBean> childBean;
        private String goodsname;

        public GroupBean() {
        }

        public GroupBean(List<ChildBean> childBean, String goodsname) {
            this.childBean = childBean;
            this.goodsname = goodsname;
        }

        public List<ChildBean> getChildBean() {
            return childBean;
        }

        public void setChildBean(List<ChildBean> childBean) {
            this.childBean = childBean;
        }

        public String getGoodsname() {
            return goodsname;
        }

        public void setGoodsname(String goodsname) {
            this.goodsname = goodsname;
        }
    }

    class ChildBean{
        private String name;

        public ChildBean(String name) {
            this.name = name;
        }

        public ChildBean() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值