- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现一个支持多项选择的图片库。主要布局是 GridView
,它使用自定义 Adapter
。每个显示的项目都是 ImageView
+ CheckBox
的组合。如果选中了多个复选框,我想通过触摸 GridView
中的任何图像来取消选中它们。我正在调用 notifyDataSetChanged()
来实现此功能。不幸的是,这个“全部取消选中”功能不起作用。我还是个新手,所以可能有一些我不明白的基本知识。有人能告诉我我的 Adapter
有什么问题吗?谢谢。
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// References to our images
public Integer[] mThumbIds = {
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3
};
// The layout inflater used to retrieve gallery children layouts
private LayoutInflater mInflater;
// Array used to update checkboxes
public boolean[] checkboxesState = new boolean[getCount()];
public ImageAdapter(Context c) {
mContext = c;
mInflater = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Define the initial checked/unchecked state of the checkboxes
for(int i = 0; i < getCount(); i++) {
checkboxesState[i] = false;
}
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/* Create a new View for each cell displayed in the GridView.
* In our case a View is an instance of RelativeLayout from which
* we can extract the image and checkbox to be displayed in the cell
*/
public View getView(int position, View convertView, ViewGroup parent) {
View cell = convertView;
if (cell == null) {
// No View passed, create one.
cell = mInflater.inflate(R.layout.galleryitem, null);
// Setup the View content
ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
checkBox.setChecked(checkboxesState[position]);
checkBox.setId(position);
imageView.setImageBitmap(downsample(mThumbIds[position]));
// Setup the View behavior
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Uncheck all checked items
uncheckAll();
}
});
checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
checkboxesState[cb.getId()] = cb.isChecked();
}
});
}
return cell;
}
private Bitmap downsample(int resId){
// Some code here
}
private void uncheckAll() {
// This code does not work. GridView is not refreshed
for(int i = 0; i < checkboxesState.length; i++) {
checkboxesState[i] = false;
}
notifyDataSetChanged();
}
}
更新
我意识到我最初的帖子并没有准确描述 uncheckAll 方法是如何失败的。当我选中 N 个复选框并调用 uncheckAll 方法时会发生什么:
这种行为让我想到 gridview 项目可能出乎意料地改变了它们在 GridView
中的位置,所以我为每个项目添加了一个具有唯一名称的 TextView
.我的想法是正确的:当我滚动屏幕时,项目会改变它们在 GridView
中的位置。调用 uncheckAll 时也会发生同样的情况。我找到了适合我的解决方案 in this thread .
注意:一开始我没有意识到移动问题,因为在这个开发阶段我为每个 GridView
项目使用相同的 ImageView
资源。
最佳答案
问题很可能是您的 getView() 方法正确地初始化了一个新的单元格 View ,但从未更新已回收的单元格 View 的状态。所以基本上每个单元格 View 只会显示它被初始化的状态。它应该看起来像这样:
public View getView(int position, View convertView, ViewGroup parent) {
View cell = convertView;
if (cell == null) {
// No View passed, create one.
cell = mInflater.inflate(R.layout.galleryitem, null);
// Setup the View content
ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
checkBox.setChecked(checkboxesState[position]);
checkBox.setId(position);
imageView.setImageBitmap(downsample(mThumbIds[position]));
// Setup the View behavior
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Uncheck all checked items
uncheckAll();
}
});
checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
checkboxesState[cb.getId()] = cb.isChecked();
}
});
} else {
ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
checkBox.setChecked(checkboxesState[position]);
checkBox.setId(position);
imageView.setImageBitmap(downsample(mThumbIds[position]));
}
return cell;
}
此外,为了提高 getView() 方法的性能,请查看此 tutorial .
关于android - 具有自定义 ImageAdapter 的 GridView 在数据集更改后不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832907/
我希望带有 ImageAdapter 的 GridView 具有自定义按下的按钮。我已经有了自定义按钮,但是 onClick 它会在按钮周围创建一个很大的橙色 onClick 东西。我只希望 onCl
我目前正在开发一个应用程序,它允许用户从 GridView 库中选择一个 ImageView 并单独显示它并展示它的所有美感。 GridView 在 fragment 上,然后将数据传递给 Activ
下面是我的代码。我正在尝试将我的 SD 卡中的图像检索到 GridView 中。我的代码有问题。我不知道如何将图像设置到我的 ImageView 中。我的 ImageAdapter 中出现错误“Ima
我有一个 getView() 的 Android Gallery ImageAdapter 实现,如下所示: public View getView(int arg0, View arg1, View
每次我测试我的应用程序时,它都会在闪屏后崩溃,并给出如下错误: 02-14 13:03:29.305: A/libc(9935): Fatal signal 11 (SIGSEGV) at 0x000
我正在尝试按照 ImageView 和 GalleryView 教程在 fragment 类中添加一个 galleryview,但是出现无法将 ImageAdapter 应用于 Tab1Fragmen
ListView 代码如下: public final static String ITEM_TITLE = "title"; public final static String ITE
我用来将图像从 SD 卡显示到 GridView 的 ImageAdapter 代码导致图像重复。同一组图像(例如 10 个)在 GridView 中重复。 这是我的适配器代码: private cl
我正在使用带有 ImageAdapter 的 Gallery 加载它,并使用 ImageViews 从我的资源中提取图像。我的问题是在我的适配器中传递给 getView() 方法的 convertVi
我做了什么 我在布局中创建了一个 GridView。这个 GridView 我想填充我在 Google 文档中创建的 ImageAdapter:Hello GridView 我的图片的图片路径,我保存
我的安卓有问题。我想使用以下代码为图库设置 ImageAdapter: Integer[] images = {R.drawable.image01}; static int counter = 0;
我有这个用于 android 的 listView 的 ImageAdapter: @Override public View getView(int position, View convertVi
我正在尝试使用 ImageAdapter 在 GridView 中显示我的 Activity 图像。都是基本功能,即使我匹配来自多个来源的代码,但我的代码没有运行,这是什么问题?ImageAdapte
好吧,在周末取得了很多进展之后,我陷入了 gridView 和 ImageAdapter 的困境。到目前为止,我所做的就是我的主要 Activity 获取用户选择的标签,执行查询并返回 json_ec
正如标题已经提到的,我想更改我的 ImageView onClick 吗?我在 ImageAdapter 中制作了一个包含 4 个项目的 GridView。当我单击每个 ImageView 时,它会在
我是 Android 新手,在“扩展 Fragment”上实现 GridView 时遇到一个问题。 Tab2SalonActivity.java public class Tab2SalonActiv
我正在实现一个支持多项选择的图片库。主要布局是 GridView,它使用自定义 Adapter。每个显示的项目都是 ImageView + CheckBox 的组合。如果选中了多个复选框,我想通过触摸
我正在使用 ImageAdapter extends BaseAdapter 来扩展 gridview。 Gridview 有两个 textview。我想为其中之一设置自定义字体。在 ImageAda
我正在尝试创建一个 Logo 测验,这很有效,但我无法弄清楚如何从 ImageAdapter 修改特定图像。任何指导将不胜感激。谢谢! 这是我的测验 Activity : public class Q
我正在使用 google 文档中提供的示例 ImageAdpater 来使用可绘制对象填充 gridview。我想要做的是用 xml 文件中的一组可绘制对象填充 gridview。 我使用 Typed
我是一名优秀的程序员,十分优秀!