Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android listView 自定義布局結合CheckedTextView實現多選

android listView 自定義布局結合CheckedTextView實現多選

編輯:關於Android編程


apiDemo中有一個listView實現多選的例子

比較簡單


[plain]
package com.example.android.apis.view; 
 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
 
/** 
 * This example shows how to use choice mode on a list. This list is  
 * in CHOICE_MODE_MULTIPLE mode, which means the items behave like 
 * checkboxes. 
 */ 
public class List11 extends ListActivity { 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_multiple_choice, GENRES)); 
         
        final ListView listView = getListView(); 
 
        listView.setItemsCanFocus(false); 
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    } 
 
 
    private static final String[] GENRES = new String[] { 
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", 
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" 
    }; 

package com.example.android.apis.view;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This example shows how to use choice mode on a list. This list is
 * in CHOICE_MODE_MULTIPLE mode, which means the items behave like
 * checkboxes.
 */
public class List11 extends ListActivity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, GENRES));
       
        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }


    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
}

利用了framework中的布局 simple_list_item_multiple_choice.xml ,該布局內容就是一個CheckedTextView


[html]
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
/> 

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
/>

在實際的應用中我們肯定會遇到復雜布局的情況,一旦使用復雜布局,發現listview原生的多選模式失效了!失效原因通過查看ListView的源碼可以發現,問題出在setupChild函數中

 

[java]
private void setupChild(View child, int position, int y, boolean flowDown,int childrenLeft, boolean selected, boolean recycled) { 
        .................. 
        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) { 
            if (child instanceof Checkable) { 
                ((Checkable) child).setChecked(mCheckStates.get(position)); 
            } 
        } 
    } 

private void setupChild(View child, int position, int y, boolean flowDown,int childrenLeft, boolean selected, boolean recycled) {
  ..................
  if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
   if (child instanceof Checkable) {
    ((Checkable) child).setChecked(mCheckStates.get(position));
   }
  }
 }

其中的child對象就是自定義adapter的getView()函數的返回結果,由於我們沒有實現Checkable接口所以無法操作。變通辦法,ListView有一個察看item是否被選中的函數,結合該函數,修改自定義adapter的getView函數:


[java]
public View getView(int position, View convertView, ViewGroup parent) { 
        boolean check = ((ListView)parent).isItemChecked(position); 
        viewHolder.myCheckedTextView.setChecked(check); 
    } 

public View getView(int position, View convertView, ViewGroup parent) {
  boolean check = ((ListView)parent).isItemChecked(position);
  viewHolder.myCheckedTextView.setChecked(check);
 }(o(︶︿︶)o 唉csdn編輯器真心難用啊 怎麼改都不能把結尾的那些雜亂代碼刪掉)

 

[java]
 

[plain]
<PRE class=html name="code" sizcache="1" sizset="7"><PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
<PRE></PRE> 
 
</PRE> 

 

 

 

 

 

 

 

 

 

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved