每当我在列表视图中选中一个复选框时,其他随机复选框也会被选中。这可能是由于 listview 的项目回收。
我还尝试android:focusable="false"
在某些地方建议将复选框设置为复选框,但是在选中其复选框时,仍未要求onListItemClick()行。
我想要的是只有用户选中的复选框应该保持选中状态,直到用户取消选中它们。
我在下面给出了完整的代码,可以直接运行。
活动代码 - ProjActivity.java:
public class ProjActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
final CopyOfMyCustomAdapter a = new CopyOfMyCustomAdapter(this, packages);
getListView().setAdapter(a);
}}
最后,自定义布局文件 - testlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="CheckBox"
android:focusable="false"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:focusable="false"
/>
更新:在下面的答案中提出建议后,我的 CustomAdapter :
public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck;
public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
super(context, NO_SELECTION);
appInfoList = myList;
ctx=context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm = context.getPackageManager();
positionArray = new ArrayList<Boolean>(myList.size());
for(int i =0;i<myList.size();i++){
positionArray.add(false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return appInfoList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
if(row==null){
row = mInflater.inflate(R.layout.testlayout, null);
// visiblePosArray[position%visiblePosArray.length]=position;
holder = new Holder();
holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);
holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);
row.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.ckbox.setFocusable(false);
holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.ckbox.setChecked(positionArray.get(position));
holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked ){
System.out.println(position+"--- :)");
positionArray.add(position, true);
}else
positionArray.add(position, false);
}
});
return row;
}
static class Holder
{
ImageView appIcon;
CheckBox ckbox;
}
}
当我上下滚动时,我可以看到随机索引在我的布尔 Arraylist 中更改为 true,而在 syso 中。