如果单击新图像,则将先前选择的图像替换为原始图像

发布于 2024-11-08 14:22:56 字数 850 浏览 0 评论 0 原文

我正在使用带有 ImageAdapter 的网格视图来显示图像。 我有两组图像,其中 mThumbIds 包含原始图像,cThumbIds 包含所选图像。

现在,当我单击图像时,我会用所选图像更改普通图像。代码如下:

final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
            iv.setOnClickListener(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]);

但是当我单击另一张图像时,另一张图像也显示为选中状态,问题就出现了。正确的方法是显示所选的新图像并删除所选的旧图像。换句话说,旧图像应恢复为原始图像。

请帮助我

,谢谢,

Pankaj

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.setOnClickListener(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

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

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

发布评论

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

评论(2

メ斷腸人バ 2024-11-15 14:22:56

您需要创建一个变量并将单击的图像的 id 保存在其中。当用户单击其他图像时,首先根据变量中的 id 重置其他图像,然后将变量值替换为当前单击图像的 id。

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.

澜川若宁 2024-11-15 14:22:56

我假设您正在使用 ImageAdapter 类的修改副本="nofollow">本教程 并且您发布的代码位于该类的 getView(int,View,ViewGroup) 方法中。

您保存所选图像的索引,但不保存视图本身。您需要保存两者才能恢复旧图像,如下所示:

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);
}

我对行 ImageView iv = (ImageView) v.findViewById(R.id.icon_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).

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