android复选框不选中无法点击按钮,Android-Listveiw的checkbox,Button焦点问题
CheckBox搶占Item焦點,導致Item點擊無效!
方法一:CheckBox設置android:clickable="false"
方法二:在Item根布局或ListView布局設置android:descendantFocusability="blocksDescendants"
在ListView的Item中的Button,CheckBox等子控件會搶占焦點,使得點擊item本身沒有響應!
常用android:descendantFocusability=”blocksDescendants”覆蓋子類控件焦點
descendantFocusability屬性定義viewGroup和其子控件之間關系:
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
android:descendantFocusability="blocksDescendants">
android:clickable="false"/>
android:descendantFocusability="blocksDescendants"/>
適配器在getview()中重復使用[被移除屏幕的item,即不可見的項]
會造成被選中的checkbox重新出現,顯示異常,故需要記錄checkbox的狀態!
public class MyAdapter extends BaseAdapter implements OnItemClickListener {
private HashMap isSelected; // 記錄checkbox狀態
public MyAdapter() {
// 初始化所有checkbox為未選擇
isSelected = new HashMap();
for (int i = 0; i < list.size(); i++)
isSelected.put(i, false);
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ViewHolder holder;
...
holder.cb.setChecked(isSelected.get(position)); // 更新checkbox狀態
return view;
}
@Override
public void onItemClick(AdapterView> arg0, View view,
int position, long arg3) {
// 切換checkbox狀態
isSelected.put(position, !isSelected.get(position));
notifyDataSetChanged();
}
}
public class MainActivity extends Activity {
...
MyAdapter adp = new MyAdapter();
listview.setAdapter(adp);
listview.setOnItemClickListener(adp);
...
}
總結
以上是生活随笔為你收集整理的android复选框不选中无法点击按钮,Android-Listveiw的checkbox,Button焦点问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qml demo分析(customgeo
- 下一篇: android sina oauth2.