mardi 12 mai 2015

setBackgroundResource with alpha shows prior color

I have a simple BaseAdapter that shows a list of options the user can toggle on or off. When the option is selected, I use an opaque color, and when it is off, I use a transparent background.

In prior versions of the OS (at least up to 4.4.4), this worked as expected. However, when I upgraded one of our test tablets to Android 5.1.1, I noticed that whenever I toggle an item off (and set the background resource to transparent), the 'selected' color bleeds through. It is not until I make another selection (on a different item) that the 'old' color goes away and the item is truly transparent.

Below is the snippet of code used to set the background resource. I have tried setting the resource to 0 (removing the background) and to transparent #00000000 to no avail.

 @Override @NotNull
 public View getView ( int position, @Nullable View view, @NotNull ViewGroup viewGroup )
 {
   final MapLayerModel mapLayer = (MapLayerModel)getItem( position );

   final Context context = viewGroup.getContext();
   final LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

   if( view == null )
   {
      view = inflater.inflate( R.layout.menu_list_item, null );
   }

   // Configure the view
   //

   // Set text view
   final TextView textView = (TextView)view.findViewById( R.id.menu_item_textview );
   textView.setText( mapLayer.getDisplayName() );

   // Set image
   final ImageView imageView = (ImageView)view.findViewById( R.id.menu_item_image );
   imageView.setImageResource( mapLayer.getIconResource() );

   final ImageView selectionIndicatorView = (ImageView)view.findViewById( R.id.selected_indicator );
   if( mapLayer.isSelected() )
   {
      Log.e( LOG_TAG, "Setting " + mapLayer.getType() + " to selected color" ) ;
      view.setBackgroundResource( R.color.layer_menu_item_selected );
      selectionIndicatorView.setImageResource( R.drawable.layer_menu_item_selected );
   }
   else
   {
      Log.e( LOG_TAG, "Setting " + mapLayer.getType() + " to transparent" ) ;
      view.setBackgroundResource( R.color.crc_transparent );
      selectionIndicatorView.setImageResource( R.drawable.layer_menu_item_unselected );
   }

   return view;
}

where R.color.crc_transparent is set to #00000000

Aucun commentaire:

Enregistrer un commentaire