单击时图库图像会发生变化

发布于 2024-10-31 03:23:17 字数 1622 浏览 0 评论 0原文

当单击 Android 图库中的图像时,我需要将其更改为另一张图像。

我正在使用 Imageadapter 在图库中设置图像数组,并使用 onItemClickListener 来显示一条消息 - 我需要 onItemClick 将图库中的图像更改为 Imageadapter 数组之外的另一个图像。

这是图库代码:

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);  
    ga.setAdapter(new ImageAdapter(this));  

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        //@Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected icon " + (arg2+1) + " in Lifestyles", 
                    Toast.LENGTH_SHORT).show();
        }

    });

这是 ImageAdapter:

public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {  
    ctx = c;
}

//@Override
public int getCount() {

    return pics.length;
}

//@Override
public Object getItem(int arg1) {

    return arg1;
}

//@Override
public long getItemId(int arg0) {

    return arg0;
}

//@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {   
    ImageView iv = new ImageView(ctx);
    iv.setImageResource(pics[arg0]);
    iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
    iv.setLayoutParams(new Gallery.LayoutParams(135,135));
    iv.setBackgroundResource(imageBackground);
    return iv;
}

private Integer[] pics = {     
        R.drawable.icon_0,
        R.drawable.icon_1,
        R.drawable.icon_2,
        R.drawable.icon_3,
};

}

When an image in an Android gallery is clicked, I need it to change to another image.

I'm using an Imageadapter to set up an array of images in the gallery, with an onItemClickListener that, for now, toasts a message - I need that onItemClick to change the image in the gallery to another image outside of the Imageadapter array.

Here is the Gallery code:

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);  
    ga.setAdapter(new ImageAdapter(this));  

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        //@Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected icon " + (arg2+1) + " in Lifestyles", 
                    Toast.LENGTH_SHORT).show();
        }

    });

And here is the ImageAdapter:

public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {  
    ctx = c;
}

//@Override
public int getCount() {

    return pics.length;
}

//@Override
public Object getItem(int arg1) {

    return arg1;
}

//@Override
public long getItemId(int arg0) {

    return arg0;
}

//@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {   
    ImageView iv = new ImageView(ctx);
    iv.setImageResource(pics[arg0]);
    iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
    iv.setLayoutParams(new Gallery.LayoutParams(135,135));
    iv.setBackgroundResource(imageBackground);
    return iv;
}

private Integer[] pics = {     
        R.drawable.icon_0,
        R.drawable.icon_1,
        R.drawable.icon_2,
        R.drawable.icon_3,
};

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-11-07 03:23:17

我想最好在 getView 本身中工作,让视图在 onItemClick 中无效,然后更改 getView 中的图像

I guess it would be better to work that in the getView itself, Have the view invalidate in onItemClick and then change the image in the getView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文