国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

支持多項(xiàng)選擇的ExpandableListView

2019-12-12 00:46:59
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了ExpandableListView多項(xiàng)選擇展示的具體代碼,供大家參考,具體內(nèi)容如下

目標(biāo)(需求):

1. 創(chuàng)建一個(gè)可展開可收縮的列表;

2. 其列表項(xiàng)包含多個(gè)checkable的部件,當(dāng)選擇某一行時(shí),該行包含的checkable的部件需要作出相應(yīng)的變化;

3. 可以選擇多個(gè)列表項(xiàng),并且這些列表項(xiàng)可被讀出

結(jié)果圖:

實(shí)現(xiàn):

1. 創(chuàng)建主layout用于規(guī)劃列表顯示。對(duì)于具體的列表項(xiàng),為了實(shí)現(xiàn)的方便我們也創(chuàng)建一個(gè)layout文件。

<?xml version="1.0" encoding="utf-8"?> <com.home.mymultichecklistview.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/layout"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="horizontal" >  <com.home.mymultichecklistview.CheckableTextView     android:id="@+id/item"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="6dip"    style="@style/text"    android:layout_weight="1"  />     <com.home.mymultichecklistview.InertCheckBox    android:id="@+id/checkbox"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginRight="5dp"    android:maxWidth="40dp"     android:maxHeight="40dp"     android:focusable="false"     android:layout_gravity="right"    android:button="@drawable/checkbox"  />     </com.home.mymultichecklistview.CheckableLinearLayout> 

2. 類似ListView,ExpandableListView也是通過Adapter來管理其包含的各種元素和操作,這里我們創(chuàng)建一個(gè)擴(kuò)展自BaseExpandableListAdapter的Adapter。與ListView不同的是,ExpandableListAdapter要渲染實(shí)現(xiàn)兩級(jí)View(Group級(jí)和列表項(xiàng)級(jí))的操作。它通過getGroupView()渲染Group項(xiàng),通過getChildView()渲染列表子項(xiàng)。

@Override   public View getGroupView(int groupPosition, boolean isExpanded,       View convertView, ViewGroup parent) {     View groupView = convertView;     if (groupView == null) {       groupView = new TextView(context);       ((TextView)groupView).setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);       groupView.setPadding(50,0,0,0);     }     ((TextView)groupView).setText(groupData[groupPosition]);     ((TextView)groupView).setTextColor(context.getResources().getColor(R.color.fgcolor));          return groupView;   }    @Override   public View getChildView(final int groupPosition, final int childPosition,       boolean isLastChild, View convertView, ViewGroup parent) {     View itemView = convertView;     final ViewHolder vh;     if (itemView == null) {       LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       itemView = inflater.inflate(R.layout.item_view, null);              vh = new ViewHolder();       vh.layout = (CheckableLinearLayout)itemView.findViewById(R.id.layout);       vh.item = (TextView)itemView.findViewById(R.id.item);       itemView.setTag(vh);     } else {       vh = (ViewHolder)itemView.getTag();     }     vh.item.setText(itemData[groupPosition][childPosition]);     final ExpandableListView listView = ((ExpandableListView)((MainActivity)context).findViewById(R.id.list));     final int position = listView.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));     listView.setItemChecked(position, checkedState[groupPosition][childPosition]);     vh.layout.setOnClickListener(new OnClickListener() {        @Override       public void onClick(View v) {         ((CheckableLinearLayout)v).toggle();         checkedState[groupPosition][childPosition] = !checkedState[groupPosition][childPosition];          listView.setItemChecked(position, ((CheckableLinearLayout)v).isChecked());       }     });     return itemView;   }  

3. 為每一列表子項(xiàng)容器創(chuàng)建OnClickListener監(jiān)聽鼠標(biāo)的點(diǎn)擊事件。在這里要注意,由于列表子項(xiàng)包含了CheckBox,所以為了使點(diǎn)擊事件不要被CheckBox捕獲,我們需要?jiǎng)?chuàng)建一個(gè)擴(kuò)展自CheckBox的類來屏蔽鼠標(biāo)和鍵盤事件。同時(shí),需要在這個(gè)容器里搜索其包含的checkable的部件并將check操作傳給這些部件。

Adapter中的方法getChildView()需要實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊監(jiān)聽器:

public View getChildView(final int groupPosition, final int childPosition,       boolean isLastChild, View convertView, ViewGroup parent) {   View itemView = convertView;   final ViewHolder vh; ...      final int position = listView.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));     listView.setItemChecked(position, checkedState[groupPosition][childPosition]);   vh.layout.setOnClickListener(new OnClickListener() {    @Override   public void onClick(View v) {     ((CheckableLinearLayout)v).toggle();     checkedState[groupPosition][childPosition] = !checkedState[groupPosition][childPosition];       listView.setItemChecked(position, ((CheckableLinearLayout)v).isChecked());   }  });  return itemView; } 

