ListFragment selector selects all items
-
01-06-2021 - |
سؤال
I have this ListFragment, which I am calling in my Activity this way:
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setTitle( getString(R.string.something) );
if ( savedInstanceState == null ) {
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
final Fragment f = CustomerListFragment.newInstance();
ft.add( android.R.id.content, f, FRAG_TAG_CUSTOMER_LIST );
ft.commit();
}
}
On my Fragment I have setted single choice:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
In my manifest I have setted it a style:
<activity android:name=".ui.CustomerListActivity" android:clearTaskOnLaunch="true" android:theme="@style/Theme.AppList">
The theme is defined as follow:
<style name="Theme.AppList">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@color/window_background</item>
<item name="android:listViewStyle">@style/AppListView</item>
</style>
<style name="AppListView" parent="@android:style/Widget.ListView.White">
<item name="android:dividerHeight">4dp</item>
<item name="android:listSelector">@color/selector_color</item>
<item name="android:ellipsize">marquee</item>
<item name="android:cacheColorHint">@color/list_item</item>
<item name="android:divider">@android:color/transparent</item>
</style>
and the colors:
<color name="list_item">@android:color/white</color>
<color name="selector_color">#ffba00</color>
But, whenever the app runs, it looks fine until I long press an item or try to drag. Then all the elements get selected and looks like this, instead of only selecting one element.
So the question is: How do I make the list to select only 1 element instead of all of them.
Any help would be much appreciated.
المحلول 2
well the solution to this was to implement a selector xml, where I added all the states of the list item:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/bg_list_item_selected"></item> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/bg_list_item_selected"/> <!-- focused -->
<item android:drawable="@drawable/bg_list_item"/> <!-- default -->
</selector>
the selector drawable states where 9 patch images. then on the XML where I defined the row I setted the layout backgroud as the selector as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:paddingLeft="@dimen/space_screen_borders"
android:paddingRight="@dimen/space_screen_borders"
android:background="@drawable/custom_selector"
>
... your layout items
</LinearLayout>
the manifest activity as follows:
<activity android:name=".ui.CustomerListActivity" android:clearTaskOnLaunch="true" android:theme="@style/Theme.AppList">
and finally the theme as follows:
<style name="Theme.AppList">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@color/window_background</item>
<item name="android:listViewStyle">@style/AppListView</item>
</style>
<style name="AppListView" parent="@android:style/Widget.ListView.White">
<item name="android:dividerHeight">4dp</item>
<!--item name="android:background">@color/</item-->
<item name="android:cacheColorHint">@color/list_item</item>
<item name="android:divider">@android:color/transparent</item>
</style>
and then the listview would show as intended with a white background for the elements and blue background for the activity and a 4dp transparent space between elements.
نصائح أخرى
You'll want to make the listSelector @android:color/transparent
and set the individual row view's selector to @color/selector_color