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

來源:互聯網
上載者:User


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> 

 

 

 

 

 

 

 

 

 

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.