By default, a ListView has a Selector that will play a TransitionDrawable when you delete a list item. However, if your view of the list item has a solid background, you wonβt be able to see the animation of the long press of the selector (or in any other states) because it is covered by the background of the list of items.
If you want to see the longpress selector animation / selected / pressed state, then the list item should have a transparent background when the item is selected / pressed / pressed. You can do this using StateListDrawable as the background of an element instead of a solid color.
Here is an example StateListDrawable for this purpose:
public class ColorfulListItemDrawable extends StateListDrawable { private final PaintDrawable mColor; public ColorfulListItemDrawable(int color) { mColor = new PaintDrawable(color); initialize(); } private void initialize() { Drawable color = mColor; Drawable selected = new ColorDrawable(Color.TRANSPARENT); addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled, android.R.attr.state_window_focused}, selected); addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled, android.R.attr.state_window_focused,android.R.attr.state_selected}, selected); addState(new int[] {android.R.attr.state_enabled,android.R.attr.state_window_focused, android.R.attr.state_selected}, selected); addState(new int[] {}, color); } public void setColor(int color) { mColor.getPaint().setColor(color); mColor.invalidateSelf(); } }
source share