Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling

前端 未结 26 2498
借酒劲吻你
借酒劲吻你 2020-12-01 00:59

I\'m using RecyclerView to display name of the items. My row contains single TextView. Item names are stored in List mItemList<

26条回答
  •  暖寄归人
    2020-12-01 01:39

    You must use in your getitem count

    public int getItemCount() {
    
                if (mList!= null)
                    return mList.size();
                else
                    return 0;
            }
    

    Also refreshing the recycler view please use this

    if (recyclerView.getAdapter() == null) {
    
                recyclerView.setHasFixedSize(true);
                mFileListAdapter= new FileListAdapter(this);
                recyclerView.setAdapter(mFileListAdapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            } else {
                mFileListAdapter.notifyDataSetChanged();        
    
            }
    

    By using this solution you are not able to solve the issue you simply use a condition inside the onBindViewHolder to resolve the java.lang.IndexOutOfBoundsException

     public void onBindViewHolder(FileHolder fileHolder, final int i) {
            if(i < mList.size)
            {
               String name = mList.get(i);       
               setSelected(fileHolder.itemView, mSelectedArray.get(i));
               fileHolder.mTextView.setText(name);
            }
        }
    

提交回复
热议问题