如何为listview 中的item设置获取焦点事件
1个回答
推荐于2018-03-28 · 知道合伙人互联网行家
关注
展开全部
ListView默认情况
当item有焦点时,item上的button等子控件获取不到焦点;
当子控件有焦点时,item无焦点无法响应onItemClick事件
ViewGroup.FOCUS_AFTER_DESCENDANTS:表示item的子控件优先于item获得焦点;
ViewGroup.FOCUS_BEFORE_DESCENDANTS:表示item优先于其子控件获得焦点。
解决办法:
[java] view plaincopy
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);
}
}
当item有焦点时,item上的button等子控件获取不到焦点;
当子控件有焦点时,item无焦点无法响应onItemClick事件
ViewGroup.FOCUS_AFTER_DESCENDANTS:表示item的子控件优先于item获得焦点;
ViewGroup.FOCUS_BEFORE_DESCENDANTS:表示item优先于其子控件获得焦点。
解决办法:
[java] view plaincopy
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询