如何在Android的GridView中处理多个侦听器 [英] How to handle multiple listeners inside GridView in Android
问题描述
在我的应用中,GridView
具有内容列表.每个项目都以布局夸大. gridview内部的单个项目包含一个图像和2个textviews
.
In my app, a GridView
has list of contents. Each item is inflated with a layout. Single item inside the gridview contains, an image and 2 textviews
.
我有一个要求,当我单击该图像时,该图像应替换为另一图像,而当我单击任何其他位置时,则应打开一个弹出窗口.我可以使用OnItemClickListener
收听第二个事件.打开弹出窗口.我该如何处理第一种情况?我的意思是我该如何收听OnClick
图片事件?
I have a requirement that when I click on the image, the image should be replaced with another image and when I click on any other places it should open a popup. I am able to listen to the second event, using OnItemClickListener
. It opens the popup. How can I handle the first case? I mean how can I listen to OnClick
event of image?
推荐答案
在视图适配器中,设置图像资源时,还要为ImageView设置onClickListener.
In your view adapter, when you set the image resource, also set an onClickListener for the ImageView.
在我的情况下,holder是一个临时的静态类,其中包含2个TextViews和一个ImageView.:
In my case, holder is a temporary static class which holds 2 TextViews and an ImageView.:
holder.mThumbnailImageView = (ImageView) convertView.findViewById(R.list.thumb);
holder.mThumbnailImageView.setImageResource(thisOrder.getIconValue());
holder.mThumbnailImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(parent.getContext(), "image clicked: " + view.getId(), Toast.LENGTH_SHORT).show();
}
});
这篇关于如何在Android的GridView中处理多个侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!