ListView item of the selected item

I have a problem in my ListView. I want to change the background of the selected item when I select it for a custom drawable that I have, but it does not work.

I want this effect, as in the image, to be selected.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" 
          android:drawable="@drawable/selected_item"/>
    <item android:drawable="@android:color/transparent" />
</selector>

And this is the text list_row

<!-- this layout is used to view row of category list and resturant list  -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_category_row" 
    android:gravity="center"
    android:layout_width="match_parent" 
    android:textColor="@color/BLACK"
    android:layout_height="55dp" 
    android:padding="10dp"
    android:text="@string/hello" 
    android:layout_marginTop="5dp"

    android:textAppearance="?android:attr/textAppearanceLarge" />

And this is a ListView code.xml:

<ListView android:id="@+id/list_category" 
    android:layout_alignParentLeft="true" 
    android:layout_height="fill_parent" 
    android:layout_width="184dp" 
    android:dividerHeight="2px"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:layout_marginTop="5dp"  
    android:listSelector="@drawable/list_selector"
    android:divider="@color/Gray"  >
+3
source share
4 answers

In touch mode, there is no selected or focused state.

However, you can have a checked state (even without a check box) and use it to change properties when "selected". In your java code, where you set up displaying your list, add this line after the list is defined:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then in your list of XML states, change this:

android:state_selected="true"

:

android:state_activated="true"

So now you should have:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_activated="true"  
        android:drawable="@drawable/selected_item"/> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

, , :

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/txt_category_row"  
    android:gravity="center" 
    android:layout_width="match_parent"  
    android:textColor="@color/BLACK" 
    android:layout_height="55dp"  
    android:padding="10dp" 
    android:text="@string/hello"  
    android:layout_marginTop="5dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:background="@drawable/item_selector" /> 

XML- , "item_selector". .

+10

CheckableFrameLayout . - CheckableFrameLayout

, , , .

+1
dynamicListView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       //arg1 will give you that view   
       arg1.setBackgroundColor(Color.BLUE); 
    }
});
-2
source

All Articles