1

I have a RecycleView Which shows a bunch of list in which one item is selected by showing its background color blue, now I want user to select any item from list and its color get changed to blue how to implement this inside RecyclerView.Adapter or any other logic

    public class ToggleAdapter extends RecyclerView.Adapter<ToggleAdapter.ToggleViewHolder>{

    private ArrayList<ToggleParams> dataList=new ArrayList<>();
    private Context context;
    private static int selection;

    public ToggleAdapter(ArrayList<ToggleParams> dataList, Context context,int selection) {
        setData(dataList,context,selection);
    }



    @Override
    public ToggleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row,parent,false);

        ToggleViewHolder toggleViewHolder=new ToggleViewHolder(v);
        return toggleViewHolder;
    }

    @Override
    public void onBindViewHolder(ToggleViewHolder holder, int position) {
        if(position==selection){
            holder.selected_item.setBackgroundColor(context.getResources().getColor(R.color.blue));
            holder.text_view.setTextColor(context.getResources().getColor(android.R.color.white));
        }
        holder.image_view.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getIMAGE_ID()));
        holder.text_view.setText(dataList.get(position).getTOGGLE_TEXT());

    }

    private void setData(ArrayList<ToggleParams> dataList, Context context,int selection) {
        this.dataList = dataList;
        this.context = context;
        this.selection = selection;
    }

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

    public  static class ToggleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public LinearLayout selected_item;
        public ImageView image_view;
        public TextView text_view;

        public ToggleViewHolder(View itemView) {
            super(itemView);

            selected_item= (LinearLayout) itemView.findViewById(R.id.selected_item);
            selected_item.setOnClickListener(this);
            image_view= (ImageView) itemView.findViewById(R.id.imageView);
            text_view= (TextView) itemView.findViewById(R.id.textView);
        }


        public void onClick(View v) {
            selection=getPosition();
            //After getting this position I want that this item list in recyclerview to change its background color but how to call notifyDataSetChange() here something equivalent to that  
        }
    }
}
Sanjeev
  • 4,255
  • 3
  • 28
  • 37

1 Answers1

0

You can use the setOnCLickListener in onBindViewHolder and can call notifyDatasetChanged there or a better way is to define an interface which gets called on when an itemView is clicked and to instantiate it in the adapter. For example see the following answer Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?

Community
  • 1
  • 1
Nishant
  • 575
  • 4
  • 15
  • I cannot call notifyDatasetChanged inside onBindViewHolder ,Because this function is non-static and it cannot be called in static class i.e onBindViewHolder – Sanjeev Jul 14 '15 at 07:27
  • `onBindViewHolder` is a method not a class. – Nishant Jul 14 '15 at 08:04