单击时图库图像会发生变化
当单击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想最好在 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