在notifyDataSetChange()时阻止Recycleview滚动

时间:2016-11-28 11:13:05

标签: android android-recyclerview notifydatasetchanged android-scroll

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

        }
    }
}

1 个答案:

答案 0 :(得分:0)

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