how can I show selection of an image in grid view at once

后端 未结 3 580
慢半拍i
慢半拍i 2021-01-07 14:41

I have a question that I have a custom grid view in which two images one is the background image and second is the check mark image, when I clicked on one Item of grid view

3条回答
  •  一整个雨季
    2021-01-07 14:55

    Well, there is one simple way to do this. You can keep reference of previously selected item. e.g. you make a variable at class level, where you are using the adapter. Then in your onClick listener, you un-select that image and set that variable with the current view(current image). e.g.

    ImageView iv_selected = null;
    
    // ..........
    
    @Override
    public void onClick(View view) 
    {
         Log.i("Clicked", "Tag###########");
         if(iv_selected != null)
         {
              // unselect the image here
         }
         // set this variable again e.g.
         iv_selected = (ImageView)view;
    
    
         //img_select.setVisibility(View.INVISIBLE);
         img_select.setFocusable(true);
         img_select.setEnabled(true);
         if(checked==0)
         {
    
              img_select.setBackgroundResource(R.drawable.selectimage);
              GreetingTextContainer greet = GreetingTextContainer.getSingletonObject();
              greet.setPosition(position);
              checked =1;
         }
         else
         {
              img_select.setBackgroundResource(0);
              checked=0;
         }
    }
    

    in this you'll always have the refernce of the single selected ImageView. So, you can uncheck it when you select something else. I hope you got the idea.

提交回复
热议问题