麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

Android ExpandableListView單選以及多選實現(xiàn)代碼

2019-10-21 21:49:45
字體:
來源:轉載
供稿:網(wǎng)友

一、概述

ExpandableListView是常用的一個控件,今天自己做了個小練習,主要需求是單選以及多選的實現(xiàn),看似比較簡單,但是還是比較復雜,把代碼貼給大家,有這種需求的可以參考一下。  

二、效果截圖

Android,ExpandableListView,單選,代碼

三、實現(xiàn)過程

activity_main.xml

<LinearLayout 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:orientation="vertical"   tools:context=".MainActivity" >    <ExpandableListView     android:id="@+id/exlistview"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:listSelector="@android:color/transparent" >   </ExpandableListView>  </LinearLayout> 

group_item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:descendantFocusability="blocksDescendants"   android:padding="10dp" >    <TextView     android:id="@+id/id_group_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerVertical="true"     android:layout_marginLeft="10dp"     android:padding="10dp"     android:text="hao"     android:textColor="@android:color/black"     android:textIsSelectable="true"     android:textSize="15sp" >   </TextView>    <CheckBox     android:id="@+id/id_group_checkbox"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentRight="true"     android:layout_centerVertical="true" />  </RelativeLayout> 

listview_item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:descendantFocusability="blocksDescendants"   android:padding="10dp" >    <TextView     android:id="@+id/id_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerVertical="true"     android:padding="10dp"     android:layout_marginLeft="30dp"     android:textColor="#55acac"     android:textIsSelectable="true"     android:textSize="15sp" >   </TextView>    <CheckBox     android:id="@+id/id_checkbox"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:clickable="false"     android:layout_alignParentRight="true"     android:layout_centerVertical="true" />  </RelativeLayout> 

 MainAcitivity.java

public class MainActivity extends Activity {   private List<Map<String, String>> parentList = new ArrayList<Map<String, String>>();   private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();   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();   }    /**    * 記錄正在選中的子listview的item條目 用hashset是為了去除重復值    */   private HashSet<String> hashSet;    private void setListener()   {     exListView.setOnGroupExpandListener(new OnGroupExpandListener()     {              @Override       public void onGroupExpand(int groupPosition)       {         //存取已選定的集合         hashSet = new HashSet<String>();       }     });     // ExpandableListView的Group的點擊事件     exListView.setOnGroupClickListener(new OnGroupClickListener()     {        @Override       public boolean onGroupClick(ExpandableListView parent, View v,           int groupPosition, long id)       {         // 可以寫點擊后實現(xiàn)的功能                  return false;       }     });     // ExpandableListView的child的點擊事件      exListView.setOnChildClickListener(new OnChildClickListener()     {        @Override       public boolean onChildClick(ExpandableListView parent, View v,           int groupPosition, int childPosition, long id)       {         Map<String, String> 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;       }     });   }    // 初始化數(shù)據(jù)   private void initData()   {     for (int i = 0; i < 10; i++)     {       Map<String, String> groupMap = new HashMap<String, String>();       groupMap.put("groupText", "item" + i);       groupMap.put("isGroupCheckd", "No");       parentList.add(groupMap);     }     for (int i = 0; i < 10; i++)     {       List<Map<String, String>> list = new ArrayList<Map<String, String>>();       for (int j = 0; j < 4; j++)       {         Map<String, String> map = new HashMap<String, String>();         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<String>();   }    private void initView()   {     exListView = (ExpandableListView) findViewById(R.id.exlistview);   }    /**    * 適配adapter    */    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);       }            /*        * groupListView的點擊事件        */       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<Map<String, String>> list = childData                 .get(groupPosition);             for (Map<String, String> map : list)             {               map.put("isChecked", "Yes");             }           } else           {             groupBox.setChecked(false);             parentList.get(groupPosition)                 .put("isGroupCheckd", "No");             List<Map<String, String>> list = childData                 .get(groupPosition);             for (Map<String, String> 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下載地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 色婷婷久久久亚洲一区二区三区 | 热99re久久免费视精品频软件 | 色交视频| 欧美大片一级毛片 | 欧美中文字幕一区二区三区亚洲 | 国产精品视频一区二区三区四 | 男男啪羞羞视频网站 | 中文字幕h | 欧美在线观看视频一区 | mmmwww| 亚洲人成中文字幕在线观看 | 色综合狠狠 | 久久97超碰 | 天天色图片 | 国产精品久久久久久久久岛 | av在线免费观看网 | 色交视频 | 午夜爽爽爽男女免费观看hd | 色播视频在线播放 | 欧美在线观看黄色 | 一级毛片在线视频 | 欧美爱爱视频免费看 | 欧美黄色免费视频 | 免费国产在线视频 | 一区二区三区欧美在线 | 精品国产一区二区三 | 万圣街在线观看免费完整版 | 亚洲国产精品久久久久婷婷老年 | 99精品无人区乱码在线观看 | 国产成人午夜精品 | 久久精品视频在线 | 色人阁五月天 | 日韩精品免费一区二区三区 | 国产88久久久国产精品免费二区 | 国产精品一区二区三区在线播放 | 欧美大逼网 | 国产精品一区二区三区在线看 | 国产青草视频在线观看 | 蜜桃精品视频 | chinesexxx少妇露脸 | 欧美一级做性受免费大片免费 |