0


I use a GalleryView to display string with a BaseAdapter
No problem to change the background and text color depending on the item state (selector)

But I want to change the background and text color only if the item is clicked not just selected, the GalleryView item state changes with the selected event which is called when you fling around (the centered item is automatically selected)

In 2 words the background mustn't change if the item isn't clicked
How can I go with it ?

Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31

2 Answers2

2

You must capture the selected event and change the background of the view to unselected. Store last clicked position to avoid changing its selected background. Also, store last clicked view to restore unselected background when another item is clicked without flinging:

    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new DaysGalleryAdapter(this, new int[]{R.layout.gallery_item}, new int[]{R.id.tv_gallery}));
    gallery.setOnItemClickListener(mOnGalleryClick);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               if(position != savedClickedPosition) 
                   view.setBackgroundResource(R.drawable.bg_unselected);
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

           }

   }); 

   private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        lastItemSelected.setBackgroundResource(R.drawable.tab_bg_unselected);
        view.setBackgroundResource(R.drawable.tab_bg_selected);
        lastItemSelected = view;
        savedClickedPosition = position;
        // Do stuff
    }       
   };

If it's too late for you Fluckysan at least hope it helps to anyone who's having the same problem. Happy coding!

crubio
  • 6,394
  • 4
  • 30
  • 33
  • Yeah I end up doing that (poster my resolution on another thread : http://stackoverflow.com/questions/6580374/how-to-avoid-selector-while-dragging-image-in-gallery-in-android/6681005#6681005). It's not very sexy but it works. Thanks for you answer though :) – Plumillon Forge Aug 18 '11 at 08:27
  • Ok Fluckysan, I'm glad you found another solution. For others interested in my answer, you also need to use your adapter to change the background using the last clicked position. That helps a lot when the last clicked position disappears off the screen and comes back. I finally got it 100% working today. – crubio Aug 18 '11 at 12:37
  • Fluckysan, you should check an answer to close the issue :) – crubio Aug 22 '11 at 13:13
0

You must implement a list selector in you xml using the attribute android:listSelector and then set this style to your list view.

Shafi
  • 1,368
  • 10
  • 24
  • List selector change the background image when the selected item change, in the case of a Gallery this is not possible if you want the background change ONLY if the item is clicked – Plumillon Forge Aug 18 '11 at 08:29