简体   繁体   English

选择项目时更改“回收者”视图的前景色

[英]Changing foreground color of Recycler view when an item is selected

How to change foreground color of Recycler view when an item is selected ? 选择一个项目后如何更改回收站视图的前景色?

eg All other items except the selected item needs to be with different foreground color. 例如,除选定项目外的所有其他项目都必须具有不同的前景色。 Google allo助手

You can do it with drawable selector. 您可以使用可绘制的选择器来实现。 Put bg.xml in drawable folder. bg.xml放在drawable文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background_selected" android:state_selected="true"/>
    <item android:drawable="@color/background_unselected"/>
</selector>

Now set this instead of your background. 现在设置它而不是您的背景。

<TextView
    ....
    android:background="@drawable/bg"
    ...
    />

You just need to set selected in your adapter. 您只需要在适配器中设置选中状态即可。

textView.setSelected(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Recycler View 选择的项目颜色没有正确改变 - Recycler View selected item color not changing properly 如何设置“回收者视图”中所选项目的颜色? - how to set color the selected Item of Recycler View? 根据回收者视图中的选定项目更改编辑屏幕 - Changing edit screen according to selected item in recycler view 如何在Recycler视图中更改所选项目的文本颜色 - how to change the text color of selected item inside recycler view 如何更改所选项目的颜色以及使用 Firebase Recycler Adapter 时? - How to change the color of selected item and when using Firebase Recycler Adapter? 检查回收站视图中的项目是否已选中 - Check if the item in recycler view selected 回收商查看中心选定的项目 - recycler view center selected item 选择时如何将项目设置为回收站视图的中心 - How to set item to center of Recycler view when selected 在回收站视图中根据商品名称更改imageview时滞后 - Lag when changing imageview based on item's name in recycler view 如何将选定的回收站视图项目与更改背景颜色的第一个项目交换 - How to swap the selected recycler view item with the first item with change background color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM