-2

I have one recyclerview recyclerview row contains two textviews

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:elevation="2dp"
android:id="@+id/l1"
android:orientation="horizontal">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:layout_marginBottom="@dimen/margin_12"
    android:layout_marginLeft="@dimen/margin_22"
    android:layout_marginRight="@dimen/margin_22"
    android:layout_marginTop="@dimen/margin_12"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fdgdfgfdgfdg"
        android:textAlignment="center"
        android:textColor="@color/color"
        android:textSize="@dimen/text_size_16" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">



        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="jh"
            android:textColor="@color/color"
            android:textSize="@dimen/text_size_9" />
    </LinearLayout>

</LinearLayout>

What i want is to change the textcolor of clicked item to red. and other items to black

What i am doing in Adapter is

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView tv_1, tv_2;
    LinearLayout l1 ;

    /*package */ MyViewHolder(final View itemView) {
        super(itemView);
        l1 = itemView.findViewById(R.id.l1) ;
        tv1 = itemView.findViewById(R.id.tv1);
        tv2 = itemView.findViewById(R.id.tv2);
        tv1.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        tv1 = itemView.findViewById(R.id.tv1);
        tv1.setTextColor(Color.parseColor("#d30c20"));
        tv2.setTextColor(Color.parseColor("#d30c20"));
        String id = tv1.getText().toString();
        mItemClickListener.onItemClick(view, getAdapterPosition(), id);
    }
}

this works fine but when i click other item i want to reset item color of previousy selected item to its original color.

In short i want to change the color of both selected item and unselected item every time

how to do this ???

Pranita
  • 803
  • 7
  • 16
  • 1
    The code you have added is not `Adapter` its `RecyclerView.ViewHolder` . You need to change the color in `onBindViewHolder()` of Adapater's. Here is the [Reference](https://stackoverflow.com/questions/16976431/change-background-color-of-selected-item-on-a-listview) you can follow. – ADM Feb 14 '18 at 06:28
  • take one boolean for first tym and check in condition selected or not – Vidhi Dave Feb 14 '18 at 06:29
  • it means you want to change the color of the selected item to Red and rest of all should be black. – Farhana Naaz Ansari Feb 14 '18 at 06:31
  • Hi, Use Arraylist of a bean class which hold property of item in the list. Update property of bean class based on clicked position and notifydata – Sanjay Kumar Feb 14 '18 at 06:40
  • Refer this link https://stackoverflow.com/questions/40692214/changing-background-color-of-selected-item-in-recyclerview – Baskaran Veerabathiran Feb 14 '18 at 06:42

2 Answers2

0

Add this to adapter;

int selectedItemPosition;
int RED_TEXT_VIEW = 100;
int BLACK_TEXT_VIEW = 101;

Every time onClick() event is triggered set the selectedItemPosition variable to clicked position. Also call notifyDataSetChanged(). Override getItemViewType() to return RED_TEXT_VIEW if position is selectedItemPosition, otherwise return BLACK_TEXT_VIEW.

When you get the viewType in onCreateViewHolder(), return appropriate views according to the viewType which should be RED_TEXT_VIEW or BLACK_TEXT_VIEW

josh_gom3z
  • 294
  • 1
  • 13
0

changing all the items to original color, before changing the clicked item to selected color will work, but it's not the best by any means. I would go for something else, like this one:

define an ArrayList in your adapter:

ArrayList<Integer> changedItems = null;

then after your change any item to your prefered color, make sure you track the item position by adding it to the ArrayList:

changedItems.add(itemPosition);

then, before setting color to the item, check if there are changed items and restore them:

if(!changedItems.isEmpty()){
            for (Integer changedItem : changedItems) {
                // restore to original color
            }
        }
shervinox
  • 166
  • 1
  • 11