Akh*_*hil 24 android android-listview
每当我检查列表视图中的复选框时,其他随机复选框也会被检查.这可能是由于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);
}}
Run Code Online (Sandbox Code Playgroud)
最后,自定义布局文件 - 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"
/>
Run Code Online (Sandbox Code Playgroud)
更新:我的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;
}
Run Code Online (Sandbox Code Playgroud)
}
当我向上和向下滚动时,我可以在syso中看到随机索引在我的布尔Arraylist中变为true.
Akh*_*hil 40
当listview回收视图时,它会回收它的当前状态以及附加到它的侦听器.在我的示例中,如果选中了复选框并且设置了onCheckedChangeListener,则两者都将基于位置保留为循环视图的一部分.因此,我们有责任重置所有状态并删除以前的侦听器.
因此,当我取消选中循环视图时,onCheckedChange监听器正在执行.一行使程序完美运行.听众被删除:
holder.ckbox.setOnCheckedChangeListener(null);
Run Code Online (Sandbox Code Playgroud)
以下是适用于可能偶然发现此问题的人的适用工具代码:
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.setOnCheckedChangeListener(null);
}
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.set(position, true);
}else
positionArray.set(position, false);
}
});
return row;
}
static class Holder
{
ImageView appIcon;
CheckBox ckbox;
}
Run Code Online (Sandbox Code Playgroud)
}
您需要跟踪检查状态,因为ListView
重新使用了Views
.因此,先前启用/禁用的位置1的状态可能与位置7一样.
所以你需要做的是将检查状态保持在数组boolean
或任何你喜欢的状态.
采用类级别boolean [] checkedState;
在构造函数中初始化它,根据您的数据数组大小,您也可以使用ArrayList<Boolean>
动态大小.
无论何时选中或取消选中,都设置OnStateChangeListener
为CheckBoxes
in getView()
,取位置并将其保存在如下数组中checkedState
:
checkedState[position] = false;// or true accordingly
Run Code Online (Sandbox Code Playgroud)
当为View
like TextView
或ImageView
任何特定位置设置其他数据时,也相应地设置检查状态,如下所示:
holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.ckbox.setChecked(checkedState[position]);
Run Code Online (Sandbox Code Playgroud)
一个很好的解释和例子:
编辑:实际上发生了什么,你的位置是越野车,解决这个添加这些线:
holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
holder.ckbox.setTag(String.valueOf(position)); // to properly track the actual position
holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
int pos = Integer.parseInt( v.getTag().toString()) ; //to take the actual position
positionArray.add(pos, isChecked); // we don't need to check whether it is true or false, however you can put if-else to debug the app.
}
});
Run Code Online (Sandbox Code Playgroud)
小智 6
这两种方法的组合对我有用:
我在类级别上有一个布尔数组,用于跟踪复选框的值.
boolean [] checkedItems = new boolean[listItems.size()];
Run Code Online (Sandbox Code Playgroud)
在getView()中:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_item_list_item,
parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView
.findViewById(R.id.menuItemLargeName);
holder.mainItemCheckBox = (CheckBox) convertView
.findViewById(R.id.menuItemLargeCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// remove the listener so that it does not get attached to other chechboxes.
holder.mainItemCheckBox.setOnCheckedChangeListener(null);
//update the checkbox value from boolean array
holder.mainItemCheckBox.setChecked(checkedItems[position]);
}
holder.name.setText(listItems.get(position).getName());
holder.mainItemCheckBox
.setOnCheckedChangeListener(onCheckedListener);
holder.mainItemCheckBox
.setTag(R.id.menuItemLargeCheckBox, position);
return (convertView);
}
Run Code Online (Sandbox Code Playgroud)
在我的OnCheckedChangeListener()中:更新布尔数组.
OnCheckedChangeListener onCheckedListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = (Integer) buttonView
.getTag(R.id.menuItemLargeCheckBox);
MenuItemObject menuItem = listItems.get(position);
if (isChecked) {
cartItems.add(menuItem);
checkedItems[position] = true;
} else {
cartItems.remove(menuItem);
checkedItems[position] = false;
}
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25323 次 |
最近记录: |