【问题标题】:Checkbox in Gridview not being checked on setSelection()未在 setSelection() 上选中 Gridview 中的复选框
【发布时间】:2015-02-03 23:19:04
【问题描述】:

我有一个 gridview,其项目布局实现了可检查,因此 gridview 可以在选择项目时处理检查项目布局中的复选框。除了设置以编程方式选择的gridview项目外,这一切都很好。复选框看起来没有被选中,但一定是在后台发生了一些事情,因为选择项目之后将其保持为未选中状态,并且再次选择时它变为选中状态。

有什么想法吗?

编辑:似乎在点击时保持未选中是由于我的代码中的一些其他逻辑,因此这可能是实际问题的一个红鲱鱼。

可检查的布局

public class WeedFilterItem extends LinearLayout implements Checkable {

private TextView label;
private CheckBox checkBox;
private boolean mChecked;

public WeedFilterItem(Context context) {
    super(context);
    init();
}

public WeedFilterItem(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public WeedFilterItem(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    inflate(getContext(), R.layout.weed_filter_item, this);
    this.label = (TextView) findViewById(R.id.filter_textview);
    this.checkBox = (CheckBox) findViewById(R.id.checkbox);
}

public void setChecked(boolean checked) {
    mChecked = checked;
    this.checkBox.setChecked(checked);
}

public boolean isChecked() {
    return mChecked;
}

public void toggle() {
    setChecked(!mChecked);
}
}

项目布局 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_gravity="center"
        android:scaleX="1.5"
        android:scaleY="1.5"/>

    <TextView
        android:id="@+id/filter_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:paddingTop="10dp"
        />

</LinearLayout>

设置网格视图

final GridView filters = (GridView) child.findViewById(R.id.filter_gridview);
final WeedFilterGridViewAdapter adapter = new WeedFilterGridViewAdapter(this, values);
filters.setAdapter(adapter);
filters.setSelection(0);

其他一切都由股票网格视图处理,只是尝试在其上调用gridView.setSelection(int)。

我还尝试将所选项目的 int 存储在适配器中,并在 getView 中手动设置复选框以及调用 notifyDataSetChanged(),但这也不起作用。

【问题讨论】:

    标签: android android-gridview android-checkbox


    【解决方案1】:

    更新

    final GridView filters = (GridView) child.findViewById(R.id.filter_gridview);
    final WeedFilterGridViewAdapter adapter = new WeedFilterGridViewAdapter(this, values);
    filters.setAdapter(adapter);
    filters.setSelection(0);
    // add this line
    filters.setItemChecked(0, true);
    

    http://developer.android.com/reference/android/widget/AbsListView.html#setItemChecked(int, boolean)

    public void setItemChecked (int position, boolean value)

    在 API 级别 1 中添加 设置指定位置的选中状态。 The is only valid if the choice mode has been set to CHOICE_MODE_SINGLE 或 CHOICE_MODE_MULTIPLE。

    参数 position 待检查状态的项 value 项目的新选中状态

    【讨论】:

    • 恐怕这不起作用,状态也可能没有被更改为检查,因为这是由于我的代码中的一些其他逻辑。
    • 在适配器上也尝试了 notifyDataSetChanged,我添加了布局的代码 sn-ps。
    • @DemandedCross 在您声明的原始问题中“这一切都很好,除了设置以编程方式选择的 gridview 项目。”。你也可以在你这样做的地方发布代码吗?
    • 这很奇怪,因为如果我在 gridview 上调用 clearChoices() 然后再调用 requestLayout() 它会取消选中复选框。
    • 太棒了!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多