ListFragment所选项目所选、项目、ListFragment

2023-09-05 09:10:05 作者:≈半死不活ヅ

我在使用SimpleCursorAdapter生成我的列表中ListFragment一个ListView。我想强调我的ListView选中的项目,我已经试过:

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.color.listselect_light_blue);

在onListItemClick,但它的工作原理奇数的情况下选择两排,当我点击的项目之一,我希望它是单一的,我还设置选择模式

in onListItemClick but it works odd and it selects two row when i click one of items and i want it to be single , i also set Choice Mode

<ListView android:id="@id/android:list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" 
           android:choiceMode="singleChoice"   
           android:cacheColorHint="#00000000"
            />

我已经试过ListSelector但是当我点击一个项目并不突出,直到我滚动列表,并打开高亮显示。

i've tried ListSelector but when i click on an item it doesn't highlight until i scroll the list and it turn highlighted.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getListView().setSelector(R.drawable.listview_selector);

}

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:drawable="@color/listselect_light_blue" />
</selector>

任何帮助,将AP preciated

any help would be appreciated

推荐答案

如果你想突出列表视图中选择项目尝试这种方式

这对我的作品。

在使用setSelector(..)。第一套适配器Listfragment

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>

在onItemClick(..)调用把这个code。

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);

    }