ExpandableListView(三)只展开一个group,没有child不展开group

本文是自己在实践中,发现的问题。

有时候想让界面更加的人性化,就要实现很多的效果,比如只展开一个group,在点击下个group的同时,关闭之前的group

在一个ExpandableListView,如何实现只展开一个group,方法如下:

mListView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				for (int i = 0; i < mAdapter.getGroupCount(); i++) {
					if (groupPosition != i) {
						mListView.collapseGroup(i);
					}
				}

			}

		});

效果图:

上图的效果,看上去很好,但是存在一个问题,虽然只展开了一个group,但是在点击下一个group的时候,该group的标题不会置顶,这就造成了一些困惑,为了解决这个问题,用到了下边的方法解决:

重写setOnGroupClickListener方法

首先要有一个sign,可以是int sign,用来记录group展开的状态

private int sign= -1;//控制列表的展开
重写 onGroupClick方法

		//只展开一个group的实现方法
				mListView.setOnGroupClickListener(new OnGroupClickListener() {

					@Override
					public boolean onGroupClick(ExpandableListView parent, View v,
							int groupPosition, long id) {
						// TODO Auto-generated method stub
						if (sign== -1) {
							// 展开被选的group
							mListView.expandGroup(groupPosition);
							// 设置被选中的group置于顶端
							mListView.setSelectedGroup(groupPosition);
							sign= groupPosition;
						} else if (sign== groupPosition) {
							mListView.collapseGroup(sign);
							sign= -1;
						} else {
							mListView.collapseGroup(sign);
							// 展开被选的group
							mListView.expandGroup(groupPosition);
							// 设置被选中的group置于顶端
							mListView.setSelectedGroup(groupPosition);
							sign= groupPosition;
						}
						return true;
					}
				});  

运行,看一下效果:



至此,在只打开一个group的同时,标题置顶


最后,补充一下,在group没有child的情况下,不展开group

		// 这里是控制如果列表没有孩子菜单不展开的效果
		mListView
				.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
					@Override
					public boolean onGroupClick(ExpandableListView parent,
							View v, int groupPosition, long id) {
						// TODO Auto-generated method stub
						if (childData.get(groupPosition).isEmpty()) {// isEmpty没有
							return true;
						} else {
							return false;
						}
					}
				});



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个 ExpandableListView 二级分栏的例子: 首先在布局文件中定义 ExpandableListView 和 Adapter: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 定义自定义的 ExpandableListAdapter --> ``` 然后在 Activity 中设置 Adapter 并为 ExpandableListView 设置点击事件: ```java public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private ExpandableListAdapter adapter; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 准备数据 prepareListData(); // 设置 Adapter adapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); expandableListView.setAdapter(adapter); // 设置点击事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } // 准备数据 private void prepareListData() { listDataHeader = new ArrayList<>(); listDataChild = new HashMap<>(); // 添加一级分栏 listDataHeader.add("Fruits"); listDataHeader.add("Vegetables"); // 添加二级分栏 List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); List<String> vegetables = new ArrayList<>(); vegetables.add("Carrot"); vegetables.add("Tomato"); vegetables.add("Potato"); // 将二级分栏添加到对应的一级分栏下 listDataChild.put(listDataHeader.get(0), fruits); listDataChild.put(listDataHeader.get(1), vegetables); } } ``` 最后是 ExpandableListAdapter 的实现: ```java public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this.context = context; this.listDataHeader = listDataHeader; this.listDataChild = listDataChild; } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView lblListHeader = convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView txtListChild = convertView.findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 其中 `list_group.xml` 和 `list_item.xml` 分别是一级分栏和二级分栏的布局文件,可以根据需要自行定义。 以上就是 ExpandableListView 二级分栏的例子,希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值