Android listView custom Layout combined with CheckedTextView for multiple selections

Source: Internet
Author: User


ApiDemo has an example of multi-choice in listView.

Relatively simple


[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"
};
}

The layout simple_list_item_multiple_choice.xml in the framework is a 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"
/>

In actual application, we will certainly encounter complicated layout. Once complicated layout is used, we find that the native multi-choice mode of listview is invalid! The failure cause can be found by checking the source code of ListView. The problem lies in the setupChild function.

 

[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 ));
}
}
}

The child object is the result returned by the getView () function of the custom adapter. It cannot be operated because the Checkable interface is not implemented. Work und: ListView has a function to check whether the item is selected. In combination with this function, modify the getView function of the custom adapter:


[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 (zookeeper) o alas, the csdn editor is really hard to use. Why can't I delete the messy code at the end)

 

[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>

 

 

 

 

 

 

 

 

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.