ListFragment Selected Item

前端 未结 1 1689
臣服心动
臣服心动 2021-01-03 10:44

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.set         


        
相关标签:
1条回答
  • 2021-01-03 11:34

    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

    <selector xmlns:android="http://schemas.android.com/apk/res/android"
            android:exitFadeDuration="@android:integer/config_mediumAnimTime">
        <item android:state_activated="true"
                android:drawable="@drawable/list_item_active_pattern"  />
        <item  android: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);
    
        }
    
    0 讨论(0)
提交回复
热议问题