У меня есть представление списка с пользовательским макетом строки (list_row.xml), который содержит элемент CheckBox:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<TableRow>
<TextView android:id="@+id/name"
android:layout_weight=".85"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="sample"
android:textSize="24sp">
</TextView>
<CheckBox android:id="@+id/enabled"
android:layout_weight=".15"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:focusable="false">
</CheckBox>
</TableRow>
</TableLayout>
В моей активности со списком у меня есть ListRowAdapter:
private static class ListRowAdapter extends ArrayAdapter<ListRow> implements
CompoundButton.OnCheckedChangeListener {
private List<ListRow> items;
private LayoutInflater vi;
private SparseBooleanArray mCheckStates;
public ListRowAdapter(Context context, int textViewResourceId,
List<ListRow> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
this.vi = LayoutInflater.from(context);
this.mCheckStates = new SparseBooleanArray(objects.size());
for(int i=0;i<mCheckStates.size();i++){
mCheckStates.put(i,items.get(i).isEnabled());
}
Log.i("VIEW", "constructor");
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
items.get(position).setEnabled(isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
ListRow r = (ListRow) buttonView.getTag();
r.setEnabled(isChecked);
mCheckStates.put(items.indexOf(r), isChecked);
Toast.makeText(getContext(), "row " + r.getName() + " changed to "+isChecked, Toast.LENGTH_SHORT).show();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
Log.i("VIEW", "init holder");
convertView = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.enabled = (CheckBox) convertView
.findViewById(R.id.enabled);
convertView.setTag(holder);
} else {
Log.i("VIEW", "using holder");
holder = (ViewHolder) convertView.getTag();
}
ListRow row = items.get(position);
if (row != null) {
holder.name.setText(row.getName());
holder.enabled.setTag(row);
holder.enabled.setChecked(mCheckStates.get(position, false)); holder.enabled.setOnCheckedChangeListener(this);
}
return convertView;
}
static class ViewHolder {
TextView name;
CheckBox enabled;
}
}
Inмоя активность (которая расширяет ListActivity) я делаю следующее:
private static List<ListRow> rows;
private ListRowAdapter lra;
... а затем
getListView().setItemsCanFocus(false);
getListView().setTextFilterEnabled(true);
rows = initRows();
lra = new ListRowAdapter(this, R.layout.list_row, rows);
setListAdapter(lra);
, и это прекрасно работает при прокрутке.Но если я не использую массив mCheckStates
, он потерпит неудачу!Я просто не могу понять почему.У меня есть массив items как член моего класса Activity, и он содержит все состояния флажков!Я обновляю его методом onCheckedChanged()
, как я обновляю mCheckState
.Таким образом, следующий код должен работать:
public boolean isChecked(int position) {
return items.get(position).isEnebled();
}
public void setChecked(int position, boolean isChecked) {
items.get(position).setEnabled(isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ListRow r = (ListRow) buttonView.getTag();
r.setEnabled(isChecked);
Toast.makeText(getContext(), "row " + r.getName() + " changed to "+isChecked, Toast.LENGTH_SHORT).show();
}
и в методе getView()
:
holder.enabled.setTag(row);holder.enabled.setChecked(row.isEnabled());
Но это не так.Он теряет состояния флажков при прокрутке, и я не знаю почему.