【发布时间】: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