栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android ExpandableListView单选以及多选实现代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android ExpandableListView单选以及多选实现代码

一、概述

ExpandableListView是常用的一个控件,今天自己做了个小练习,主要需求是单选以及多选的实现,看似比较简单,但是还是比较复杂,把代码贴给大家,有这种需求的可以参考一下。 

二、效果截图

三、实现过程

activity_main.xml

 
 
   
   
 
 

group_item.xml

 
 
 
   
   
 
   
 
 

listview_item.xml

 
 
 
   
   
 
   
 
 

 MainAcitivity.java

public class MainActivity extends Activity { 
  private List> parentList = new ArrayList>(); 
  private List>> childData = new ArrayList>>(); 
  private ExpandableListView exListView; 
  private Context context = this; 
  private MyAdapter adapter; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initView(); 
    initData(); 
    setListener(); 
  } 
 
   
  private HashSet hashSet; 
 
  private void setListener() 
  { 
    exListView.setonGroupExpandListener(new onGroupExpandListener() 
    { 

      @Override 
      public void onGroupExpand(int groupPosition) 
      { 
 //存取已选定的集合 
 hashSet = new HashSet(); 
      } 
    }); 
    // ExpandableListView的Group的点击事件 
    exListView.setonGroupClickListener(new onGroupClickListener() 
    { 
 
      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
   int groupPosition, long id) 
      { 
 // 可以写点击后实现的功能 
  
 return false; 
      } 
    }); 
    // ExpandableListView的child的点击事件 
 
    exListView.setonChildClickListener(new onChildClickListener() 
    { 
 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
   int groupPosition, int childPosition, long id) 
      { 
 Map map = childData.get(groupPosition).get( 
     childPosition); 
 String childChecked = map.get("isChecked"); 
 if ("No".equals(childChecked)) 
 { 
   map.put("isChecked", "Yes"); 
   hashSet.add("选定" + childPosition); 
 } else 
 { 
   map.put("isChecked", "No"); 
   if (hashSet.contains("选定" + childPosition)) 
   { 
     hashSet.remove("选定" + childPosition); 
   } 
 } 
 System.out.println("选定的长度==1" + hashSet.size()); 
 System.out.println("选定的长度==2" 
     + childData.get(groupPosition).size()); 
 if (hashSet.size() == childData.get(groupPosition).size()) 
 { 
   parentList.get(groupPosition).put("isGroupCheckd", "Yes"); 
 } else 
 { 
   parentList.get(groupPosition).put("isGroupCheckd", "No"); 
 } 
 adapter.notifyDataSetChanged(); 
 return false; 
      } 
    }); 
  } 
 
  // 初始化数据 
  private void initData() 
  { 
    for (int i = 0; i < 10; i++) 
    { 
      Map groupMap = new HashMap(); 
      groupMap.put("groupText", "item" + i); 
      groupMap.put("isGroupCheckd", "No"); 
      parentList.add(groupMap); 
    } 
    for (int i = 0; i < 10; i++) 
    { 
      List> list = new ArrayList>(); 
      for (int j = 0; j < 4; j++) 
      { 
 Map map = new HashMap(); 
 map.put("childItem", "childItem" + j); 
 map.put("isChecked", "No"); 
 list.add(map); 
      } 
      childData.add(list); 
    } 
    adapter = new MyAdapter(); 
    exListView.setAdapter(adapter); 
    exListView.expandGroup(0); 
    hashSet = new HashSet(); 
  } 
 
  private void initView() 
  { 
    exListView = (ExpandableListView) findViewById(R.id.exlistview); 
  } 
 
   
 
  private class MyAdapter extends baseExpandableListAdapter { 
    @Override 
    public Object getChild(int groupPosition, int childPosition) 
    { 
      // TODO Auto-generated method stub 
      return childData.get(groupPosition).get(childPosition); 
    } 
 
    @Override 
    public long getChildId(int groupPosition, int childPosition) 
    { 
      // TODO Auto-generated method stub 
      return childPosition; 
    } 
 
    @Override 
    public View getChildView(int groupPosition, int childPosition, 
 boolean isLastChild, View convertView, ViewGroup parent) 
    { 
 
      ViewHolder holder = null; 
      if (convertView == null) 
      { 
 holder = new ViewHolder(); 
 convertView = View.inflate(context, R.layout.listview_item, 
     null); 
 holder.childText = (TextView) convertView 
     .findViewById(R.id.id_text); 
 holder.childBox = (CheckBox) convertView 
     .findViewById(R.id.id_checkbox); 
 convertView.setTag(holder); 
      } else 
      { 
 holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.childText.setText(childData.get(groupPosition) 
   .get(childPosition).get("childItem")); 
      String isChecked = childData.get(groupPosition).get(childPosition) 
   .get("isChecked"); 
      if ("No".equals(isChecked)) 
      { 
 holder.childBox.setChecked(false); 
      } else 
      { 
 holder.childBox.setChecked(true); 
      } 
      return convertView; 
    } 
 
    @Override 
    public int getChildrenCount(int groupPosition) 
    { 
      // TODO Auto-generated method stub 
      return childData.get(groupPosition).size(); 
    } 
 
    @Override 
    public Object getGroup(int groupPosition) 
    { 
      return parentList.get(groupPosition); 
    } 
 
    @Override 
    public int getGroupCount() 
    { 
      // TODO Auto-generated method stub 
      return parentList.size(); 
    } 
 
    @Override 
    public long getGroupId(int groupPosition) 
    { 
      // TODO Auto-generated method stub 
      return groupPosition; 
    } 
 
    @Override 
    public View getGroupView(final int groupPosition, 
 final boolean isExpanded, View convertView, ViewGroup parent) 
    { 
      ViewHolder holder = null; 
      if (convertView == null) 
      { 
 holder = new ViewHolder(); 
 convertView = View.inflate(context, R.layout.group_item, null); 
 holder.groupText = (TextView) convertView 
     .findViewById(R.id.id_group_text); 
 holder.groupBox = (CheckBox) convertView 
     .findViewById(R.id.id_group_checkbox); 
 convertView.setTag(holder); 
      } else 
      { 
 holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.groupText.setText(parentList.get(groupPosition).get( 
   "groupText")); 
      final String isGroupCheckd = parentList.get(groupPosition).get( 
   "isGroupCheckd"); 
 
      if ("No".equals(isGroupCheckd)) 
      { 
 holder.groupBox.setChecked(false); 
      } else 
      { 
 holder.groupBox.setChecked(true); 
      } 
     
       
      holder.groupBox.setonClickListener(new onClickListener() 
      { 
 
 @Override 
 public void onClick(View v) 
 { 
   CheckBox groupBox = (CheckBox) v 
.findViewById(R.id.id_group_checkbox); 
   if (!isExpanded) 
   { 
     //展开某个group view 
     exListView.expandGroup(groupPosition); 
   } else 
   { 
     //关闭某个group view 
     exListView.collapseGroup(groupPosition); 
   } 
 
   if ("No".equals(isGroupCheckd)) 
   { 
     exListView.expandGroup(groupPosition); 
     groupBox.setChecked(true); 
     parentList.get(groupPosition).put("isGroupCheckd", 
  "Yes"); 
     List> list = childData 
  .get(groupPosition); 
     for (Map map : list) 
     { 
map.put("isChecked", "Yes"); 
     } 
   } else 
   { 
     groupBox.setChecked(false); 
     parentList.get(groupPosition) 
  .put("isGroupCheckd", "No"); 
     List> list = childData 
  .get(groupPosition); 
     for (Map map : list) 
     { 
map.put("isChecked", "No"); 
     } 
   } 
   notifyDataSetChanged(); 
 } 
      }); 
      return convertView; 
    } 
 
    @Override 
    public boolean hasStableIds() 
    { 
      return true; 
    } 
 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    { 
      return true; 
    } 
 
  } 
 
  private class ViewHolder { 
    TextView groupText, childText; 
    CheckBox groupBox, childBox; 
  } 
} 

四、总结及注意点

1、设置CheckBox的点击事件,而非别的

2、exListView.collapseGroup(groupPosition); 关闭正展开的子ListView.

这是demo地址,欢迎下载:

Demo下载地址

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号