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, a simple response button control is provided, and to 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 might grab 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
?
| 1234567891011121314151617181920212223 |
@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) { if (view instanceof EditText) { //EditText设置为可焦点 EditText editText = (EditText) view; editText.setFocusable(true); editText.setFocusableInTouchMode(true); } else if(view instanceof Button || view instanceof RadioButton ) { //设置为可焦点,手指抬起时设为不可焦点 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(); //将所有有可能获取焦点的控件的setFocusable和setFocusableInTouchMode方法设为false setEditTextFoucsableFalse(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"
A companion tour, a free dating site: www.jieberu.com
Push family, free tickets, scenic spots: www.tuituizu.com
Android ListView Focus Event conflict problem and resolution