简体   繁体   English

颜色状态列表不适用于RecyclerView TextView

[英]Color statelist not working on recyclerview textview

I set up a color statelist like so: 我像这样设置颜色状态列表:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/white_25percent_opacity" android:state_selected="true"/>
    <item android:color="@color/white_25percent_opacity" android:state_pressed="true"/>
    <item android:color="@color/white_25percent_opacity" android:state_focused="true"/>
    <item android:color="@android:color/white"/>

</selector>

Then I tried to set it in the xml for a recyclerview item like so: 然后,我尝试在xml中为recyclerview项设置它,如下所示:

<TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:paddingLeft="10dp"
        android:paddingStart="10dp"
        android:paddingRight="10dp"
        android:paddingEnd="10dp"
        android:textColor="@color/mySelector"
        />

but it doesnt work - the color doesnt change when pressed. 但它不起作用-按下时颜色不会改变。 So I tried to set it programmatically in onBindViewHolder like this: 所以我试图像这样在onBindViewHolder中以编程方式设置它:

viewHolder.myTextView.setTextColor(ContextCompat.getColorStateList(context, R.color.mySelector));

and I also tried like this: 而且我也这样尝试过:

viewHolder.myTextView.setTextColor(ContextCompat.getColor(context, R.color.mySelector));

which also don't work. 这也不起作用。 Where is the mistake here and why doesn't this work for recyclerviews? 这里的错误在哪里,为什么对recyclerviews不起作用? To clarify - the text is shown in the initial color (white) but doesn't change to the pressed color. 为了澄清-文本以初始颜色(白色)显示,但不会更改为按下的颜色。

Edit: also tried to solve it by making the selector a drawable - but it didn't work: 编辑:还尝试通过使选择器成为可绘制的方法来解决它-但它不起作用:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/white_25percent_opacity" android:state_selected="true"/>
    <item android:drawable="@color/white_25percent_opacity" android:state_pressed="true"/>
    <item android:drawable="@color/white_25percent_opacity" android:state_focused="true"/>
    <item android:drawable="@android:color/white"/>

</selector>

If I set an ontouchlistener and switch the colors manually then it works properly - but I want to do this with a statelist. 如果我设置了一个ontouchlistener并手动切换了颜色,则它可以正常工作-但我想使用状态列表来做到这一点。

Hi Implement your XML like the below code , Hope it would be helpful may be the issue is you have given both state_pressed and state_selected = true. 嗨,像下面的代码一样实现XML,希望这对您有帮助,可能是因为您同时指定了state_pressed和state_selected = true。

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Color when the row is selected -->
    <item android:drawable="@android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
    <!-- Standard background color -->
    <item android:drawable="@android:color/white" android:state_selected="false" />
</selector>

事实证明,我需要在textview上进行设置以使其起作用:

android:clickable="true"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM