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
- XML layout file settings (multi-select mode settings):
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="multipleChoice"/>
- 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