Using Switch in RecyclerView srcoll

Deadly 提交于 2019-12-23 16:28:06

问题


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


回答1:


You need to use this in the adapter:

@Override
    public int getItemViewType(int position) {
        return position;
    }



回答2:


This is a very common problem with RecyclerView and there are lots of answers there in Stackoverflow.

You already have understood your problem i.e. reusing the views. So you might take a look at these answers to get a better idea about how you can overcome it.

  • Put an else condition everywhere you need to update a view of a list item dynamically.
  • Using another list to keep track of the list items in which the Switch is enabled or disabled. You can see this answer here.

These will do the trick for you I hope.



来源:https://stackoverflow.com/questions/36807029/using-switch-in-recyclerview-srcoll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!