0

I'm changing the background at the firts textview (postion 0) in onBindViewHolder

this is my RecyclerAdapter

public class MenuCardAdapter extends RecyclerView.Adapter<MenuCardAdapter.MenuCardViewHolder> {



 //Context and Reference Parent
    private Context context;
    private List<MenuCard> objects;
    private ItemOnCardviewMenu listener;

     MenuCardViewHolder holder = null;

    public MenuCardAdapter(Context context , List<MenuCard> objects, ItemOnCardviewMenu listener) {
        this.context = context;
        this.objects = objects;
        this.listener = listener;
    }




    @Override
    public MenuCardAdapter.MenuCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_card, parent, false);
        MenuCardViewHolder vHolder = new MenuCardViewHolder(v,this.listener);
        return  vHolder;

    }

    public MenuCard getItem(int position) {
        return objects.get(position);
    }




    @Override
    public void onBindViewHolder(final MenuCardViewHolder holder, final int position) {

        holder.title.setText(objects.get(position).getmTexto());
        holder.id.setText(objects.get(position).getmId());
        if(position==0){
            holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
        }
        this.holder = holder;

    }


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




    class MenuCardViewHolder extends RecyclerView.ViewHolder

    {

        TextView title;
        TextView id;
        CardView card;
        public MenuCardViewHolder(final View itemView, final ItemOnCardviewMenu listener) {
            super(itemView);

            id = (TextView) itemView.findViewById(R.id.id_card);
            title = (TextView) itemView.findViewById(R.id.card_text);
            card = (CardView) itemView.findViewById(R.id.card_view);
            card.setTag(this.card);

            title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(listener != null){

                        listener.onRowClicked(getAdapterPosition(), v, card, id.getText().toString());


                    }
                }
            });
        }
    }
}

in this part is where i'm changing the background

@Override
public void onBindViewHolder(final MenuCardViewHolder holder, final int position) {

    holder.title.setText(objects.get(position).getmTexto());
    holder.id.setText(objects.get(position).getmId());
    if(position==0){
        holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
    }
    this.holder = holder;

}

but when i run the app look like this menu pos 0. menu pos 0

but at the same time the position 8 change its color menu pos 7 menu pos 7

i dont know why is this happening, happend too whe a click "option 2" the "option 9" also change it background.

thanks in advance.

sinsuren
  • 1,745
  • 2
  • 23
  • 26
Samu Pad
  • 1
  • 2
  • try http://stackoverflow.com/a/36131979/2032561 its for listview but you can understand the logic and implement the same for recyclerview – Bharatesh Aug 11 '16 at 14:23

4 Answers4

2

You have to put else case also because it reuses objects(view holder pattern).

 if(position==0){
    holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
}
 else{
    holder.title.setBackgroundColor(context.getResources().getColor(some other color));
}
Prashanth Debbadwar
  • 1,047
  • 18
  • 33
  • When i change the color from the activity the color is reset, i implement a interface for handler clicks and change the color for simulate selected option, but when i scroll the color is removed, how i can save the state and simulate selected element? btw, the else works fine – Samu Pad Aug 11 '16 at 15:36
0

ListView reuse items so else condition also required. Otherwise you will face such behavior. Where you scroll 0th position item goes outside and view reused by other list item for display. So already set background showing for other item.

Ramit
  • 416
  • 3
  • 8
0

use

if(position == 0){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   holder.title.setBackgroundColor(context.getResources().getColor(R.color.black, context.getTheme()));
}else{
    holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
}
else{
 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    holder.title.setBackgroundColor(context.getResources().getColor(R.color.green,  context.getTheme()));
}else{
     holder.title.setBackgroundColor(context.getResources().getColor(R.color.green));
     }
 }

This is because getColor(int id) is deprecated on API's 23 and above, you should use getColor(int id, Theme theme) if the api level is 23 or higher

Tiago Oliveira
  • 1,582
  • 1
  • 16
  • 31
0

I can solve my problem thanks to

Prashanth Debbadwar for your suggestion

this is how a figure out for simulate a menu

@Override
public void onBindViewHolder(final MenuCardViewHolder holder, final int position) {

    holder.title.setText(objects.get(position).getmTexto());
    holder.id.setText(objects.get(position).getmId());
    if(position==posOption){
        holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
    }
    else{
        holder.title.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
    }

    holder.title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          if(posOption != position){
              holder.title.setBackgroundColor(context.getResources().getColor(R.color.black));
              posOption = position;

          }else {
              holder.title.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
          }

        notifyDataSetChanged();


        }
    });

    this.holder = holder;

}

also i romove the interface from the activity

Samu Pad
  • 1
  • 2