Change background image of a Recycler View Adapter after click

Asked

Viewed 385 times

1

The thing is, I created a Adapter amending a RecyclerView in an Activity I call through this function (within Activity):

 private void gerarDatasView(CalendarJur calendario){


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        listDatas.setLayoutManager(linearLayoutManager);

        ListDatasAdapter adapter = new ListDatasAdapter();
        adapter.setDateJur(calendario.getDatas());

        listDatas.setAdapter(adapter);

    }

The listDatas is the RecyclerView.

And it works perfectly according to the image:

inserir a descrição da imagem aqui

Each item in this list was created through onBindViewHolder in my Adapter like this:

public void onBindViewHolder(@NonNull final ViewListDatas holder, int position) {
        DateJur data = datas.get(position);
        holder.diaMes.setText(data.getDiaMes());
        holder.diaSemana.setText(data.getDiaSemana());

        if(holder.getAdapterPosition() == posNow){
            holder.btn.setImageResource(R.drawable.background_date_layout_selected);
            posNow = holder.getAdapterPosition();
        }
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.btn.setImageResource(R.drawable.background_date_layout_selected);
                // muda a cor de fundo conforme o click
            }
        });
    }

When I click on another date the background color changes the way I want, but the old date would have to stay with the default background (white), but both are with the selected background (blue) see:

inserir a descrição da imagem aqui

My question:

How do I let the adapter know that it has to change the other item to the default color after a click?

There’s a way to control it inside the Adapter or I’d have to do it another way?

As I did not want to leave the question too extensive, I put the codes I think are necessary, but if you need other information just ask.

2 answers

2


An approach would be to keep the position of the adapter that was clicked, so whenever the click was done just change the background of the item that is stored in the variable to "normal" and finally change the background of the item clicked to "new".

Something like this:

RecyclerView recyclerView;
int posUltimoClick = -1;

public void onBindViewHolder(@NonNull final ViewListDatas holder, int position) {
        DateJur data = datas.get(position);
        holder.diaMes.setText(data.getDiaMes());
        holder.diaSemana.setText(data.getDiaSemana());

        if(holder.getAdapterPosition() == posNow){
            holder.btn.setImageResource(R.drawable.background_date_layout_selected);
            posNow = holder.getAdapterPosition();
        }
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(posUltimoClick != -1){

                    // Vai aceder ao layout anteriormente clicado e mudar o fundo para o "normal"
                    Button btnAnt = recyclerView.getLayoutManager().findViewByPosition(posUltimoClick).findViewById(R.id."item que quer alterar o fundo");
                    btnAnt.setImageResource(R.drawable.background_date_layout_normal);
                    notifyItemChanged(posUltimoClick);
                    posUltimoClick = position;

                    // muda a cor de fundo do item clicado atualmente
                    holder.btn.setImageResource(R.drawable.background_date_layout_selected);
                }else{
                    posUltimoClick = position;
                    // muda a cor de fundo do item clicado atualmente
                    holder.btn.setImageResource(R.drawable.background_date_layout_selected);
                }
            }
        });
    }

Note: to access recyclerview you must pass it as a parameter of the adapter

I hope I’ve helped.

  • 1

    Great idea! I got another way... I’ll post too later. I will not mark as sure yet because I want to test and see if other answers appear. But.. thank you very much!

0

The way I did (I don’t know if it’s the best) was using LinearLayoutManager along with an internal interface in the Adapter:

Adapter

public class ListDatasAdapter extends RecyclerView.Adapter<ListDatasAdapter.ViewListDatas> {

    public ClickResponseDataItem clickItemResponse;

    public interface ClickResponseDataItem {
        void response(int position);
    }

    @NonNull
    @Override
    public ViewListDatas onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       ... código sem importância ...
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewListDatas holder, int position) {
        final DateJur data = datas.get(position);

       // botao de click
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( posNow != holder.getAdapterPosition()){
                    holder.btn.setImageResource(R.drawable.background_date_layout_selected);
                    holder.diaSemana.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
                    holder.diaMes.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
                    posNow = holder.getAdapterPosition();
                    clickItemResponse.response(holder.getAdapterPosition()); // interface
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewListDatas extends RecyclerView.ViewHolder {
       ... código sem importância ...
    }
}

In the Activity i implement Adapter interface:

public class JuridicoCalendarActivity extends AppCompatActivity implements ListDatasAdapter.ClickResponseDataItem {

    private LinearLayoutManager linearLayoutManager;
    private int posIni = 0;
    private RecyclerView listDatas;

And I insert the function into it:

 public void response(int position) {

        View v = linearLayoutManager.findViewByPosition(posIni);
        ImageButton btn = v.findViewById(R.id.btnDataJur);
        TextView text1 = v.findViewById(R.id.diaMesDataJur);
        TextView text2 = v.findViewById(R.id.diaSemanaDataJur);

        text1.setTextColor(ContextCompat.getColor(getBaseContext(), R.color.colorText));
        text2.setTextColor(ContextCompat.getColor(getBaseContext(), R.color.colorText));

        btn.setImageResource(R.drawable.background_date_layout_unselect);
        posIni = position;

    }

Still in the Activity I install my adapter as follows:

// configuro o LinearLayoutManager
linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
listDatas.setLayoutManager(linearLayoutManager); // seto no RecyclerView

adapter = new ListDatasAdapter(); // crio o adaptador
adapter.clickItemResponse = this; // seto a interface
listDatas.setAdapter(adapter); // seto o adapta

In this way, with the linearLayoutManager i can manage every Adapter item in function response() of Activity.

    // pega a view
    View v = linearLayoutManager.findViewByPosition(posIni);
    // altera os valores
    ImageButton btn = v.findViewById(R.id.btnDataJur);
    TextView text1 = v.findViewById(R.id.diaMesDataJur);
    TextView text2 = v.findViewById(R.id.diaSemanaDataJur);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.