简体   繁体   English

RecyclerView index=-1 快速滚动到底部时

[英]RecyclerView index=-1 when scrolling fast to the bottom

I have created a RecyclerView to display items and everything shows and works fine except to one scenario: when I scroll fast to the bottom of the RecyclerView .我创建了一个RecyclerView来显示项目,一切都显示并且工作正常,除了一种情况:当我快速滚动到RecyclerView的底部时。

If I do so, I get the following error:如果这样做,我会收到以下错误:

java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1

It directs me to this part of my code (specifically to checkIfBorrowed.get( getAdapterPosition() ) ):它引导我查看我的代码的这一部分(特别是checkIfBorrowed.get( getAdapterPosition() ) ):

public class itemtestAdapter extends RecyclerView.Adapter<itemtestAdapter.testViewHolder> {

    private List<Discoveritems> items;
    private List<Boolean> checkIftested;

    interface OnItemCheckListener {
        void onItemCheck(Discoveritems items);
        void onItemUncheck(Discoveritems items);
    }

    @NonNull
    private OnItemCheckListener onItemCheckListener;

    public itemtestAdapter(List<Discoveritems> items, List<Boolean> checkIftested, @NonNull OnItemCheckListener onItemCheckListener) {
        this.items = items;
        this.onItemCheckListener = onItemCheckListener;
        this.checkIftested = checkIftested;
    }

    @Override
    public testViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_item_test_list, parent, false );
        return new testViewHolder(view);
    }

    @Override
    public void onBindViewHolder(testViewHolder holder, int position) {
        holder.bind(items.get(position));

        final Discoveritems currentItem = items.get(position);

        holder.setOnClickListener( v -> {
            holder.Cb_test.setChecked(
                    !holder.Cb_test.isChecked());
            if (holder.Cb_test.isChecked()) {
                onItemCheckListener.onItemCheck(currentItem);
            } else {
                onItemCheckListener.onItemUncheck(currentItem);
            }
        } );

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class testViewHolder extends RecyclerView.ViewHolder {
        ImageView Iv_test,itemPic;
        TextView itemName;
        CheckBox Cb_test;
        Discoveritems items;

        public testViewHolder(View itemView) {
            super(itemView);

            Iv_testitemPic = itemView.findViewById(R.id.iv_itemCover);
            Tv_testitemName = itemView.findViewById( R.id.tv_testeditemName);
            Cb_test = itemView.findViewById( R.id.cb_testitem );
            Cb_test.setClickable( false );

        }

        public void setOnClickListener(View.OnClickListener onClickListener) {
            itemView.setOnClickListener(onClickListener);
        }

        public void bind(Discoveritems items) {

            this.items = items;

            String itemID = items.getitemID();
            MyitemClient client = new MyitemClient();
            client.getitems(itemID, new JsonHttpResponseHandler(){
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response){
                    if(response!=null){
                        final Myitem items = Myitem.fromJson(response);

                        if (checkIftested.get( getAdapterPosition() )){
                            itemView.setEnabled(false);
                            Tv_itemName.setText("my" + items.getTitle());
                        } else {
                            Tv_itemName.setText( items.getTitle() );
                        }
                    }
                }

            });
            
        }
    }

}

Thing is that inside bind I can't figure out how to get the position of the item without using getAdapterPosition and I also cant understand why it only happens when I scroll fast.问题是在绑定内部我无法弄清楚如何在不使用 getAdapterPosition 的情况下获取项目的getAdapterPosition ,而且我也无法理解为什么它只在我快速滚动时才会发生。

Also it is weird that it says length = 15 while checkIfBorowed's size is 12 only.同样奇怪的是它说长度 = 15 而 checkIfBorowed 的大小仅为 12。 Thank you谢谢

Change holder.bind(items.get(position));更改holder.bind(items.get(position)); to holder.bind(items.get(position), position);holder.bind(items.get(position), position); and then change viewholders bind method to accept the new position argument ie bind(Discoveritems items, int position) and use position instead of getAdapterPosition() .然后更改 viewholders bind方法以接受新的 position 参数,即bind(Discoveritems items, int position)并使用 position 而不是getAdapterPosition()

The issue is you are not using the position the adapter gave your viewholder which can change when views are recycled and thus they dont macth.问题是您没有使用 position 适配器给您的查看器,它可以在视图被回收时更改,因此它们不适用。 Please see here for more https://stackoverflow.com/a/38970513/1133011请参阅此处了解更多https://stackoverflow.com/a/38970513/1133011

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM