在NotifyDataSetchange()NotifyDataSethange()时,请防止回收浏览滚动



当我使用notifyDataSetChange()通知我的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);
        }
    }
}

使用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);
                }
            }
        });

相关内容

  • 没有找到相关文章

最新更新