[Android] The listview item event and the view event in the item both exist.

Source: Internet
Author: User

Encountered a problem:

The onitemclicklistener of listview listens to events. If a button in the item also listens to events, it cannot listen to events at the same time, but it does not respond.

Solution:

The essence of the solution is to transfer the event to a view in the item without The onitemclicklistener event.

Solution 1. Obtain the view of the outermost layer of an item

 
At this time, if you set listview. setonitemlongclicklistener () for listview; after the event, it is found that only long-pressed edittext can trigger the item event. All methods for getting the focus and losing the focus are tried. There is no solution: the simplest solution is to get the layout of the outermost layer of the item in the getview method: viewholder. ll_item= (Linearlayout) convertview. findviewbyid (R.ID. Ll_item); then set the layout event in the getview method: viewholder. ll_item.setonlongclicklistener (NewOnlongclicklistener () {@ overridePublicBoolean onlongclick (view v) {}); you can click edittext to input text or item to trigger the event you need!

Solution 2: Add a view overwrite to the bottom layer of the item

Side effect: other views on the view cannot monitor events. (Not tested)

 

Side effects

 

Solution 3. This articleArticleIt seems that the problem of simultaneous listening can be solved.

Http://txlong-onz.iteye.com/blog/907186

In addition, you cannot add both listview and imageview listeners by using this method. In the web search, some attributes are not added to XML, and the XML root node in list configuration cannot be added.Android: descendantfocusability = "blocksdescendants"And addAndroid: focusable = "false", AboveCode.

 

These are related to the view and listview focus issues:

Http://blog.sina.com.cn/s/blog_a855dc2c01014ith.html

 

Solution 4: (tested) completely solved

Similar to the preceding figure, a view is used at the bottom to listen to events. Use framelayout in item to solve the problem completely.

If a listener is written to the upper-layer view, when an event is monitored, the event will be handed over for processing and will not be passed down;

If the upper view does not have a listener, the event will be passed down until the view listens to and processes the event, or ends at the bottom.

the upper view does not affect the lower view listening events.

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.