Skip to content Skip to sidebar Skip to footer

LAndroid ListView OnListItemClick API 8 Highlight Row Moving To A Row

As I am working on a project where it needs to work with API 8 I found a lot of examples on how to highlight a row would not work as they need minimum API 10 upwards. So I have lo

Solution 1:

Your approach is wrong there is some thing called selector which works on all the api version almost and I have did it on API level 8 as well there is the example try this

Here's Adapter Layout

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@drawable/rowselector" >

   . . . . . . 

</RelativeLayout>

you can see I am setting background which actually is a selector

My rowselector (keeping this in drawable)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@drawable/left_navbg_hover" />
    <item
     android:drawable="@drawable/left_navbg" />
</selector>

where left_navbg_hover and left_navbg is an Image I have used .

Try that , all the best !


Solution 2:

if you are using custom list adaptor than you can do one thing:

create one public method in your adaptor class like :

public void setSelectedPosition( int pos )
    {
        selectedPos = pos; // selectedPos is global variable to handle clicked position
        // inform the view of this change
        notifyDataSetChanged();
    }

In getView method change your backgroundcolor:

 if ( selectedPos == position )
    {
            ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
    }

onItemClick method call adaptor method:

@Override
    public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 )
    {
        int position = arg2;

        youradapter.setSelectedPosition( position );

    }

Post a Comment for "LAndroid ListView OnListItemClick API 8 Highlight Row Moving To A Row"