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

标签 android gridview android-cursoradapter mediastore

我在 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/

相关文章:

Android-以编程方式绕过删除默认锁定屏幕

android - ListView 的这个自定义 CursorAdapter 是否为 Android 正确编码?

android - 将 ArrayAdapter 转换为 CursorAdapter 以在 SearchView 中使用

android - 自定义对话框与 imageview 到 gridview

android - GIF 图片加载缓慢问题

android - 如何设置 View 或 Activity 来处理以前的列表 Activity ?例如 "see full detail page"

android - android 中的警报

java - Android MediaPlayer 重置卡住 UI

java - 在进度变化时我想绘制矩形

c# - 在 gridview 中分页时试图了解错误的来源