Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Switch in RecyclerView srcoll

I have use the Switch in the RecyclerView. It have facing the issue of recycling behaviour. When I switch on the 1st position ,it automatically on the switch at 10 postion ... I think it due to reuse of the view. How to fix it. find the screenshot: https://www.dropbox.com/s/4ms2jf9e28fyc7u/error.png?dl=0

    private void setAdapter(ArrayList data) {

            ManageCategoryAdapter adapter = new ManageCategoryAdapter(data);
            adapter.SetOnItemClickListener(listClick);
            mRecyclerView.setAdapter(adapter);
        }
     public class ManageCategoryAdapter extends RecyclerView.Adapter<ManageCategoryAdapter.ViewHolder> {


    private ArrayList<String> catData=new ArrayList<>();
    private OnItemClickListener mItemClickListener;


    public ManageCategoryAdapter(ArrayList<String> listadap) {
        catData=listadap;
        System.out.println("$$$$$$$$$"+"adapterclass");

    }


    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_manage_list, parent, false);
        return new ViewHolder(v);
    }


    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.category.setText(catData.get(position));


    }


    public int getItemCount() {

        return catData.size();
    }


    public void onClick(View view) {

    }

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

        public TextView category;
        public Switch switchClick;

        public ViewHolder(View itemView) {
            super(itemView);
            category=(TextView)itemView.findViewById(R.id.cat_text);
            switchClick=(Switch)itemView.findViewById(R.id.switch_btn);
            switchClick.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getPosition());
            }

        }
    }
    public void myNotifyDataSetChanged(ArrayList list)
    {
        System.out.println("$$$notify");
        catData.addAll(list);
        this.notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}

This how I set the adapter class

like image 423
dinesh kumar Avatar asked Mar 06 '26 03:03

dinesh kumar


1 Answers

You need to use this in the adapter:

@Override
    public int getItemViewType(int position) {
        return position;
    }
like image 183
Miguel Yengle Avatar answered Mar 08 '26 16:03

Miguel Yengle