如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了 我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendants”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItemClick()方法
public void UpdateList(int selectedItem)
{
ListAdapter la = m_List.getAdapter();
int itemNum = m_List.getCount();
for(int i=0; i<itemNum; i++)
{
HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(i);
if ( i == selectedItem)
map.put("ItemIcon", R.drawable.image1);
else
map.put("ItemIcon", R.drawable.image2);
}
((SimpleAdapter)la).notifyDataSetChanged();
}
map.put("想要修改的字段","新的值");
//想设置ViewGroup里各View的焦点顺序特性,可用
//layout.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);根据参数有不同特性
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)会让所有子View无法获取焦点
public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
if (position == 1)
{
// listView.setItemsCanFocus(true);
// Use afterDescendants, because I don't want the ListView to steal focus
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
myEditText.requestFocus();
}
else
{
if (!listView.isFocused())
{
// listView.setItemsCanFocus(false);
// Use beforeDescendants so that the EditText doesn't re-take focus
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
listView.requestFocus();
}
}
}
public void onNothingSelected(AdapterView<?> listView)
{
// This happens when you start scrolling, so we need to prevent it from staying
// in the afterDescendants mode if the EditText was focused
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
有时候根据需要 <ListView
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:descendantFocusability="beforeDescendants"
/>
主要是 android:descendantFocusability属性的使用