`

expandlistview

 
阅读更多

expandlistview的DEMO,来自网络。

 

for (int i = 0; i < 5; i++) {
			Map<String, String> curGroupMap = new HashMap<String, String>();
			groupData.add(curGroupMap);
			curGroupMap.put(G_TEXT, "Group " + i);

			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			for (int j = 0; j < 5; j++) {
				Map<String, String> curChildMap = new HashMap<String, String>();
				children.add(curChildMap);
				curChildMap.put(C_TEXT1, "Child " + j);
				curChildMap.put(C_TEXT2, "Child " + j);
			}
			childData.add(children);
		}

		adapter = new ExAdapter(ExListView.this);
		exList = (ExpandableListView) findViewById(R.id.list);
		exList.setAdapter(adapter);
		exList.setGroupIndicator(null);
		exList.setDivider(null);
		exList.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Log.d("test", "onChildClick" + "childPosition" + childPosition);
				Log.d("test", "onChildClick" + "groupPosition" + groupPosition);
				return false;
			}
		});
		
		exList.setOnGroupExpandListener(new OnGroupExpandListener() {
			
			@Override
			public void onGroupExpand(int groupPosition) {
				Log.d("test", "onGroupExpand");
				Log.d("test", "onGroupExpand" + "groupPosition" + groupPosition );
			}
		});

 

 

class ExAdapter extends BaseExpandableListAdapter {
		ExListView exlistview;

		public ExAdapter(ExListView elv) {
			super();
			exlistview = elv;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {

			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.member_listview, null);
			}

			TextView title = (TextView) view.findViewById(R.id.content_001);
			title.setText(getGroup(groupPosition).toString());

			ImageView image = (ImageView) view.findViewById(R.id.tubiao);
			if (isExpanded)
				image.setBackgroundResource(R.drawable.btn_browser2);
			else
				image.setBackgroundResource(R.drawable.btn_browser);

			return view;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public Object getGroup(int groupPosition) {
			return groupData.get(groupPosition).get(G_TEXT).toString();
		}

		public int getGroupCount() {
			return groupData.size();

		}

		// **************************************
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.member_childitem, null);
			}
			final TextView title = (TextView) view
					.findViewById(R.id.child_text);
			title.setText(childData.get(groupPosition).get(childPosition).get(
					C_TEXT1).toString());
			final TextView title2 = (TextView) view
					.findViewById(R.id.child_text2);
			title2.setText(childData.get(groupPosition).get(childPosition).get(
					C_TEXT2).toString());
			if(childData.get(groupPosition).size() == 1){
				
			}else if(childData.get(groupPosition).size() > 1){
				if(childData.get(groupPosition).size() % 2 == 0){ //偶数
					if(childPosition == 0){//white up
						Log.d("test", "偶数white up");
					}else if(childPosition == childData.get(groupPosition).size() - 1){ //black down
						Log.d("test", "偶数black down");
					}else if((childPosition + 1) % 2 != 0){//white nomal
						Log.d("test", "偶数white nomal");
					}else if((childPosition + 1) % 2 == 0){//black normal
						Log.d("test", "偶数black normal");
					}
				}else if(childData.get(groupPosition).size() % 2 != 0){
					if(childPosition == 0){//white up
						Log.d("test", groupPosition + "奇数white up" +childPosition);
					}else if(childPosition == childData.get(groupPosition).size() - 1){ //white down
						Log.d("test",  groupPosition + "奇数white down"+childPosition);
					}else if((childPosition + 1) % 2 != 0){//white nomal
						Log.d("test",  groupPosition + "奇数white nomal"+childPosition);
					}else if((childPosition + 1) % 2 == 0){//black normal
						Log.d("test",  groupPosition + "奇数black normal"+childPosition);
					}
				}
			}
			return view;
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public Object getChild(int groupPosition, int childPosition) {
			return childData.get(groupPosition).get(childPosition).get(C_TEXT1)
					.toString();
		}

		public int getChildrenCount(int groupPosition) {
			return childData.get(groupPosition).size();
		}

		// **************************************
		public boolean hasStableIds() {
			return true;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}

 转载:
http://www.linuxidc.com/Linux/2011-08/41349.htm

 

客户提出需求,要求分组列表单击一个group其他的group应该关闭,这个group的group标题应该顶头。

我网上大致找了一圈没找到相关的,于是去google group里搜索了下 ,发现有人提出了他的解决方法:

http://groups.google.com/group/Android-developers/browse_thread/thread/638f7ae1cb75b710/0154a631a2f38717?lnk=gst&q=setOnGroupClickListener#0154a631a2f38717

受此启发我想出了自己的方法,重写setOnGroupClickListener方法,使用flag控制:

 

定义flag记录列表状态

 

int expandFlag = -1;//控制列表的展开  

重写onGroupClick方法

1.ExpandableListView listView = getExpandableListView();  
2.  
3.listView.setOnGroupClickListener(new OnGroupClickListener() {  
4.  
5. @Override  
6. public boolean onGroupClick(ExpandableListView listView, View v,  
7.   int groupPosition, long id) {  
8.    
9.  if (expandFlag == -1) {  
2.   //展开被选的group  
3.   listView.expandGroup(groupPosition);  
4.         //设置被选中的group置于顶端  
5.         listView.setSelectedGroup(groupPosition);  
6.         expandFlag = groupPosition;  
2.  } else if (expandFlag == groupPosition) {  3.   listView.collapseGroup(expandFlag);  
4.   expandFlag = -1;  5.  } else {  
6.   listView.collapseGroup(expandFlag);  
7.   //展开被选的group  
8.   listView.expandGroup(groupPosition);  
9.         //设置被选中的group置于顶端  
10.         listView.setSelectedGroup(groupPosition);  
11.         expandFlag = groupPosition;  12.  }  
13.          
14.        return true;  
15. }  
16.});  

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-08/41349.htm

 

自己修改的:

 

mApplicationsList.setOnGroupClickListener(new OnGroupClickListener() {
							@Override
							public boolean onGroupClick(ExpandableListView listView, View v,int groupPosition, long id) {
								TGLog.d("__groupPosition__expandFlag_" + groupPosition + "|" + expandFlag);
								if(groupPosition == 0){
									behDB.addOneBehCount("1806", 3);
								}else if(groupPosition == 1){
									behDB.addOneBehCount("1801", 3);
								}
								//实现:分组列表单击一个group其他的group应该关闭,这个group的group标题应该顶头。
								if (expandFlag == -1) {
									// 展开被选的group
									if(mApplicationsList.isGroupExpanded(groupPosition)){
										listView.collapseGroup(groupPosition);
									}else {
										if(groupPosition == 1){
											listView.collapseGroup(0);
											listView.expandGroup(groupPosition);
										}else if(groupPosition == 0){
											listView.expandGroup(groupPosition);
											listView.collapseGroup(1);
										}
										
									}
									// 设置被选中的group置于顶端
									listView.setSelectedGroup(groupPosition);
									expandFlag = groupPosition;
								} else if (expandFlag == groupPosition) {
									if(mApplicationsList.isGroupExpanded(groupPosition)){
										listView.collapseGroup(expandFlag);
									}else {
										if(groupPosition == 1){
											listView.collapseGroup(0);
											listView.expandGroup(groupPosition);
										}else if(groupPosition == 0){
											listView.expandGroup(groupPosition);
											listView.collapseGroup(1);
										}
									}
									expandFlag = -1;
								} else {
									if(mApplicationsList.isGroupExpanded(groupPosition)){
										listView.collapseGroup(expandFlag);
									}else {
										// 展开被选的group
										if(groupPosition == 1){
											listView.collapseGroup(0);
											listView.expandGroup(groupPosition);
										}else if(groupPosition == 0){
											listView.expandGroup(groupPosition);
											listView.collapseGroup(1);
										}
									}
									
									// 设置被选中的group置于顶端
									listView.setSelectedGroup(groupPosition);
									expandFlag = groupPosition;
								}

								return true;
							}
						});  

 

转载:

 

http://k-beta.com/android-learning-expandablelistview-contact.html

 

  ExpandableListView listView = getExpandableListView();
  
  listView.setOnGroupClickListener(new OnGroupClickListener() {

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

 

转载:

 

http://v5browser.iteye.com/blog/1728386

分享到:
评论

相关推荐

    ExpandListView

    ExpandListView在我们开发中使用非常常见,但原生的ExpandListView往往会达不到我们使用的效果,我们会以两个例子为例来理解ExpandListView,现在我们看下如何做呢?blog:...

    2级菜单Expandlistview

    标题“2级菜单Expandlistview”恰好描述了这个控件在实现多级菜单时的应用。在这个教程中,我们将深入探讨ExpandableListView的工作原理以及如何使用它来创建一个具有二级菜单的界面。 ExpandableListView是...

    安卓listview相关相关-ExpandListView滑动到顶端时固定.zip

    在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据,而ExpandListView是ListView的一个扩展,它支持子项的展开和折叠,使得显示更丰富的信息。这个压缩包"安卓listview相关相关-ExpandListView...

    ExpandListView自定义下拉刷新和上拉加载更多

    在Android开发中,ExpandListView是一种常用的控件,它允许用户展开和折叠子项,从而提供更丰富的信息展示。本文将详细讲解如何实现ExpandListView的自定义下拉刷新和上拉加载更多的功能,这对于构建动态加载数据的...

    expandlistview 加如动画效果

    "expandlistview 加入动画效果"就是这样一个需求,它指的是为ExpandableListView添加特定的动画,例如当一个分组被展开时,其他分组会向下平滑地移动,这种效果就像水波纹扩散一样,给予用户更为直观和舒适的视觉...

    expandlistview多层展开Android-TreeView-原版

    本项目"expandlistview多层展开Android-TreeView-原版"旨在实现多层展开的功能,通过嵌套`ExpandableListView`来达到这一目标。 `ExpandableListView`是Android SDK提供的一种可扩展的列表视图,它不仅支持单个条目...

    expandlistview的下拉刷新

    而“expandlistview的下拉刷新”是指为这个控件添加下拉刷新功能,使得用户在顶部拉动时可以更新数据。下拉刷新在移动应用中非常常见,为用户提供了一种便捷的方式来获取最新的内容。 实现`ExpandableListView`的...

    用RecyclerView写的ExpandListView

    总结来说,使用`RecyclerView`实现`ExpandListView`功能,主要涉及到自定义`Adapter`、处理点击事件、管理布局以及优化性能等方面。这种实现方式具有更高的灵活性,可以根据项目需求定制更多复杂的功能,同时也能...

    带展开动画的expandlistview

    "带展开动画的expandlistview"是GitHub上一个开源项目,它在ExpandableListView的基础上添加了动态展开和折叠的动画效果,提升了用户体验。 这个项目的核心在于`AnimatedExpandableListView`,它继承自Android原生...

    ExpandListView实例

    在Android开发中,ExpandListView是一种常用的控件,它允许用户展开和折叠子列表项,为用户提供了一种层次化的数据展示方式。本实例将详细介绍如何在实际应用中实现ExpandListView的功能。 首先,理解...

    利用ExpandListView集成BaseExpandListView重写各种方法设置对应布局实现个性化的二级列表界面新颖优美.rar

    利用ExpandListView集成BaseExpandListView重写各种方法设置对应布局实现个性化的二级列表界面新颖优美.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    安卓listview相关相关-有趣的ExpandListView.zip

    而ExpandListView是ListView的一个扩展,它增加了可折叠/展开子项的功能,使得用户可以对列表中的某一项进行展开以显示更多详细内容,这种设计常用于层级结构的数据展示,如菜单、目录等。 在"有趣的ExpandListView...

    异步加载 仿expandListView效果

    异步加载 仿expandListView效果 分页异步加载 仿expandListView效果 分页异步加载 仿expandListView效果 分页异步加载 仿expandListView效果 分页

    仿通讯录的ExpandListView的GroupName悬停

    在Android开发中,ExpandListView是一种常用的控件,它允许用户展开和折叠子列表项,非常适合用来展示层次结构的数据。在“仿通讯录的ExpandListView的GroupName悬停”这个主题中,我们关注的是如何实现一种类似...

    ExpandListView自动更新(以前发到eoe,现在整理下)

    在Android开发中,`ExpandListView`是一个非常常用的控件,它允许我们展示可折叠的列表项,即子项可以通过点击父项来展开或收起。本文将深入探讨如何实现`ExpandListView`的自动更新功能,这在数据动态变化时特别...

Global site tag (gtag.js) - Google Analytics