Layouts and Controls (eight)-listview know how much (under) Choicemode detailed

Source: Internet
Author: User

9.4 ListView of Choice Mode

ListViewWith its own radio and multi-select function, that is, in single-selection mode, it can remember the currently selected unique list items, in the multi-select mode, it can remember the current selection of all the list items.

There are 4 types of this selection pattern CHOICE_MODE_NONE CHOICE_MODE_SINGLE CHOICE_MODE_MULTIPLE CHOICE_MODE_MULTIPLE_MODAL .

Using setChoiceMode() a function, you can set ListView the selection mode.

ListView lv = (ListView) findViewById(R.id.list_view);lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
9.4.1 Choice_mode_none

This is the ListView default selection mode, and when a user clicks on a list item, no clicked list item is treated as a selected list item.

9.4.2 Choice_mode_single

Using this option mode, when the user clicks on the list item, the last clicked list item is treated as the selected list item.

new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_single_choice , datalist);lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);lv.setAdapter(adapter);

Each time you click on a list item, you can see that the list item is clicked in its listener function.

lv.setOnItemClickListener(new ListView.OnItemClickListener() {    @Override    publicvoidonItemClickintlong id) {        //添加需要响应的操作    }}

You can get a list of the locations of the ListView getCheckedItemPositions() selected data items later,

SparseBooleanArray checkedItems = lv.getCheckedItemPositions();

For example, when a list item 3->2->1-7 is clicked in the order ordinal, it 7 will be recorded as the currently selected data item. After the call getCheckedItemPositions() , you will get a key value pair, which will only be recorded,

key=7value=true

To see the selected effect, the layout provided by the Android SDK is used as the layout of the android.R.layout.simple_list_item_single_choice list item.

9.4.3 Choice_mode_multiple

With this option mode, when the user clicks on the list item, all clicked list items are treated as the selected list item.

new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_multiple_choice, datalist);lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);lv.setAdapter(adapter);

Each time you click on a list item, you can see that the list item is clicked in its listener function.

lv.setOnItemClickListener(new ListView.OnItemClickListener() {    @Override    publicvoidonItemClickintlong id) {        //添加需要响应的操作    }}

Gets the list of locations for the ListView getCheckedItemPositions() selected data item,

SparseBooleanArray checkedItems = lv.getCheckedItemPositions();

For example, when a list item 3->2->1->7 is clicked in the order ordinal, it 3 2 1 7 will be recorded as the currently selected data item. After the call getCheckedItemPositions() , will get a key value pair, which will be recorded,

key=1value=truekey=2value=truekey=3value=truekey=7value=true

These records are arranged from small to large according to the position of the list item.

If the user clicks on a data item that has already been selected, it cancels the selection. For example, when a list item 3->2->1->7->2->1 is clicked in the order ordinal, it becomes

key=1value=falsekey=2value=falsekey=3value=truekey=7value=true

For a list item that has been canceled, it is not removed from the record, but it is changed to its value false .

To see the selected effect, the layout provided by the Android SDK is used as the layout of the android.R.layout.simple_list_item_multiple_choice list item.

9.4.4 Choice_mode_multiple_modal

In a CHOICE_MODE_MULTIPLE pattern, multiple selections cannot be made when the user has to enter multi-select mode by long pressing any of the list items.

This pattern is used in CHOICE_MODE_MULTIPLE a similar way, but it needs to be set up ActionMode so that after long pressing the list item, ActionBar the menu bar changes to open the multi-select menu.

  1. Implementation ListView of the MultiChoiceModeListener interface,

    Private  class Mymultichoicemodelistener implements ListView. Multichoicemodelistener {    @Override     Public void onitemcheckedstatechanged(Actionmode mode,intPositionLongIdBooleanChecked) {//Add a response after the list item is clicked}@Override     Public Boolean Oncreateactionmode(Actionmode mode, menu menu) {//return true here        return true; }@Override     Public Boolean Onprepareactionmode(Actionmode mode, menu menu) {//return true here        return true; }@Override     Public Boolean onactionitemclicked(Actionmode mode, MenuItem Item) {//return true here        return true; }@Override     Public void Ondestroyactionmode(Actionmode mode) {    }}
  2. Using the created interface, passed to ListView ,

    new ArrayAdapter<String>(this,                 new MyMultiChoiceModeListener();lv.setMultiChoiceModeListener(callback);lv.setAdapter(adapter);

In the CHOICE_MODE_MULTIPLE_MODAL mode, the click on the list item is in ListView.MultiChoiceModeListener the onItemCheckedStateChanged() function of the response.

To see the selected effect, the layout provided by the Android SDK is used as the layout of the android.R.layout.simple_list_item_multiple_choice list item.

9.4.5 about the state background of radio and multiple selection

In the previous demo, different data item layouts were used for different selection modes,

CHOICE_MODE_SINGLE         --> android.R.layout.simple_list_item_single_choiceCHOICE_MODE_MULTIPLE       --> android.R.layout.simple_list_item_multiple_choiceCHOICE_MODE_MULTIPLE_MODAL --> android.R.layout.simple_list_item_multiple_choice

We can also choose our own layout to achieve. But regardless of the layout, we want the selected list item to have a special effect, and to distinguish it from other data items that are not selected. There are two options here,

  1. Use the Clickable control or layout that implements the interface as the layout of the data item. For example, what we used earlier android.R.layout.simple_list_item_multiple_choice android.R.layout.simple_list_item_single_choice , they are CheckedTextView . When you click on them, they come with a tick box to see the effect.

    So, you can choose CheckBox Switch This type of layout that also implements the Clickable interface, or you can customize a layout that implements the Clickable interface. Here android.R.layout.simple_list_item_multiple_choice 's an example of the implementation,

    <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/listpreferreditemheightsmall"    android:textappearance="? Android:attr/textappearancelistitemsmall"    android:gravity="Center_vertical"    Android:checkmark="? Android:attr/listchoiceindicatormultiple"    Android:paddingstart="? Android:attr/listpreferreditempaddingstart"    Android:paddingend="? Android:attr/listpreferreditempaddingend"/>
  2. Use one for the layout of the data item Selector , telling the layout when it is activated going to be displayed later. For example, define a layout custom_item_layout.xml ,

    < TextView  xmlns:android  = "HTTP +/ Schemas.android.com/apk/res/android " android:id  =     "@android: Id/text1"  android:layout_width  = "match_parent"  android:layout_height  = "wrap_content"  android:minheight  =" 48DP " -- ;  set a minimum height android:gravity= "center_vertical" android:padding= "5DP" android:background= for good layout @dr Awable/selector "></textview ;   

    Give this layout a design selector , add the android:activated settings to the property,

    <selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_activated="true" android:drawable="@color/yellow"/></selector>

    Using this layout,

    new ArrayAdapter<String>(this,                 R.layout.custom_item_layout, datalist);lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);lv.setAdapter(adapter);

    When we choose more, the selected items turn yellow and see the following interface,

Layouts and Controls (eight)-listview know how much (under) Choicemode detailed

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.