How to handle multiple listeners inside GridView in Android

点点圈 提交于 2019-12-25 10:19:10

问题


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.

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?


回答1:


In your view adapter, when you set the image resource, also set an onClickListener for the 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();
    }
});


来源:https://stackoverflow.com/questions/13880780/how-to-handle-multiple-listeners-inside-gridview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!