擴(kuò)展自CheckBox的InertCheckBox需要屏蔽鍵盤和鼠標(biāo)事件

public class InertCheckBox extends CheckBox {   @Override   public boolean onKeyDown(int keyCode, KeyEvent event) {   //直接返回false   return false;   }    @Override   public boolean onKeyUp(int keyCode, KeyEvent event) {   //直接返回false   return false; }    @Override   public boolean onTouchEvent(MotionEvent event) {   //直接返回false   return false;   }     @Override    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {   //直接返回false   return false;   ... } 

列表項(xiàng)容器需要實(shí)現(xiàn)Checkable接口并且把check操作傳遞給其checkable的子部件

public class CheckableLinearLayout extends LinearLayout implements Checkable { ...   @Override   public void setChecked(boolean checked) {   this.isChecked = checked;   for (Checkable view : checkableViews) {    view.setChecked(checked);    }   }      @Override   public boolean isChecked() {    return this.isChecked;   }    @Override   public void toggle() {    isChecked = !isChecked;    for (Checkable view : checkableViews)    {   view.toggle();    }   }    @Override   protected void onFinishInflate() {   super.onFinishInflate();          for (int i=0; i<this.getChildCount(); i++) {   findCheckableChild(this.getChildAt(i));   }  }    private void findCheckableChild(View child) {   if (child instanceof Checkable) {     checkableViews.add((Checkable)child);   }        if (child instanceof ViewGroup) {       for (int i=0; i<((ViewGroup)child).getChildCount(); i++) {     findCheckableChild(((ViewGroup) child).getChildAt(i));     }   }   } ... } 

開發(fā)中遇到的問題:

1. 渲染后的child view類似于放在一個(gè)cache中,下一次再通過convertView取時(shí),由于Group的收縮或擴(kuò)展操作會(huì)隱藏/顯示一些child view,導(dǎo)致某一child View的flat position發(fā)生變化,獲取到的convertView不是原來希望獲取的view。所以,每次獲取到view后都需要對(duì)其內(nèi)容重新設(shè)置(比如設(shè)置正確文本,設(shè)置監(jiān)聽器等)

2. check的狀態(tài)設(shè)置很tricky。我開始認(rèn)為直接在監(jiān)聽器中調(diào)用容器的toggle()方法即可。結(jié)果發(fā)現(xiàn)一旦某個(gè)group做了expand或collapse操作后,所有列表項(xiàng)的check狀態(tài)全沒了。后來發(fā)現(xiàn)原來group做了expand/collapse操作后,ListView會(huì)對(duì)其所有子項(xiàng)重新設(shè)置check狀態(tài),而check狀態(tài)的值是存在ListView的一個(gè)SparseBooleanArray表里(mCheckStates)。由于沒有對(duì)這個(gè)表進(jìn)行設(shè)置,所以一刷新check狀態(tài)就全丟了。并且由于這個(gè)表的key是基于拉平后所有可見的列表項(xiàng)的位置定的,當(dāng)group擴(kuò)展或收縮后,同一個(gè)列表項(xiàng),它拉平后的位置還會(huì)變化。所以,為了解決這個(gè)問題,我在adapter里增加了一個(gè)二維表用于記錄每一列表項(xiàng)的check狀態(tài)。在執(zhí)行 listView的setItemChecked函數(shù)時(shí),其check狀態(tài)是從這個(gè)自己創(chuàng)建的表中讀出的,不能通過ListView的mCheckStates來讀。這個(gè)我認(rèn)為是ExpandableListView的一個(gè)缺陷。

遺留的已知問題:

我使用了@drawable/checkbox 來定義checkbox check 和uncheck時(shí)的圖片,但當(dāng)checkbox被check上時(shí),這個(gè)checked的圖片沒有生效。不知道為什么,還需要進(jìn)一步debug.

源程序:Multi-check-in-expandablelistview

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 敖汉旗| 茌平县| 永年县| 新乡市| 叶城县| 红安县| 淄博市| 荥阳市| 会同县| 赞皇县| 株洲县| 景泰县| 饶平县| 沾益县| 榕江县| 阿图什市| 海南省| 凤翔县| 湟源县| 保山市| 同江市| 乌鲁木齐县| 瓮安县| 集安市| 南宁市| 林周县| 西丰县| 应城市| 游戏| 青冈县| 株洲市| 涟源市| 永定县| 开封市| 阜宁县| 巴林左旗| 渭南市| 邮箱| 丽水市| 仁寿县| 阿荣旗|