Overriding Android ListView onItemClick calling element to be selected (checked)

I am trying to better understand the internal functioning of ListView, as it relates to the selection of one or more items - in fact it is amazing how difficult it was for me to even understand it in my understanding.

By default, just clicking on a ListView item sets the checked state for this item to true. How to override this behavior so that this choice is not made?

And, more importantly, what is the basic mechanics of ListView here? Is the default row view onClick and then calling the ListView onItemClick / LongClick handlers, or how does this click processing get sequenced?

I want to enable selectMode for multipleChoice, but I only want to select it onItemLongClick. Overriding onItemClick does not change this behavior, and overriding the row view in the onClick handler in the getView () function of the adapter seems to prevent the ListView onItemClick and onItemLongClick from ever happening.

Below is a more detailed context of my application.


My goal is to have ListView displayed in ListActivity, which functions as follows:

  • Clicking on an element performs an action without selection (extends the line to show more information)
  • A long press on an item selects it. The item selection is indicated by highlighting the background of the line (as in the Gmail application).
  • You can select multiple items.

My application structure:

  • Activity is an extension of ListActivity
  • The adapter is an extension of the ArrayAdapter <>
  • ListView line layouts are fully customizable layouts (not any built-in ListView line layouts)

My understanding of the built-in functions for ListView allows me to such an extent that I

  • select parameter Mode to multipleChoice
  • using the Checked ListView function to create and track selections
  • using a custom selector as an "activated screen index" to display the selection ( example here )
+4
source share
1 answer

Store an ArrayList to save the selected ListView position. When the ListView is selected, check that the ArrayList is whether the position of the position is in the ArrayList or not. If the element is not in a state of changing the ArrayList of the element to check else, change the state to unchecked and remove the position object from the ArrayList. It worked for me.

+1
source

Source: https://habr.com/ru/post/1502500/


All Articles