Android ListView Multi-select mode

Source: Internet
Author: User

ListView uses multi-select mode benefits

interaction and data separation , in the multi-select state does not need to modify the data source , at the time of finalization to obtain the selection index to determine the selected data.

ListView mode
    • CHOICE_MODE_NONE: Normal mode;
    • CHOICE_MODE_SINGLE: single selection mode;
    • CHOICE_MODE_MULTIPLE: multi-select mode;
    • CHOICE_MODE_MULTIPLE_MODAL: Multi-select mode (used with Actionmode).
Setup mode
    1. XML layout file settings (multi-select mode settings):
      <ListView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:choiceMode="multipleChoice"/>
    2. Code settings:
      listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Choose

The ListView Multi-select State is recorded in the ListView control and implemented by its parent class Abslistview.
List item outermost needs to implement Checkable interface, such as CheckBox, Checkedtextview and other controls.
If you need to use a container class control, for example LinearLayout , you can override the control to implement an Checkable interface.
Like what:

public class Checkablelinearlayout extends LinearLayout implements checkable {Private Boolean mchecked = false;    Public Checkablelinearlayout (Context context) {super (context);    } public Checkablelinearlayout (context context, @Nullable AttributeSet attrs) {Super (context, attrs);  } public Checkablelinearlayout (context context, @Nullable attributeset attrs, int defstyleattr) {Super (context,    Attrs, defstyleattr);    } @Override public void Toggle () {setchecked (!mchecked);    } @Override public Boolean isChecked () {return mchecked; } @Override public void setchecked (Boolean checked) {if (mchecked! = checked) {mchecked = Checke            D            Refreshdrawablestate ();                for (int i = 0, Len = Getchildcount (), i < Len; i++) {View child = Getchildat (i);                if (child instanceof checkable) {((checkable) child). setchecked (checked); }            }        }    }} 
Get Selection data

Gets the final selection result as the selected index collection, which is a SparseBooleanArray record of the action item selection State (if the item is checked and then canceled, the status is false).
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();

Android ListView Multi-select mode

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.