首页 > 代码库 > ListView之OnItemSelectedListener监听器无响应问题!

ListView之OnItemSelectedListener监听器无响应问题!

onItemSelected()在纯触摸屏的手机上是没有效果的,在有硬键盘的手机上按上下键才会有选中效果。
如果你要做多选的话,可以在ListView中加复选框来选择列表,新建其它按钮来操作选中的选项,或者使用OnItemClickListener


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的子控件需要获得<strong>焦点</strong>时
		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);
	}
}

ListView之OnItemSelectedListener监听器无响应问题!