ExpandableListView之BaseExpandableListAdapter

之前使用的SimpleExpandableListAdapter有较大局限性,样式单一,修改难度大,这里不建议使用,而是利用BaseExpandableListAdapter,其实SimpleExpandableListAdapter也是继承自BaseExpandableListAdapter,这里用到的布局和上一个一样,先定义一个内部类MyExpandableListViewAdapter继承自BaseExpandableListAdapter,需要实现几个方法:
1.getGroupCount() 返回父元素的个数
2.getChildrenCount(int groupPosition) 返回当前父元素下的子元素个数
3.getGroup(int groupPosition) 返回当前父元素的数据
4.getChild(int groupPosition, int childPosition) 返回当前父元素下子元素的数据
5.getGroupId(int groupPosition) 返回当前父元素的id
6.getChildId(int groupPosition, int childPosition) 返回当前父元素下的子元素的id
7.hasStableIds() 是否有稳定的id,这里默认false不修改
8.getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 返回当前父元素的view
9.getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 返回当前子元素的view
10.isChildSelectable(int groupPosition, int childPosition) 是否可以选择,默认false,这里用不到,不修改

SecondActivity

package com.fitsoft;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
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 SecondActivity extends AppCompatActivity {

    ExpandableListView expandableListView;

    MyExpandableListViewAdapter myExpandableListViewAdapter;

    String[] groupStringArr = {"腾讯", "百度", "阿里巴巴"};

    String[][] childStringArr = {
            {"QQ","微信","QQ浏览器"},
            {"百度搜索","百度地图","百度外卖"},
            {"淘宝","支付宝","天猫商城"}
    };


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

        expandableListView = findViewById(R.id.expandable_ListView);

        myExpandableListViewAdapter = new MyExpandableListViewAdapter();

        expandableListView.setAdapter(myExpandableListViewAdapter);

    }

    class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return groupStringArr.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return childStringArr[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupStringArr[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childStringArr[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition * 100;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return groupPosition * 100 + childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            View view = convertView;
            if(view == null){
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_group, parent, false);
            }

            Object data = getGroup(groupPosition);

            TextView textView = view.findViewById(R.id.tv_group);
            textView.setText(data.toString());

            if(groupPosition % 2 == 1){
                textView.setTextColor(Color.RED);
            }else{
                textView.setTextColor(Color.BLUE);
            }

            return view;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            View view = convertView;
            if(view == null){
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_child, parent, false);
            }

            TextView textView = view.findViewById(R.id.tv_child);

            Object data = getChild(groupPosition, childPosition);

            textView.setText(data.toString());

            if(childPosition > 1){
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            }else{
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            }

            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
}

这里的数据源用的是上一个数据源,主要实现的方法是getGroupViewgetChildView,另外要注意的是,设置id的时候,为避免父元素和子元素id重复,设置父元素位置*100为其真正id,这样只要子元素不超过100个,就不会重复。

View view = convertView;
if(view == null){
    view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_group, parent, false);
}

利用这几句代码判断当前view是否需要新建,否则使用缓存

Object data = getGroup(groupPosition);

从数据源中提取相应数据

TextView textView = view.findViewById(R.id.tv_group);
textView.setText(data.toString());

并为相应的TextView设置文本

if(groupPosition % 2 == 1){
    textView.setTextColor(Color.RED);
}else{
    textView.setTextColor(Color.BLUE);
}

为了看出自定义效果,在这里为文本设置颜色,其中数组位置为奇数的设置红色,偶数的设置蓝色。
子元素的设置类似父元素,在子元素中只是区分字体的大小,不赘述。
效果图:

原文地址:https://www.cnblogs.com/zqm-sau/p/11503420.html