Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

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

Posted on November 11, 2022 By

Solution 1 :

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.

Solution 2 :

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
    }

}

Problem :

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>

Comments

Comment posted by REX

Can you add your recycler view item xml code (i.e your custom_list.xml code) ?

READ  [FIXED] android - How to test that an intent was sent from a fragment using IntentsTestRule and launchFragmentInContainer
Powered by Inline Related Posts

Comment posted by Ãssãssîñ

I just added it please check it

Comment posted by REX

This is a better approach if you want to track your item selection.

Comment posted by Ãssãssîñ

I did what you told me, it works but when i scroll down i found another item its layout background has been changed like when i press the first item it changes its background but the seventh item changes too , I dont want it to be like that i want every item to be pressed to change thier background

Comment posted by Ãssãssîñ

Thanks to you too i found the solution

Android Tags:android-recyclerview, java

Post navigation

Previous Post: [FIXED] android – Please help, after flutter update, (The name ‘MenuItem’ is defined in the libraries) in visual studio
Next Post: [FIXED] react-native: How to remove underline while typing text in TextInput Component on android device?

Related Posts

[FIXED] java – Is there a way to move recycledView item to the bottom when click on it? Android
[FIXED] android – How to override xml view (that use data Binding) from Library in my project (# Override_Views_Data_Binding_Android ) Android
[FIXED] Set backgrounds for Android groups differently – Android Android
[FIXED] flutter – How do I run the debugger in Android Studio in order to figure out this error? Android
[FIXED] android – Combining Firebase Authentication with Realtime Database Android
[FIXED] How to fix Recyclerview returns same items android Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme