سؤال

I have a Listview generated using SimpleAdapter. I want to highlight my row with a color on clicking it and want to keep it highlighted till the other row is clicked. I have done the following but it doesn't help.I am new to this so please guide me step by step. My codes are as follows:

Selector sel.xml in drawable folder

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

<item android:drawable="@android:color/black" android:state_activated="false"/>
<item android:drawable="@android:color/black" android:state_activated="false" android:state_pressed="false"/>
<item android:drawable="@android:color/black" android:state_pressed="true"/>
<item android:drawable="@android:color/black" android:state_activated="true"/>

 </selector>

listview xml

  <com.fortysevendeg.swipelistview.SwipeListView
        android:id="@+id/swipy"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        swipe:swipeFrontView="@+id/front"
        swipe:swipeBackView="@+id/back"

      android:listSelector="@color/blue2"
        swipe:swipeMode="right"
        />
هل كانت مفيدة؟

المحلول

use

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) 

and selector will be

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

        <item android:drawable="@android:color/black" android:state_activated="false"/>
        <item android:drawable="@android:color/black" android:state_activated="false" android:state_pressed="false"/>
        <item android:drawable="@android:color/black" android:state_pressed="true"/>
        <item android:drawable="@android:color/black" android:state_activated="true"/>

    </selector>

your listview

<ListView
    android:id="@+id/my_list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="true"
    android:fadeScrollbars="true"
    android:fastScrollEnabled="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollbarFadeDuration="100"
    android:scrollbars="vertical" />

نصائح أخرى

Example :

<ListView
        android:id="@+id/listview"   

         android:dividerHeight="3dp" 
         android:divider="#000000"
         android:cacheColorHint="#05806D"
         android:listSelector="#808080"
         android:choiceMode="singleChoice"
         android:text="No Details found"
         android:layout_width="fill_parent"
         android:layout_height="match_parent" >

and you can use different color as yours requirement in android:listSelector="@drawable/list_selector" or android:listSelector="Your color code"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top