gpt4 book ai didi

android - CursorAdapter 在 Android 上如何在 GridView 中工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:54 25 4
gpt4 key购买 nike

我在 gridview 上使用光标适配器时遇到问题,我使用光标从媒体商店加载照片。我意识到我的 newView 和 bindView 被完全调用了。我的意思是假设我有 500 张照片,newView 也会被调用相同的次数。

我做错了什么吗?我认为它只会在单元格在屏幕上可见时调用。

    public int taskA = 0;

public GalleryCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
int index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
long id = cursor.getLong(index);

Bundle idBundle = new Bundle();
idBundle.putLong("id", id);

Message msg = new Message();
msg.setData(idBundle);

ImageHandler imgHandler = new ImageHandler(context, (ImageView) view);
imgHandler.sendMessage(msg);

view.setTag(imgHandler);
Log.w("task s", " count");
}

@SuppressLint({ "NewApi", "NewApi" })
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
int index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
long id = cursor.getLong(index);

ImageView iView = new ImageView(context);

Bundle idBundle = new Bundle();
idBundle.putLong("id", id);

Message msg = new Message();
msg.setData(idBundle);

ImageHandler imgHandler = new ImageHandler(context, iView);
imgHandler.sendMessage(msg);

iView.setTag(imgHandler);
taskA++;
Log.w("task s", taskA+ " count");
return iView;
}

static class ImageHandler extends Handler {

private ImageView mView;
private Context mContext;

public ImageHandler(Context c, ImageView v) {
mView = v;
mContext = c;
}

@Override
public void handleMessage(Message msg) {

Bundle idBundle = msg.getData();

Long id = idBundle.getLong("id");
Bitmap image = MediaStore.Images.Thumbnails.getThumbnail(
mContext.getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
new Options());

mView.setImageBitmap(image);
}
}

最佳答案

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ImageView iView = new ImageView(context);
iView.setLayoutParams(new GridView.LayoutParams(200, 200));
taskA++;
Log.w("task s", taskA+ " count");
return iView;
}

请注意,我删除了所有不应该在 newView 中的代码(它应该在 bindView 中)用您需要的任何高度/宽度替换 new GridView.LayoutParams(200, 200) , 不要使用 wrap content 因为你的内容在开始时是空的,导致 0x0 像素,所以你光标中的所有 ImageViews 一次都适合 GridView (因此每个 View 都会调用 newView 和 bindView )

关于android - CursorAdapter 在 Android 上如何在 GridView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com