notifyDataSetChanged() 时防止 Recyclerview 滚动
Posted
技术标签:
【中文标题】notifyDataSetChanged() 时防止 Recyclerview 滚动【英文标题】:Prevent Recycleview scroll when notifyDataSetChange() 【发布时间】:2017-04-12 02:16:47 【问题描述】:当我使用notifyDataSetChange()
通知我的recycleview
时,它会自动滚动到顶部。
我正在为多窗格目的制作此适配器。
该应用仅适用于平板电脑。
所以有谁能给我解决这个问题吗??
这是我的 recycleview 适配器。
public class PickersListAdapter extends RecyclerView.Adapter<PickersListAdapter.DataObjectHolder>
private Context context;
private ArrayList<PickersModel> pickersModelArrayList;
private ItemsListFragment fragment;
public PickersListAdapter(Context context, ArrayList<PickersModel> pickersModelArrayList, ItemsListFragment itemsListFragment)
this.context = context;
this.pickersModelArrayList = pickersModelArrayList;
fragment = itemsListFragment;
@Override
public PickersListAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, final int viewType)
View view = LayoutInflater.from(context).inflate(R.layout.inflate_list_pickers, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
@Override
public void onBindViewHolder(final PickersListAdapter.DataObjectHolder holder, final int position)
holder.myBackground.setSelected(pickersModelArrayList.get(position).isClicked());
holder.checkBox.setChecked(pickersModelArrayList.get(position).isChecked());
holder.txtItemName.setText(pickersModelArrayList.get(position).getItemName());
holder.txtitemQuantity.setText(pickersModelArrayList.get(position).getQuantity());
Log.e("image url...", pickersModelArrayList.get(position).getItemIcon());
Glide.with(context).load(pickersModelArrayList.get(position).getItemIcon()).placeholder(R.drawable.ic_item_icon).into(holder.imageView);
holder.myBackground.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
for (int i = 0; i < pickersModelArrayList.size(); i++)
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
notifyDataSetChanged();
);
@Override
public int getItemCount()
return pickersModelArrayList.size();
public class DataObjectHolder extends RecyclerView.ViewHolder
private CheckBox checkBox;
private ImageView imageView;
private TextView txtItemName, txtitemQuantity;
private LinearLayout myBackground;
public DataObjectHolder(View itemView)
super(itemView);
myBackground = (LinearLayout) itemView.findViewById(R.id.myBackground);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxPickersItems);
imageView = (ImageView) itemView.findViewById(R.id.imgItemIconPickerItems);
txtItemName = (TextView) itemView.findViewById(R.id.txtItemNamePickerItems);
txtitemQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantityPickerItems);
【问题讨论】:
【参考方案1】:使用setTag()
,getTag()
,然后使用不同的函数来通知。
试试这样的:
holder.myBackground.setTag(position);
holder.myBackground.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if (null != v.getTag())
int position = (int) v.getTag();
for (int i = 0; i < pickersModelArrayList.size(); i++)
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
notifyItemChanged(position);
);
【讨论】:
setTag
?做什么的?只需在您的ViewHolder
中实现OnClickListener
,仅此而已
setTag()
是必需的,因为在列表中它应该具有您单击的视图的位置。因此,只有该位置的更改才应该反映,而不是每个项目。
您只需要在您的ViewHolder
中实现OnClickListener
,不需要setTag
/ getTag
,有关更多信息,请参阅ViewHolder
文档
它是必需的。因此,要么将ViewHolder
放入标签中,要么将position
放入。否则每次它将占据列表中最后一个显示项目的位置
好的,然后讨论结束。使用getAdapterPosition()
并仅通知该部分。以上是关于notifyDataSetChanged() 时防止 Recyclerview 滚动的主要内容,如果未能解决你的问题,请参考以下文章
ArrayAdapter.NotifyDataSetChanged() 不工作?
[完成]notifyDataSetChanged() 不会自动更新 ListActivity
notifyDataSetChanged() 上的 WinDeath