简体   繁体   English

在Android的GridView中更改图像的背景资源

[英]change background resource of an image in the gridview in android

here am trying to change the background resource for the image, am success in that, but the problem is, when i clicking on all the items in the gridview every item has changes in the background resource, my question is if i click on item which is 0 position it has to change the background resource image, and again if i click on the item which is in 1 position it has to change the background resource and as well as a image on 0 position has to set normal image like without background resource comes to normal state. 这里我试图更改图像的背景资源,是否成功,但是问题是,当我单击gridview中的所有项目时,每个项目的背景资源都有变化,我的问题是,如果我单击哪个项目是0位置,它必须更改背景资源图像,再次,如果我单击处于1位置的项目,则它必须更改背景资源,以及位于0位置的图像必须设置普通图像,例如没有背景资源进入正常状态。

public void onItemClick(AdapterView<?> arg0, View vv, int arg2, long arg3) {
        // TODO Auto-generated method stub

        vv.setBackgroundResource(android.R.drawable.btn_default);


    } 

Provide me to set only one selected item has to change the image. 请允许我设置仅一个选定项目即可更改图像。 Thanks in advance. 提前致谢。

You need modify your adapter to set background for checked items. 您需要修改适配器以设置选中项目的背景。 For Example: 例如:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // creating view

    if(item.isChecked()){
        veiw.setBackgroundResource(android.R.drawable.btn_default);
    }

    return view;
}

You actually need to modify the Adapter and for that i will refer you to this question here How to set an image as background image on a click? 您实际上需要修改适配器,为此,我将在此处您介绍此问题。 如何在单击时将图像设置为背景图像?

Check the accepted answer and do it as described and you are good to go 检查接受的答案并按照说明进行操作,您一切顺利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM