Change background image of an Adapter Recycler View after click

0

It is as follows, I created a Adapter that changes a RecyclerView in an Activity that I call through this function (within the 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:

EachiteminthislistwascreatedusingonBindViewHolderinmyAdapterlikethis:

publicvoidonBindViewHolder(@NonNullfinalViewListDatasholder,intposition){DateJurdata=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(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){holder.btn.setImageResource(R.drawable.background_date_layout_selected);//mudaacordefundoconformeoclick}});}

WhenIclickonanotherdatethebackgroundcolorchangesthewayIwant,buttheolddatewouldhavetobewiththedefaultbackground,butbothwillhavethebackgroundselected(blue)see:

/p>

My question:

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

Can you control this within the Adapter or would I have to do it differently?

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

    
asked by anonymous 07.09.2018 / 07:37

2 answers

1

One approach would be to store the position of the adapter item that was clicked in a variable, so whenever the click is done, simply change the background of the item that is stored in the variable to "normal" and finally change the background of the item clicked to the "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 have to pass it as the constructor parameter of adapter

I hope I have helped.

    
08.09.2018 / 17:54
0

The way I did (I do not know if it's the best) was using LinearLayoutManager along with an internal interface on 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 activity I implement the adapter interface:

public class JuridicoCalendarActivity extends AppCompatActivity implements ListDatasAdapter.ClickResponseDataItem {

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

And I enter the function inside 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;

    }

In activity I instantiate 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

This way, with linearLayoutManager I can manage each adapter item in the response() function of the 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);
    
14.09.2018 / 20:49