ExpandListView实现下拉列表案例

来自:网络
时间:2022-12-26
阅读:

ExpandListView实现下拉列表案例,供大家参考,具体内容如下

使用方式与ListView类似,是ListView的一个延申,Group为TextView,子元素为ListView。

效果图:

ExpandListView实现下拉列表案例

代码:

实体类:

Group.java

public class Group {
    private String gName;

    public Group() {
    }

    public Group(String gName) {
        this.gName = gName;
    }

    public String getName() {
        return gName;
    }

    public void setName(String gName) {
        this.gName = gName;
    }
}

Item.java

public class Item {

    private int iId;
    private String iName;

    public Item() {
    }

    public Item(int iId, String iName) {
        this.iId = iId;
        this.iName = iName;
    }

    public int getId() {
        return iId;
    }

    public String getName() {
        return iName;
    }

    public void setId(int iId) {
        this.iId = iId;
    }

    public void setName(String iName) {
        this.iName = iName;
    }
}

自定义适配器:ExpandableListViewAdapter.java

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<Group> groupList;
    private ArrayList<ArrayList<Item>> itemList;

    public ExpandableListViewAdapter(Context context, ArrayList<Group> gData, ArrayList<ArrayList<Item>> iData) {
        this.mContext = context;
        this.groupList = gData;
        this.itemList = iData;
    }

    //返回Group的个数
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //返回某个Group对应的Item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).size();
    }

    //返回某个Group对象
    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    //返回某个Item对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    //返回Group的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //返回Item的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    //获取指定组处的组数据
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_group,parent,false);
            holderGroup = new ViewHolderGroup();
            holderGroup.tv_name_group = convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(holderGroup);
        }else {
            holderGroup = (ViewHolderGroup) convertView.getTag();
        }
        holderGroup.tv_name_group.setText(groupList.get(groupPosition).getName());
        return convertView;
    }

    //获取指定组的数据、指定子列表项处的子列表项数据
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem holderItem = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_item,parent,false);
            holderItem = new ViewHolderItem();
            holderItem.iv_img_icon = convertView.findViewById(R.id.img_icon);
            holderItem.tv_name_item = convertView.findViewById(R.id.tv_item_name);
            convertView.setTag(holderItem);
        }else {
            holderItem = (ViewHolderItem) convertView.getTag();
        }
        holderItem.iv_img_icon.setImageResource(itemList.get(groupPosition).get(childPosition).getId());
        holderItem.tv_name_item.setText(itemList.get(groupPosition).get(childPosition).getName());
        return convertView;
    }

    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView tv_name_group;
    }

    class ViewHolderItem {
        ImageView iv_img_icon;
        TextView tv_name_item;
    }
}

主布局:ExpandableListViewActivity.java

public class ExpandableListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view);
        //View
        ExpandableListView exListView = findViewById(R.id.expand_list_person);
        //Model
        ArrayList<Group> groupList = new ArrayList<>();
        ArrayList<ArrayList<Item>> itemList = new ArrayList<>();
        groupList.add(new Group("朋友"));
        groupList.add(new Group("同事"));
        groupList.add(new Group("陌生人"));
        //用来存放单个item,下面用三个不同的item集合存放对应分类的item
        ArrayList<Item> items = new ArrayList<>();
        //朋友
        items.add(new Item(R.mipmap.img2, "小莉"));
        items.add(new Item(R.mipmap.img7, "小红"));
        items.add(new Item(R.mipmap.img8, "小美"));
        itemList.add(items);
        //同事
        items = new ArrayList<>();
        items.add(new Item(R.mipmap.img17, "小倩"));
        items.add(new Item(R.mipmap.img12, "小雯"));
        items.add(new Item(R.mipmap.img13, "小芳"));
        itemList.add(items);
        //陌生人
        items = new ArrayList<>();
        items.add(new Item(R.mipmap.img14, "小涵"));
        items.add(new Item(R.mipmap.img15, "小蕾"));
        items.add(new Item(R.mipmap.img16, "小雪"));
        itemList.add(items);

        ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(this, groupList, itemList);
        exListView.setAdapter(adapter);

        //为下拉列表中子元素绑定点击事件
        exListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(), "你点击了" + itemList.get(groupPosition).get(childPosition).getName(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

activity_expandable_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="5dp"
    tools:context=".MainActivity">

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

</RelativeLayout>

item_exlist_group.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"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_group_name"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center_vertical"
        android:paddingLeft="30dp"
        android:text="AP"
        android:textStyle="bold"
        android:textSize="20sp" />

</LinearLayout>

item_exlist_item.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"
    android:padding="5dp">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:focusable="false"
        android:src="@mipmap/img2" />

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:focusable="false"
        android:text="小莉"
        android:textSize="18sp" />

</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

返回顶部
顶部