After changing my code around (first attempt) I am having a similar problem. I have updated my getView() to execute the proper way.
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
Resources res = activity.getResources();
if( convertView == null ) {
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate( R.layout.video_gallery_item, parent, false );
}
Bitmap bmp = getBitmap( videoIds[ position ] );
/* This layer drawable will create a border around the image. This works */
Drawable[] layers = new Drawable[ 2 ];
layers[ 0 ] = new BitmapDrawable( res, bmp );
layers[ 1 ] = res.getDrawable( R.drawable.border_selected );
LayerDrawable layerDrawable = new LayerDrawable( layers );
/* Create the StateListDrawable */
StateListDrawable drawable = new StateListDrawable();
drawable.addState( StateSet.WILD_CARD, new BitmapDrawable( res, bmp ) );
drawable.addState( new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable );
ImageView v = (ImageView)convertView.findViewById( R.id.image );
v.setImageDrawable( drawable );
v.setAdjustViewBounds( true );
thumbnails[ position ] = bmp;
return convertView;
}
This adapter is being use on a GridView called videoGallery:
videoGallery.setChoiceMode( GridView.CHOICE_MODE_MULTIPLE_MODAL );
videoGallery.setMultiChoiceModeListener( new MultiChoiceModeListener() { ... }
The problem I am having is that the images do not change when they are selected on the GridView via long click. The action bar changes, my contextual menu appears, etc. I have also tried creating the StateListDrawable via XML with the same results. Thoughts?
UPDATE
Changing
drawable.addState( new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable );
to
drawable.addState( StateSet.WILD_CARD, layerDrawable );
in my getView() shows the border I am looking for. So maybe the StateListDrawable isn’t getting the state change? Anybody have any thoughts?
Well I figured it out. There were a few problems with my states. First in the order:
In the first
addStateI am using the wildcard. Because of this android would match this state before the one following that. Also, despite android saying the item is “checked” the actual state is “activated”. So I changed the two lines to:And it is working perfectly!