Tell me how to properly highlight the clicked item in RecycleView, so that after clicking the selection disappears and some action is performed. Now the list is built and there is a separate color for each RecycleView item, a selector is installed for the RecycleView itemrow (maybe it is not needed).

android:background="@drawable/selector_recycleview" 

Adapter written:

 @Override public void onBindViewHolder(final UslugiAdapter.MyViewHolder holder, final int position) { holder.rl2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { holder.rl1.setBackgroundColor(helper.getColor(mContext,R.color.lightGray)); holder.rl2.setBackgroundColor(helper.getColor(mContext,R.color.lightGray)); } }); holder.tv_name.setText(entriesList.get(position).getFirst()); holder.tv_desc.setText(entriesList.get(position).getSecond()); if (position==1){ holder.rl1.setBackgroundColor(helper.getColor(mContext,R.color.colorGRAYSer1)); holder.rl2.setBackgroundColor(helper.getColor(mContext,R.color.colorGRAYSer2)); Picasso.with(mContext).load(helper.getDrawableName(mContext,entriesList.get(position).getThird())).into(holder.iv_logo); } if (position==2){ holder.rl1.setBackgroundColor(helper.getColor(mContext,R.color.colorBlueSer1)); holder.rl2.setBackgroundColor(helper.getColor(mContext,R.color.colorBlueSer2)); Picasso.with(mContext).load(helper.getDrawableName(mContext,entriesList.get(position).getThird())).into(holder.iv_logo); } 

}

Made a selection through holder.rl2.setOnClickListener, but it does not disappear after clicking. Then I started to rake some rakes ... Most likely I’m missing something. Please help! EDIT selector_recycleview

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="@color/lightGray" /> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="@android:color/transparent" /> </shape> </item> 

Item Marking

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:clickable="true" android:background="@drawable/selector_recycleview" > <RelativeLayout android:layout_width="match_parent" android:layout_height="101dp" > <View android:id="@+id/someview555" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorLightBlue" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <TableRow android:layout_width="match_parent" android:layout_height="100dp" android:layout_above="@+id/someview555" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/linear1" android:layout_weight="1" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/tow_white70" android:id="@+id/imageView21" android:layout_weight="1" android:padding="15dp" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:id="@+id/linear2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.4" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView49" android:layout_weight="1" android:textColor="@color/colorWhite" android:textSize="18sp" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:paddingLeft="15dp" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView52" android:layout_below="@+id/textView49" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textColor="@color/colorWhite" android:paddingLeft="15dp" /> </RelativeLayout> </LinearLayout> </TableRow> </RelativeLayout> 

    1 answer 1

    Just make the background-drawable normal, and not manually change colors,

    eg:

     <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@color/lightGray" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@color/lightGray" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/lightGray" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/lightGray" /> <item android:state_focused="true" android:drawable="@color/lightGray" /> <item android:drawable="@color/transparent" /> </selector> 
    • I specified my normal background-drawable in the question. This is a wrong decision. Where I manually specify the colors, this is the way it should be, otherwise there is no way to achieve the established colors for the RecycleView Item. - Ivan Vovk
    • item layout in studio - georgehardcore
    • change the color of the child's RelativeLayout a background leave it at the guy - georgehardcore
    • Tried and so, too, does not help! - Ivan Vovk
    • then it becomes unclear what exactly you want to achieve - georgehardcore