tareqbinhariz
tareqbinhariz

Reputation: 3

Changing the background of selected items in recyclerView Android (Java)

I have a linearlayout in my recyclerView and i waant to implement on click event on every item, when i select the first item should change the background of the linearlayout of the first item and when i pressed it again returns the first background , i just want to select multiple items not only one can anyone help me with this!!!!

here is my adapter code

public class MyAdapter extends 
RecyclerView.Adapter<MyAdapter.MyViewHolder> {


String[] adkr;
    String[] repeat;
        Animation anim;
            Context co;

public MyAdapter(Context context, String[] all_adkars, String[] all_repeat) {

    co = context;
        adkr = all_adkars;
            repeat = all_repeat;

}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = LayoutInflater.from(co);
        View view = layoutInflater.inflate(R.layout.custom_list, parent, false);

    return new MyViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    holder.textView.setText(adkr[position]);
        holder.repeat_Tv.setText(repeat[position]);
            setAnimation(holder.itemView,position);

}

@Override
public int getItemCount() {
    return adkr.length;
}


public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textView, repeat_Tv;
        LinearLayout layout;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.textView);
            repeat_Tv = itemView.findViewById(R.id.repeat);
                layout = itemView.findViewById(R.id.ll_bg);

    }


}

private void setAnimation(View viewToAnimate, int position) {


    anim = AnimationUtils.loadAnimation(co, R.anim.list_anim);
    viewToAnimate.startAnimation(anim);


}

}

Here is my custom list xml code

 <?xml version="1.0" encoding="utf-8"?> 
 <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingBottom="20dp"
 android:id="@+id/rl_item"
>



<androidx.cardview.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/ll_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient"
        android:orientation="vertical"
        android:padding="10dp">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/shorooq"
            android:textAlignment="center"
            android:layout_margin="5dp"
            android:textColor="#CCFFFFFF"
            android:textSize="20sp"
     />

 <Button
 android:layout_width="match_parent"
 android:layout_height="2dp"
 android:background="@drawable/repeat"
 android:layout_marginTop="15dp"
 />

        <TextView
            android:id="@+id/repeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/shorooq"
            android:textAlignment="center"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:textColor="#BFBB8763"
            android:textSize="25sp"
       />



    </LinearLayout>
  </androidx.cardview.widget.CardView>
  </androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 149

Answers (2)

eimmer
eimmer

Reputation: 1709

In MyAdapter, add a field for selected items, since adkr is an array of strings:

Set<String> selectedItems = new HashSet();

Then as @REX says, add an onClick listener to the MyViewHolder but I would alter the approach. I would add a bind method, so it looks like this.

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textView, repeat_Tv;
        LinearLayout layout;
        int position;
        String value;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.textView);
            repeat_Tv = itemView.findViewById(R.id.repeat);
            layout = itemView.findViewById(R.id.ll_bg);
        }

        public void bind(String value, int position){
            textView.setText(value);
            repeat_Tv.setText(value);
            if(selected.contains(value)){
                // background for selected
            } else {
                // background for unselected
            }
            this.value = value;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if(selected.contains(value)){
                selected.remove(value);
            } else {
                selected.add(value);
            }
            notifyItemChanged(position);
        }
    }

Bind it like this:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.bind(adkr[position], position);
}

This will allow you to track what is selected and show it appropriately.

Upvotes: 1

REX
REX

Reputation: 149

You can add clicklistener in your viewholder which will detect the on click events. code changes will be something like this:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textView, repeat_Tv;
    LinearLayout layout;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.textView);
        repeat_Tv = itemView.findViewById(R.id.repeat);
        layout = itemView.findViewById(R.id.ll_bg);
        layout.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        // change background of layout here
    }

}

Upvotes: 0

Related Questions