ListView中怎么才能让特定的ITEM获取焦点
答案:2 悬赏:0 手机版
解决时间 2021-02-25 02:56
- 提问者网友:风月客
- 2021-02-24 19:16
ListView中怎么才能让特定的ITEM获取焦点
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-02-24 20:26
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);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