【问题标题】:set image to imageview from clicked item in listview in android从android的listview中单击的项目将图像设置为imageview
【发布时间】:2013-09-25 20:43:41
【问题描述】:

我创建了一个带有延迟加载图像的列表视图。图片来自网络。如何获取当前选定的图像位图并将该位图设置为其他图像视图?

我使用了这段代码,但不起作用:

mUuserGallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                ListAdapter lList = mUuserGallery.getAdapter();
                final View imageView1 = (View)lList.getView(position, view, parent);
                final ImageView imageView3 = (ImageView) imageView1.findViewById(R.id.imageIcon);

                imageView3.setDrawingCacheEnabled(true);
                imageView3.buildDrawingCache();


                if (imageView3.getDrawable() instanceof BitmapDrawable) {
                    b = ((BitmapDrawable) imageView3.getDrawable()).getBitmap();
                } else {
                    Drawable d = imageView3.getDrawable();
                    b = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(b);
                    d.draw(canvas);
                }

                mImgViewer = (ImageView)mView.findViewById(R.id.imageView2);
                mImgViewer.bringToFront();
                mImgViewer.setImageBitmap(b);


            }
        });

【问题讨论】:

  • {final View imageView1 = mUuserGallery.getChildAt(position);} 这就是问题所在。我没有得到所选元素的正确视图...

标签: android listview bitmap imageview


【解决方案1】:

您知道延迟加载依赖于在某些数据结构上使用那些可绘制或位图以供以后使用。 (列表视图滚动)。

如果您使用地图数据结构来存储可绘制对象:

Map<String, Drawable> drawableMap;

所以,你可以得到如下的drawable:

if (drawableMap.containsKey(urlString)) {
    return drawableMap.get(urlString);
}

使用位置从列表视图适配器获取 urlString。

【讨论】:

    【解决方案2】:

    试试这个:

     mImgViewer.setImageDrawable(imageView3.getDrawable());
    

    【讨论】:

    • 最终查看 imageView1 = mUuserGallery.getChildAt(position); - 这就是问题所在,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多