Why does calling setBackgroundColor in a view interrupt its long-lasting color change and selection?

In Android, by default, when you click on a list item for a long time, it goes from the highlight color to white, indicating that the user is holding it and the contextual item is displayed. Alternatively, you can use the trackball (or the arrow buttons on some phones) to select list items and then use your fingers.

However, I do have a ListView whose element views I call setBackgroundColor, and both of these expected behaviors no longer work. Does anyone know why this is so and how to fix it?

Notes. Setting the background color in xml is not an option, because I need to be able to set / change the color dynamically.

The code for my newView function is as follows:

@Override public View newView(Context ctx, Cursor cursor, ViewGroup parent) { View view = new View(mCtx); final LayoutInflater li = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = li.inflate(R.layout.task_list_item, null); return view; } 
+4
source share
5 answers

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(); } } 
+9
source

When you set the background color, you override the default background, which seems like a color animation. If you need a different color, you need to create an animation with the desired color.

+5
source

You want to create a Selector that deals with the selected state.

Since you cannot use the XML solution, you want to programmatically create a StateListDrawable and set it using ListView.setSelector .

+3
source

This probably happens because you just set a single color to the layout .. where by default it has something like stateList and it has different colors for different states (click, focus, lingClicked), etc ... .you can create one and use it to set backGround ...

+1
source

I have a one line solution, if you use ListView (it probably works on all views), in your ListView code add android:drawSelectorOnTop="true" .

For instance:

 <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listID" android:drawSelectorOnTop="true" ... /> 
0
source

All Articles