android:使用BaseExpandableListAdapter实现可折叠的列表

使用BaseExpandableListAdapter可以实现折叠的列表,例如QQ分组
BaseExpandableListAdapter与BaseAdapter的基本原理是一样的,只是在传值的时候,前者要求传入2组(group和child),后者只需1组即可。
1.重写BaseExpandableListAdapter类,内容如下:

package com.hodi.hodi_opencv;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by AA on 2016/11/23.
 * 使用BaseExpandableListAdapter 可以实现所谓的可折叠的列表,例如QQ里好友的分组的功能
 */

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList;
    private List<List<String>> childList;
    public MyExpandableListAdapter(Context context, List<String> groups, List<List<String>> childs) {
        this.context = context;
        this.groupList = groups;
        this.childList = childs;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childList.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return groupList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childList.get(i).get(i1);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        GroupViewHolder holder;
        if (convertView == null){
            holder      = new GroupViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandable_group, null);

            holder.t1      = (TextView)       convertView.findViewById(R.id.tv_item_text1);
            holder.t2      = (TextView)       convertView.findViewById(R.id.tv_item_text2);
            holder.ivIcon  = (ImageView)      convertView.findViewById(R.id.iv_icon);
            holder.ivArrow = (ImageView)      convertView.findViewById(R.id.iv_arrow);
            holder.rlItem  = (RelativeLayout) convertView.findViewById(R.id.rl_item_group);

            convertView.setTag(holder);
        }
        else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        holder.t1.setText(getGroup(groupPosition).toString());

        if (isExpanded){
            holder.ivArrow.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_arrow_up));
        }
        else {
            holder.ivArrow.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_arrow_down));
        }
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
        ChildViewHolder holder;
        if (convertView == null){
            holder = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandable_child, null);
            holder.t1 = (TextView) convertView.findViewById(R.id.tv_item_child1);
            holder.t2 = (TextView) convertView.findViewById(R.id.tv_item_child2);
            holder.t3 = (TextView) convertView.findViewById(R.id.tv_item_child3);
            holder.rlItem = (RelativeLayout) convertView.findViewById(R.id.rl_item_child);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.t1.setText(getChild(i, i1).toString());

        return convertView;

    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class GroupViewHolder{
        TextView t1;
        TextView t2;
        ImageView ivIcon;
        ImageView ivArrow;
        RelativeLayout rlItem;
    }
    class ChildViewHolder{
        TextView t1;
        TextView t2;
        TextView t3;
        RelativeLayout rlItem;
    }
}

其中getGroupView、getChildView与BaseAdapter的getView实现原理一样

创建一个类,调用它

package com.hodi.hodi_opencv;

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

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

/**
 * Created by AA on 2016/11/23.
 */

public class TestExpandableListAdapterActivity extends Activity {

    private static final String TAG = "TestExpandableListAdapterActivity";
    private ExpandableListView expandableListView;

    private MyExpandableListAdapter adapter;
    private List<List<String>> childs;

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

    
    private void initView(){
        expandableListView = (ExpandableListView) findViewById(R.id.eListView);
        adapter = new MyExpandableListAdapter(this,getGroupData(),getChildData());
        expandableListView.setAdapter(adapter);

    }

    private List<String> getGroupData(){
        List<String> groups = new ArrayList<>();
        groups.add("行度");
        groups.add("金额");
        setChildData(groups.size());
        return groups;
    }

    private void setChildData(int count){
         childs = new ArrayList<>();
        for (int i = 0;i<count;i++){
            List<String> data = new ArrayList<>();
            data.add("起始行度");
            data.add("结束行度");
            childs.add(data);
        }
    }

    private List<List<String>> getChildData(){
        return childs;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值