android可扩展列表-ExpandableListView

简介

ExpandableListView可扩展列表,或者称其为二级列表,它能够完美的实现二级列表的显示和隐藏
官网

声明javaBean

首先声明需要用的数据类,及其关联类

public class Person {
    private List hobbies;
    private String name;
    /**
     * more code ...
     */
}
public class Hobby {
    private String name;
    /**
     * more code ...
     */
}

列表项布局

  • item.xml
    
    
        
    
    
  • child_item.xml
    
    
        
    
    

重写adapter

实现adapter有三种方式

  • 继承BaseExpandableListAdapter
  • 使用SimpleExpandableListAdapter
  • 使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter

推荐使用第一种方法
核心方法就是getGroupView(),getChildView()分别用于声明列表视图,和子列表视图,代码如下:

public class ExpandAdapter extends BaseExpandableListAdapter {
    private int itemViewId;
    private int childViewId;
    private LayoutInflater layoutInflater;
    private List people;

    public ExpandAdapter(int itemViewId, int childViewId, LayoutInflater layoutInflater, List people) {
        this.itemViewId = itemViewId;
        this.childViewId = childViewId;
        this.layoutInflater = layoutInflater;
        this.people = people;
    }
    //返回列表项数量
    @Override
    public int getGroupCount() {
        return people.size();
    }
    //返回子列表项数量
    @Override
    public int getChildrenCount(int groupPosition) {
        return people.get(groupPosition).getHobbies().size();
    }
    //获得指定列表项数据
    @Override
    public Object getGroup(int groupPosition) {
        return people.get(groupPosition);
    }
    //获得指定子列表项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return people.get(groupPosition).getHobbies().get(childPosition);
    }
    //获得父列表id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //获得子列表id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    //指定位置相应的组视图
    @Override
    public boolean hasStableIds() {
        return true;
    }
    //设置父列表的view
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView!=null)
            return convertView;
        View view = layoutInflater.inflate(itemViewId,parent,false);
        TextView textView = view.findViewById(R.id.per_name);
        textView.setText(people.get(groupPosition).getName());
        return view;
    }
    //设置子列表的view
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView!=null)
            return convertView;
        View view = layoutInflater.inflate(childViewId,parent,false);
        TextView textView = view.findViewById(R.id.hobby_name);
        textView.setText(people.get(groupPosition).getHobbies().get(childPosition).getName());
        return view;
    }

    //当选择子节点的时候,调用该方法(点击二级列表)
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

实例化ExPandableListView

在activity实例化组件,并设置数据,设置adapter

expandableListView = findViewById(R.id.expandable);
expandableListView.setAdapter(new ExpandAdapter(R.layout.item,R.layout.child_item,LayoutInflater.from(getBaseContext()),init()));

监听事件

ExPandableListView提供对一级列表和二级列表的点击事件监听,分别是OnGroupClickListener()OnChildClickListener()

你可能感兴趣的:(android可扩展列表-ExpandableListView)