android ListView的item與其子控制元件的焦點問題
阿新 • • 發佈:2019-02-16
ListView預設情況
當item有焦點時,item上的button等子控制元件獲取不到焦點;
當子控制元件有焦點時,item無焦點無法響應onItemClick事件
ViewGroup.FOCUS_AFTER_DESCENDANTS:表示item的子控制元件優先於item獲得焦點;
ViewGroup.FOCUS_BEFORE_DESCENDANTS:表示item優先於其子控制元件獲得焦點。
解決辦法:
listView.setOnItemSelectedListener(onItemSelectedListener); private AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //當此選中的item的子控制元件需要獲得焦點時 parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); //else parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); } @Override public void onNothingSelected(AdapterView<?> parent) { parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); } }