【问题标题】:Refreshing checkbox state in a listview刷新列表视图中的复选框状态
【发布时间】:2011-01-17 22:44:49
【问题描述】:

我有包含 CheckBox 元素的自定义行布局 (list_row.xml) 的列表视图:

<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;
    }

}

在我的 Activity(扩展 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());

但事实并非如此。滚动时它会丢失复选框状态,我不知道为什么。

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    我不确定我是否遵循了您在此处所说的所有内容,但请记住,ListView 在您滚动时会回收视图。这意味着您不能假设视图将在getView 的开头处于任何默认状态。这可能就是为什么每次获得新视图时停止将复选框重置为正确状态时它不起作用的原因。

    也就是说,您是否考虑过使用ListViews 内置的复选框工具?将其选择模式设置为 CHOICE_MODE_MULTIPLE 并使用他们提供的布局之一,例如 android.R.layout.simple_list_item_multiple_choice

    【讨论】:

    • 在我的视图声明中我有 android:choiceMode="multipleChoice" (这就是你的意思吗?)并且....不幸的是我不能使用 simple_list_item_multiple_choice 因为我有非常复杂的行布局.与下面的示例不同
    • 好的,您仍然应该能够允许 Android 处理通过在您想要复选框的布局中使用 ID 为 @android:id/text1 的 CheckedTextView 来跟踪检查了哪些 CheckBox。
    • 抱歉,没听懂...您的意思是使用 CheckedTextView 而不是 CheckBox?
    • 好吧,我查看了 simple_list_* 布局,它们都有一个 ID 为 @android:id/text1 的 CheckedTextView。我认为id是重要的部分。我会先尝试,如果它有效,请尝试将其切换到 CheckBox 并查看它是否仍然有效。
    【解决方案2】:

    我发现,因为 getView 正在重绘和回收滚动时的列表项,所以需要在类似的语句中明确说明何时应选中复选框,何时不选中(所选项目的背景颜色的类似解决方案) /p>

    if (condition){...设置复选框选中代码...}else{...设置复选框未选中代码...}

    复选框状态可以来自一个数组,每次用户在列表视图中选中或取消选中复选框时都必须更新该数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多