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"