简体   繁体   中英

listview selection color in android

in my listview, when user long press on a item, i draw a custom actionbar and provide user option to delete multiple items at a time.

by default if i perform long press action, i will get selection color as blue and it gets disappear.

To overcome i tried adding a selector like this.

listviewselector.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@color/RED"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@color/BLUE" /> 

</selector> 

If i set this selector, when user performs long press i can see red color, but after wards if user performs selection, no color is getting retained on item. By default it looks white.

I tried setting background color based on condition like below

   if(mSelectedItemsIds.get(key))
        {
            convertView.setBackgroundColor(REDCOLOR);
        }
        else
        {
            convertView.setBackgroundColor(WHITE);
        }

In this case, if user performs long press and then if user select multiple items i could see red color and by default all the items color will be white. But if user touches any item the default color will be nothing ie no color appears on selection.

How to get default white color, on tap blue color and upon multiple selection red color?

i tried like this New selector:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@color/RED"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@color/WHITE" /> 

</selector> 



 if(mSelectedItemsIds.get(key))
            {
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.RED));
            }
            else
            {
                convertView.setBackgroundColor(R.drawable.listviewselector);
            }

In this i get by default all items blue color. why?

i tried like this, it worked.

if(mSelectedItemsIds.get(key))
        {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.BLUE));
        }
        else
        {
            convertView.setBackgroundColor(android.R.drawable.list_selector_background);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM