这篇文章主要为大家详细介绍了ExpandListView实现下拉列表案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ExpandListView实现下拉列表案例,供大家参考,具体内容如下
使用方式与ListView类似,是ListView的一个延申,Group为TextView,子元素为ListView。
效果图:
代码:
实体类:
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:ExpandListView实现下拉列表案例


基础教程推荐
猜你喜欢
- Flutter进阶之实现动画效果(三) 2022-10-28
- Android实现短信验证码输入框 2023-04-29
- IOS获取系统相册中照片的示例代码 2023-01-03
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- iOS开发使用XML解析网络数据 2022-11-12
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS开发 全机型适配解决方法 2023-01-14
- Android开发Compose集成高德地图实例 2023-06-15