Unfortunately, the only way I found is to have an extra look in your layout that will emulate the selected state. Here it is (it works on pre-Lollipop):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeight" android:background="?attr/selectableItemBackground"> <View android:id="@+id/item_selected" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/colorControlHighlight"/> <android.support.v7.widget.AppCompatTextView android:id="@+id/item_title" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:textAppearance="?attr/textAppearanceListItem"/> </FrameLayout>
Then, in your adapter, you set the item_selected visibility item_selected either View.VISIBLE or View.GONE , based on the need to make this specific item selected.
PS Obviously, the AppCompat support library is used in this solution, so to use it in the build.gradle file, build.gradle need to add the build.gradle line:
implementation 'com.android.support:appcompat-v7:28.0.0'
Alex semeniuk
source share