在RecyclerView srcoll中使用Switch [英] Using Switch in RecyclerView srcoll
本文介绍了在RecyclerView srcoll中使用Switch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在RecyclerView
中使用了Switch
.它已经面临着回收行为的问题.当我在第一个位置上切换时,它会在位置10时自动打开...我认为这是由于视图的重用.如何解决.找到屏幕截图:
https://www.dropbox.com/s/4ms2jf9e28fyc7u/error. png?dl = 0
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
推荐答案
您需要在适配器中使用它:
You need to use this in the adapter:
@Override
public int getItemViewType(int position) {
return position;
}
这篇关于在RecyclerView srcoll中使用Switch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文