ExpandableListView(二级ListView)的使用

ExpandableListView首次加载全部展开,而不能点击收缩呢?

1、首次加载全部展开:

mDownloadListView.setAdapter(mDownloadAdapter);
		for (int i = 0; i < downloadGroup.size(); i++) {
			mDownloadListView.expandGroup(i);
		}

提醒:加载前别忘了判断adapter是否为空和有没有Group数据哦、

2、不能点击收缩:

mDownloadListView.setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				return true;
			}
		});

OK了,把他的组点击事件屏蔽。呵呵、

3、ExpandableListView的长按事件
网上的资料很多,但是真正管用的没几个,最后找到一个确实可以用的,在这分享下:
(1)

//长按事件
		mExpandableListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				// TODO Auto-generated method stub
	      menu.setHeaderTitle("选择操作");
	      menu.add(0, DOWNLOAD_RETRY, 0, "重试");
	      menu.add(0, DOWNLOAD_DEL, 0, "删除");
	      menu.add(0, DOWNLOAD_START, 0, "启动");
			}
		});

(2)

/**
	 * 长按菜单响应函数
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		//关键代码
		ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {//上面的type设定这里类型的判定!这里是child判定!
			int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); //在child判定里面,获取该child所属group!
			int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); //在child判定里面,获取该child所属position!
			switch (item.getItemId()) {
			case DOWNLOAD_RETRY:
				makeTextShort("我是重试");
				break;
			case DOWNLOAD_DEL:
				makeTextShort("我是删除");
				break;
			case DOWNLOAD_START:
				makeTextShort("我是启动");
			default:
				break;
			}
          return true;
		}
		return false;
	}


4、目前上面浮动的未安装View没有实现,iOS的貌似是叫IphoneTreeView 。


转载于:https://my.oschina.net/u/2320057/blog/548445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android Studio 中,可以使用 ExpandableListView 控件来实现二级列表。 以下是实现二级列表的步骤: 1. 在布局文件中添加 ExpandableListView 控件: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建一个 Adapter 类来提供数据和视图: ```java public class MyExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> groupList; private Map<String, List<String>> childMap; public MyExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) { this.context = context; this.groupList = groupList; this.childMap = childMap; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { String groupName = groupList.get(groupPosition); List<String> childList = childMap.get(groupName); return childList.size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { String groupName = groupList.get(groupPosition); List<String> childList = childMap.get(groupName); return childList.get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = new TextView(context); textView.setText(groupList.get(groupPosition)); textView.setPadding(100, 0, 0, 0); textView.setTextSize(20); return textView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = new TextView(context); String groupName = groupList.get(groupPosition); List<String> childList = childMap.get(groupName); textView.setText(childList.get(childPosition)); textView.setPadding(150, 0, 0, 0); textView.setTextSize(16); return textView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 3. 在 Activity 中获取 ExpandableListView 控件的实例,并设置 Adapter: ```java ExpandableListView expandableListView = findViewById(R.id.expandableListView); List<String> groupList = new ArrayList<>(); groupList.add("Group 1"); groupList.add("Group 2"); Map<String, List<String>> childMap = new HashMap<>(); List<String> childList1 = new ArrayList<>(); childList1.add("Child 1-1"); childList1.add("Child 1-2"); childList1.add("Child 1-3"); childMap.put("Group 1", childList1); List<String> childList2 = new ArrayList<>(); childList2.add("Child 2-1"); childList2.add("Child 2-2"); childMap.put("Group 2", childList2); MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, groupList, childMap); expandableListView.setAdapter(adapter); ``` 这样就可以在 Android 应用中实现二级列表了。如果需要更多的样式和交互效果,可以在 Adapter 中进行定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值