问题1:
listview item不可以点击 checkbox可以选择(由于有些控件抢占点击)
参考 AbListView源代码:
final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right; if (inList && !child.hasFocusable()) { if (mPerformClick == null) { mPerformClick = new PerformClick(); } final AbsListView.PerformClick performClick = mPerformClick; performClick.mClickMotionPosition = motionPosition; performClick.rememberWindowAttachCount();如果子控件hasFocusable为false才可以正常点击(如果子控件具备抢占焦点的能力会影响到Item的点击事件)。
解决:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity" android:descendantFocusability="blocksDescendants" android:padding="10dp"> <CheckBox android:id="@+id/id_cb" android:focusable="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true"/>
1:该父控件设置 android:descendantFocusability="blocksDescendants" 后就不用设置子控件了。
2::所有抢占焦点的控件(CheckBox Button) 设置android:focusable="false"
2.首页第一个CheckBox被选中后,第二页第一个Checkbox也被选中问题。
checkbox复用问题解决办法:
1:
给业务对象添加一个属性(推荐最简单)
public boolean isChecked() { return isChecked; } public void setIsChecked(boolean isChecked) { this.isChecked = isChecked; } private boolean isChecked;
getView中:
final CheckBox cb = holder.getView(R.id.id_cb); cb.setChecked(bean.isChecked()); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bean.setIsChecked(cb.isChecked()); } });点击后设置对应类的IsChecked属性,当getView被调用时根据这个属性更改Checked值。
2:
全局定义一个List<Integer>记录checkbox true的position
在getView中
final CheckBox cb = holder.getView(R.id.id_cb); cb.setChecked(false); if (mPos.contains(holder.getPosition())) { cb.setChecked(true); } //cb.setChecked(bean.isChecked()); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(cb.isChecked()) mPos.add(holder.getPosition()); else { mPos.remove((Integer)holder.getPosition()); } // bean.setIsChecked(cb.isChecked()); } });