开发者

Replace the previous selected image with original image if one clicks on a new image

开发者 https://www.devze.com 2023-03-07 00:41 出处:网络
I am using grid view with ImageAdapter to display images. I have two set of images that is mThumbIds containing original images and cThumbIds containing the selected images.

I am using grid view with ImageAdapter to display images. I have two set of images that is mThumbIds containing original images and cThumbIds containing the selected images.

Right now when i click on an image i change the normal image with the selected image. The code is as below:

final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
            iv.setOnClickLi开发者_运维知识库stener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //iv.setColorFilter(Color.LTGRAY);
                    iv.setImageResource(cThumbIds[position]);
                    //iv.bringToFront();

                    index= position;

                }

            });

            iv.setImageResource(mThumbIds[position]);

But the problem arises when i click on the another image the other image also shows as selected. The correct way would be to show the new image as selected and remove the older one as selected .In other words the older one should revert back to original one.

Please help me on this

Thanks,

Pankaj


You need to create a variable and keep the clicked image's id in that. When the user clicks some other image, first reset the other image as per the id in the variable and then replace the variable value with the id of the currently clicked image.


I'm assuming you are using a modified copy of the ImageAdapter class in this tutorial and that the code you have posted is in the getView(int,View,ViewGroup) method of that class.

You save the index of the image that is selected but you don't save the view itself. You need to save both in order to revert the old image, something like this:

private int selectedPosition = -1;
private ImageView selectedView = null;
...
public View getView(int position, View convertView, ViewGroup parent) {
    // I don't understand what this line is about??
    ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
    // Why not something like this??
    // ImageView iv = new ImageView(mContext);

    iv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // Set the selected image for the ImageView just pressed.
            iv.setImageResource(cThumbIds[position]);

            // Revert to the original image for the ImageView previously
            // pressed.
            if (selectedPosition != -1) {
                selectedView.setImageResource(mThumbIds[selectedPosition]);
            }

            // Save the position and ImageView just pressed so it can be
            // reverted next time an ImageView is pressed
            selectedPosition = position;
            selectedView = (ImageView) view;
        }
    });

    iv.setImageResource(mThumbIds[position]);
    return (iv);
}

I'm a bit confused about the line ImageView iv = (ImageView) v.findViewById(R.id.icon_image); though (as I mention in my code example).

0

精彩评论

暂无评论...
验证码 换一张
取 消