Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

android - Cannot highlight items in RecyclerView with selectors

I want simply highlight item when I press on it.

I have selectors below (@drawable/expandable_menu)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@color/purple_200" android:state_pressed="true" />
<item android:drawable="@color/purple_500" android:state_selected="true" />
<item android:drawable="@color/chat" android:state_activated="true" />
<item android:drawable="@color/transparent"/>
</selector>

In my onclick listener I select this item

RecyclerView XML:

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="70dp"
        android:id="@+id/chat_messages"
        app:stackFromEnd="true"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:background="@drawable/expandable_menu"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:focusable="true">

Message XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:layout_gravity="left"
>

<TextView
    android:id="@+id/chat_msg_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="this is me"
    android:textColor="@color/black"
    android:textSize="16dp"
    android:background="@color/cardview_shadow_start_color"
    android:padding="10dp"
    android:layout_alignParentTop="true" />
 </RelativeLayout>

I followed this answer (https://stackoverflow.com/a/30046476) but no success

question from:https://stackoverflow.com/questions/65882091/cannot-highlight-items-in-recyclerview-with-selectors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you have to set OnClickListener to View, which have selector set as background. I see you have selector set for TextView (child), are you setting onClick to this View or for whole itemView (which in your case is root RelativeLayout)?

you may also try to add android:duplicateParentState for TextView - this should make TextView draw pressed state when parent is pressed, not only TextView itself (which isn't clickable at all when no OnClickListener set)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...