Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ListView multiple selection Mode in Android

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge of this article "how to achieve ListView multiple selection mode in Android" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve ListView multiple selection mode in Android" article.

Benefits of ListView using multiple selection mode

The interaction is separated from the data, there is no need to modify the data source in the multi-selection state, and the selection index is obtained to determine the selected data when it is finally determined.

ListView mode

CHOICE_MODE_NONE: normal mode

CHOICE_MODE_SINGLE: radio mode

CHOICE_MODE_MULTIPLE: multiple selection mode

CHOICE_MODE_MULTIPLE_MODAL: multiple selection mode (used with ActionMode).

The difference between CHOICE_MODE_MULTIPLE and CHOICE_MODE_MULTIPLE_MODAL:

The former can simultaneously respond to ListView Item click events and Item select events.

The latter responds to the Item click event without entering ActionMode mode and does not respond to the Item selection event. Does not respond to the Item click event after entering ActionMode mode, responds to the Item selection event.

At the same time, if the Item long press can automatically enter the ActionMode mode (you can also use ListView.setItemChecked to select an Item to enter the ActionMode mode), when all Item are deselected, automatically exit the ActionMode mode.

How to use CHOICE_MODE_MULTIPLE_MODAL:

ListView = (ListView) findViewById (R.id.list_view); listView.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener (new ListView.MultiChoiceModeListener ()); setting method

XML layout file settings (multiple selection mode settings):

Code settings:

ListView.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE)

Choice

The ListView multi-selection state is recorded in the ListView control and implemented by its parent class AbsListView.

The outermost layer of the list Item needs to implement the Checkable interface, such as CheckBox, CheckedTextView and other controls.

If you need to use a container control such as LinearLayout, you can override the control to implement the Checkable interface.

For example:

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 = checked; refreshDrawableState (); for (int I = 0, len = getChildCount (); I < len; iTunes +) {View child = getChildAt (I) If (child instanceof Checkable) {((Checkable) child) .setChecked (checked);}} get the selection data

The final selection result is the index collection of the selected items, which is a SparseBooleanArray, which records the item selection status of the operation (if item is selected and then canceled, it will also be recorded, the status is false).

SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions ()

API

/ / clear selected

Public void clearChoices ()

/ / determine whether to select it according to the index

Public boolean isItemChecked (int position)

/ / get the selected quantity

Public int getCheckedItemCount ()

/ / set the selected status according to the index

Public void setItemChecked (int position, boolean value)

Be careful

If there is a button in the ListView Item that will grab the Item click event, the solution is to add the Item root layout

Android:descendantFocusability= "blocksDescendants"

There are two types of ActionMode snooping callback: ActionMode.Callback and MultiChoiceModeListener. The latter inherits from the former and adds a callback method for selecting and deselecting item. However, this method is only valid in CHOICE_MODE_MULTIPLE_MODAL mode, so MultiChoiceModeListener snooping is only applicable to CHOICE_MODE_MULTIPLE_MODAL mode.

If ListView uses CHOICE_MODE_MULTIPLE_MODAL mode, it can automatically enter ActionMode mode and use setMultiChoiceModeListener (MultiChoiceModeListener listener) to set the listening callback.

The above is about the content of this article on "how to achieve ListView multiple selection mode in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report