Android ListView Focus Event conflict problem and resolution

Source: Internet
Author: User

Android ListView is good for simple list display, but when it comes to manipulating the ListView, you encounter a variety of problems. For example, save the checkbox state and the item reuse conflict, encountered the focusable control such as button, Edxttext and so on the ListView itself and the Click event Conflict. For the latter, the reason is not studied here, only solutions are provided.


If the ListView is the only button that will rob the focus of the control, the best advice is to switch the button directly to TextView, directly avoiding the focus problem.


If some product managers require complex operations in the ListView item, such as input, radio check, and so on, provide a simple response to the button control at the same time, and respond to the Onitemclick event of the ListView: Add the Ontouch event for the entire ListItem, and in the event set all the controls that may be grabbing focus to the non-focusable focus. This guarantees that the Liistview responds to the Onitemclick event, sets the Ontouch event for each control that might have the focus, sets it to the focusable focus, and then loses focus to the button, such as when the finger is away.


The specific code is as follows


@Overridepublic Boolean OnTouch (View view, Motionevent motionevent) {if (view instanceof EditText) {//edittext is set to Focusable 
   
    edittext EditText = (EditText) view;         Edittext.setfocusable (true);         Edittext.setfocusableintouchmode (True);}  else if (view instanceof Button | | view instanceof RadioButton) {//set to focusable, set as non-focusable if when finger is lifted (motionevent.getaction () = = Motion EVENT.ACTION_UP) {view.setfocusable (false); View.setfocusableintouchmode (false);} Else{view.setfocusable (True); View.setfocusableintouchmode (True);}} else {Viewholder holder = (viewholder) view.gettag ();// Set the Setfocusable and Setfocusableintouchmode methods of all controls that may have the focus to falsesetedittextfoucsablefalse (holder);} return false;}
   

Note: If there is edittext, in order to ensure that the input method normal pop-up, you need to set android:windowsoftinputmode= "Adjustpan"

Android ListView Focus Event conflict problem and resolution

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.