ListView focus, androidlistview focus

Source: Internet
Author: User

ListView focus, androidlistview focus

If the view of a single Item in ListView contains a view such as checkbox and button, the ListView. setOnItemClickListener is invalid,

The event is captured by the View, and ListView cannot capture and process the event.

Solution:

Method 1:

Add android: focusable = "false" to the view corresponding to the checkbox and button"
   Android: clickable = "false" android: focusableInTouchMode = "false"

Focusable is the key

Call getSelectedItemPosition () from OnClickListener. Click and selection are irrelevant. Selection is operated through D-pad or trackball. Click is usually a Click operation.


Method 2:

In your own getView

ConvertView = mInflater. inflate (R. layout. list_item_text, null );
ConvertView. setClickable (true );
ConvertView. setOnClickListener (clickListener );
PublicOnClickListenerMyClickListener=NewOnClickListener(){ 
PublicVoidOnClick(ViewV){ 
  // Code to be written to handle the click event 
  } 
}; 

 

If your custom ListViewItem contains a Button or Checkable subclass control, the default focus is handed over to the child control, and the basis for the ListView Item to be selected is that it can get the Focus, in other words, we can set the focusable attribute of all controls contained in items in ListView to false. In this way, the items in ListView will automatically obtain the Focus permission and be selected, in this case, the onItemClick () method in onItemClickListener is returned. However, setting the focusable attribute of the sub-Control of 'itemlayout of ListView to 'false' is a little complicated. We can set its android through the root control of 'itemlayout: descendantFocusability = "blocksDescendants". In this way, Item Layout shields all child controls from obtaining the Focus permission. You do not need to reset the focusable attribute for each control in Item Layout, in this way, you can smoothly respond to the onItenClick () method in onItemClickListener.

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.