Skip to content Skip to sidebar Skip to footer

Listfragment Selected Item

i have a ListView on a ListFragment that use SimpleCursorAdapter to generate my list. i wanted to highlight selected item in my ListView, i've tried : v.setBackgroundResource(R.col

Solution 1:

Try this way if you want to highlight selected item in Listview.

This works for me.

First set Adapter in Listfragment before use setSelector(..).

setListAdapter(mAdapter);
 getListView().setSelector(R.drawable.fragment_listselector);      

fragment_listselector.xml

<selectorxmlns:android="http://schemas.android.com/apk/res/android"android:exitFadeDuration="@android:integer/config_mediumAnimTime"><itemandroid:state_activated="true"android:drawable="@drawable/list_item_active_pattern"  /><itemandroid:drawable="@drawable/list_bg_pattern" /></selector>

when onItemClick(..) is called put this code.

@Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {   
            getListView().setItemChecked(position, true);
            getListView().setSelection(position);
            getListView().setSelected(true);

    }

Post a Comment for "Listfragment Selected Item"