Android ListView focus event conflict Problem and Solution

Source: Internet
Author: User

Android ListView focus event conflict Problem and Solution

Android ListView is easy to use for simple list display, but it may encounter various problems once involved in listView operations. For example, conflicts between saving the Checkbox status and item reuse, and controls that can obtain the focus, such as buttons and edxttext, and click events of listView. For the latter, we will not study the reasons here, but will only provide solutions.


If there is only a button in the listView, the focus control will be snatched. The best advice is to change the button to Textview directly to avoid the focus issue.


If some product managers require complex operations in listview items, such as input and single-choice check, a simple simultaneous response button control is provided here, solution to the onItemClick event in response to listView: add an OnTouch event to the entire ListItem, and set all controls that may snatch the focus to inaccessible focus in the event. This ensures that liistView responds to the OnItemClick event, and then sets the OnTouch event for each control that may obtain the focus, in which it is set to get focus, note that the focus of the Button should be lost when the finger leaves.


The Code is as follows:


@ Overridepublic boolean onTouch (View view, MotionEvent motionEvent) {if (view instanceof EditText) {// EditText is set to focus EditText editText = (EditText) view; editText. setFocusable (true); editText. setFocusableInTouchMode (true);} else if (view instanceof Button | view instanceof RadioButton) {// set to focus, set to not focus if (motionEvent. getAction () = MotionEvent. 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 obtain the focus to falsesetEditTextFoucsableFalse (holder);} return false ;}

NOTE: If EditText exists, you must set android: windowSoftInputMode = "adjustPan" to ensure that the input method pops up normally"

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.