Android 向RecyclerVIew中的选定项目添加自定义形状
如何在recyclerview中将自定义形状显示为选定项目后面的背景。我尝试过几种方法,但都失败了: 活动\u main.xml:Android 向RecyclerVIew中的选定项目添加自定义形状,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,如何在recyclerview中将自定义形状显示为选定项目后面的背景。我尝试过几种方法,但都失败了: 活动\u main.xml: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androi
<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>
<?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>
<?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>
像这样更改您的
drawer\u 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_pressed="true" />
<item android:drawable="@drawable/drawable_shape_selected"
android:state_focused="true" />
<item android:drawable="@android:color/transparent" />
</selector>
谢谢如果我想在这个形状后面添加阴影呢。你最欢迎的..阴影可以用xml添加,也可以通过创建自定义类..查看此链接以获取答案。。。兄弟此focusableInTouchMode已锁定recyclerView first click,它仅在第二次clickbro时有效更新代码..立即检查..这将解决您的问题:)将其添加到您的viewholder中
itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Toast.makeText(context, "Position " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
<?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_pressed="true" />
<item android:drawable="@drawable/drawable_shape_selected"
android:state_focused="true" />
<item android:drawable="@android:color/transparent" />
</selector>
Button button = view.findViewById(R.id.buttonPanel2);
button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focus) {
if(focus){
view.performClick();
}
}
});