http://blog.csdn.net/ljfbest/article/details/40685327
The ListView itself has a single, multi-select mode, which can be set by Listview.setchoicemode:
Listview.setchoicemode (listview.choice_mode_multiple);//Turn on multi-select mode listview.setchoicemode ( Listview.choice_mode_single);//Open Radio mode Listview.setchoicemode (listview.choice_mode_none);//default mode Listview.setchoicemode (Listview.choice_mode_multiple_modal);//not used, do not know what to do
implementing a single selectionRequired settings: Listview.setchoicemode (Listview.choice_mode_single);
the ListView control also needs to specify a selector:android:listselector= "@drawable/checkable_item_selector"
[HTML]View Plaincopy
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable= "@color/c1" android:state_pressed="true"/>
- <item android:drawable= "@color/c1" android:state_checked="true"/>
- <item android:drawable="@color/c2"/>
- </selector>
but the color of the single-selection is still the system-selected color, not the C1 you set, do not know why?
implement multi-selectsetting: Listview.setchoicemode (Listview.choice_mode_single);
To implement the Checkable interface for each item's view, the following is the LinearLayout implementation checkable interface:
[Java]View Plaincopy
- Public class Checkablelinearlayout extends LinearLayout implements checkable {
- private Boolean mchecked;
- Public checkablelinearlayout (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- @Override
- public void Setchecked (boolean checked) {
- mchecked = checked;
- Setbackgrounddrawable (checked? New Colordrawable (0xff0000a0): null); Blue appears when selected
- }
- @Override
- Public Boolean isChecked () {
- return mchecked;
- }
- @Override
- public void Toggle () {
- Setchecked (!mchecked);
- }
- }
Use as follows:
[HTML]View Plaincopy
- <com.ljfbest.temp.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/ Android "
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/tv"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:minheight="? Android:attr/listpreferreditemheightsmall"
- android:paddingend="? android:attr/listpreferreditempaddingend"
- android:paddingstart="? Android:attr/listpreferreditempaddingstart"
- android:textappearance="? Android:attr/textappearancelistitemsmall" />
- </com.ljfbest.temp.CheckableLinearLayout>
The demo chart is attached below:
Here are a few APIs to get/set the selected entry information:Listview.getcheckeditemcount ();//Gets the selected number of rows: invalid for Choice_mode_nonelistview.getcheckeditemposition ();//Gets the selected row, only valid for radio mode, returns an intlistview.getcheckeditemids ();//Gets the IDs of the selected entries, which is the long[] type, note that adapter Hasstableids () is required to return true, And these IDs are returned by adapter's getitemid (int position). Demo in Demo
listview.setitemchecked (position, value);//Set the state of a line
ListView for multi-select/Radio