How to show a custom shape as background behind the selected item inside a recyclerview. I've tried several things but all failed:
activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/nav_drawer_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/drawer_background"
android:theme="@style/drawer"></android.support.v7.widget.RecyclerView>
</LinearLayout>
drawer_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/drawable_shape_selected" android:state_checked="true"></item>
<item android:drawable="@android:color/transparent"></item>
</selector>
drawable_shape_selected.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFB74D"></solid>
</shape>
custom_layout.xml:For RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/TextViewDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
</LinearLayout>
Update:
itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Toast.makeText(context, "Position " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});