RecyclerView和java.lang.IndexOutOfBoundsException:检测到不一致。三星设备中的视图持有者适配器positionViewHolder无效

时间:2015-08-01 06:51:43

标签: android android-recyclerview

我有一个回收站视图,可以在除三星以外的所有设备上完美运行。在三星,我得到

  

java.lang.IndexOutOfBoundsException:检测到不一致。视图持有者适配器positionViewHolder

无效

当我使用另一个活动的回收者视图回到片段时。

适配器代码:

public class FeedRecyclerAdapter extends RecyclerView.Adapter<FeedRecyclerAdapter.MovieViewHolder> {
    public static final String getUserPhoto = APIConstants.BASE_URL + APIConstants.PICTURE_PATH_SMALL;
    Movie[] mMovies = null;
    Context mContext = null;
    Activity mActivity = null;
    LinearLayoutManager mManager = null;
    private Bus uiBus = null;
    int mCountOfLikes = 0;

    //Constructor
    public FeedRecyclerAdapter(Movie[] movies, Context context, Activity activity,
                               LinearLayoutManager manager) {
        mContext = context;
        mActivity = activity;
        mMovies = movies;
        mManager = manager;
        uiBus = BusProvider.getUIBusInstance();
    }

    public void setMoviesAndNotify(Movie[] movies, boolean movieIgnored) {
        mMovies = movies;
        int firstItem = mManager.findFirstVisibleItemPosition();
        View firstItemView = mManager.findViewByPosition(firstItem);
        int topOffset = firstItemView.getTop();
        notifyDataSetChanged();
        if(movieIgnored) {
            mManager.scrollToPositionWithOffset(firstItem - 1, topOffset);
        } else {
            mManager.scrollToPositionWithOffset(firstItem, topOffset);
        }
    }

    // Create new views (called by layout manager)
    @Override
    public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.feed_one_recommended_movie_layout, parent, false);

        return new MovieViewHolder(view);
    }

    // Replaced contend of each view (called by layout manager)
    @Override
    public void onBindViewHolder(MovieViewHolder holder, int position) {
        setLikes(holder, position);
        setAddToCollection(holder, position);
        setTitle(holder, position);
        setIgnoreMovieInfo(holder, position);
        setMovieInfo(holder, position);
        setPosterAndTrailer(holder, position);
        setDescription(holder, position);
        setTags(holder, position);
    }

    // returns item count (called by layout manager)
    @Override
    public int getItemCount() {
        return mMovies != null ? mMovies.length : 0;
    }

    private void setLikes(final MovieViewHolder holder, final int position) {
        List<Reason> likes = new ArrayList<>();
        for(Reason reason : mMovies[position].reasons) {
            if(reason.title.equals("Liked this movie")) {
                likes.add(reason);
            }
        }
        mCountOfLikes = likes.size();
        holder.likeButton.setText(mContext.getString(R.string.like)
            + Html.fromHtml(getCountOfLikesString(mCountOfLikes)));
        final MovieRepo repo = MovieRepo.getInstance();
        final int pos = position;
        final MovieViewHolder viewHolder = holder;
        holder.likeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMovies[pos].isLiked) {
                    repo.unlikeMovie(AuthStore.getInstance()
                        .getAuthToken(), mMovies[pos].id, new Callback<Movie>() {
                        @Override
                        public void success(Movie movie, Response response) {
                            Drawable img = mContext.getResources().getDrawable(R.drawable.ic_like);
                            viewHolder.likeButton
                                .setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
                            if (--mCountOfLikes <= 0) {
                                viewHolder.likeButton.setText(mContext.getString(R.string.like));
                            } else {
                                viewHolder.likeButton
                                    .setText(Html.fromHtml(mContext.getString(R.string.like)
                                        + getCountOfLikesString(mCountOfLikes)));
                            }
                            mMovies[pos].isLiked = false;
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Toast.makeText(mContext.getApplicationContext(),
                                mContext.getString(R.string.cannot_like), Toast.LENGTH_LONG)
                                .show();
                        }
                    });
                } else {
                    repo.likeMovie(AuthStore.getInstance()
                        .getAuthToken(), mMovies[pos].id, new Callback<Movie>() {
                        @Override
                        public void success(Movie movie, Response response) {
                            Drawable img = mContext.getResources().getDrawable(R.drawable.ic_liked_green);
                            viewHolder.likeButton
                                .setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
                            viewHolder.likeButton
                                .setText(Html.fromHtml(mContext.getString(R.string.like)
                                    + getCountOfLikesString(++mCountOfLikes)));
                            mMovies[pos].isLiked = true;
                            setComments(holder, position);
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Toast.makeText(mContext,
                                mContext.getString(R.string.cannot_like), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        });
    }

    private void setComments(final MovieViewHolder holder, final int position) {
        holder.likeAndSaveButtonLayout.setVisibility(View.GONE);
        holder.commentsLayout.setVisibility(View.VISIBLE);
        holder.sendCommentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.commentsInputEdit.getText().length() > 0) {
                    CommentRepo repo = CommentRepo.getInstance();
                  repo.sendUserComment(AuthStore.getInstance().getAuthToken(), mMovies[position].id,
                        holder.commentsInputEdit.getText().toString(), new Callback<Void>() {
                            @Override
                            public void success(Void aVoid, Response response) {
                                Toast.makeText(mContext, mContext.getString(R.string.thanks_for_your_comment),
                                    Toast.LENGTH_SHORT).show();
                                hideCommentsLayout(holder);
                            }

                            @Override
                            public void failure(RetrofitError error) {
                                Toast.makeText(mContext, mContext.getString(R.string.cannot_add_comment),
                                    Toast.LENGTH_LONG).show();
                            }
                        });
                } else {
                    hideCommentsLayout(holder);
                }
            }
        });
    }

    private void hideCommentsLayout(MovieViewHolder holder) {
        holder.commentsLayout.setVisibility(View.GONE);
        holder.likeAndSaveButtonLayout.setVisibility(View.VISIBLE);
    }

    private void setAddToCollection(final MovieViewHolder holder, int position) {
        final int pos = position;
        if(mMovies[position].isInWatchlist) {
            holder.saveButton
              .setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_check_green, 0, 0, 0);
        }
        final CollectionRepo repo = CollectionRepo.getInstance();
        holder.saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!mMovies[pos].isInWatchlist) {
                   repo.addMovieToCollection(AuthStore.getInstance().getAuthToken(), 0, mMovies[pos].id, new Callback<MovieCollection[]>() {
                            @Override
                            public void success(MovieCollection[] movieCollections, Response response) {
                                holder.saveButton
                                    .setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_check_green, 0, 0, 0);

                                mMovies[pos].isInWatchlist = true;
                            }

                            @Override
                            public void failure(RetrofitError error) {
                                Toast.makeText(mContext, mContext.getString(R.string.movie_not_added_to_collection),
                                    Toast.LENGTH_LONG).show();
                            }
                        });
                } else {
                 repo.removeMovieFromCollection(AuthStore.getInstance().getAuthToken(), 0,
                        mMovies[pos].id, new Callback<MovieCollection[]>() {
                        @Override
                        public void success(MovieCollection[] movieCollections, Response response) {
                            holder.saveButton
                                .setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_plus, 0, 0, 0);

                            mMovies[pos].isInWatchlist = false;
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Toast.makeText(mContext,
                                mContext.getString(R.string.cannot_delete_movie_from_watchlist),
                                Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        });
    }

    private String getCountOfLikesString(int countOfLikes) {
        String countOfLikesStr;
        if(countOfLikes == 0) {
            countOfLikesStr = "";
        } else if(countOfLikes > 999) {
            countOfLikesStr = " " + (countOfLikes/1000) + "K";
        } else if (countOfLikes > 999999){
            countOfLikesStr = " " + (countOfLikes/1000000) + "M";
        } else {
            countOfLikesStr = " " + String.valueOf(countOfLikes);
        }
        return "<small>" + countOfLikesStr + "</small>";
    }

    private void setTitle(MovieViewHolder holder, final int position) {
        holder.movieTitleTextView.setText(mMovies[position].title);
        holder.movieTitleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieDetailActivity.openView(mContext, mMovies[position].id, true, false);
            }
        });
    }

    private void setIgnoreMovieInfo(MovieViewHolder holder, final int position) {
        holder.ignoreMovie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieRepo repo = MovieRepo.getInstance();
                repo.hideMovie(AuthStore.getInstance().getAuthToken(), mMovies[position].id,
                    new Callback<Void>() {
                        @Override
                        public void success(Void aVoid, Response response) {
                            Movie[] newMovies = new Movie[mMovies.length - 1];
                            for (int i = 0, j = 0; j < mMovies.length; i++, j++) {
                                if (i != position) {
                                    newMovies[i] = mMovies[j];
                                } else {
                                    if (++j < mMovies.length) {
                                        newMovies[i] = mMovies[j];
                                    }
                                }
                            }
                            uiBus.post(new MoviesChangedEvent(newMovies));
                            setMoviesAndNotify(newMovies, true);
                            Toast.makeText(mContext, mContext.getString(R.string.movie_ignored),
                                Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Toast.makeText(mContext, mContext.getString(R.string.movie_ignored_failed),
                                Toast.LENGTH_LONG).show();
                        }
                    });
            }
        });
    }

    private void setMovieInfo(MovieViewHolder holder, int position) {
        String imdp = "IMDB: ";
        String sources = "", date;
        if(mMovies[position].showtimes != null && mMovies[position].showtimes.length > 0) {
            int countOfSources = mMovies[position].showtimes.length;
            for(int i = 0; i < countOfSources; i++) {
                sources += mMovies[position].showtimes[i].name + ", ";
            }
            sources = sources.trim();
            if(sources.charAt(sources.length() - 1) == ',') {
                if(sources.length() > 1) {
                    sources = sources.substring(0, sources.length() - 2);
                } else {
                    sources = "";
                }
            }
        } else {
            sources = "";
        }
        imdp += mMovies[position].imdbRating + " | ";
        if(sources.isEmpty()) {
            date = mMovies[position].releaseYear;
        } else {
            date = mMovies[position].releaseYear + " | ";
        }

        holder.movieInfoTextView.setText(imdp + date + sources);
    }

    private void setPosterAndTrailer(final MovieViewHolder holder, final int position) {
        if (mMovies[position] != null && mMovies[position].posterPath != null
            && !mMovies[position].posterPath.isEmpty()) {
            Picasso.with(mContext)
                .load(mMovies[position].posterPath)
             .error(mContext.getResources().getDrawable(R.drawable.noposter))
                .into(holder.posterImageView);
        } else {
            holder.posterImageView.setImageResource(R.drawable.noposter);
        }
        holder.posterImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieDetailActivity.openView(mActivity, mMovies[position].id, false, false);
            }
        });
        if(mMovies[position] != null && mMovies[position].trailerLink  != null
            && !mMovies[position].trailerLink.isEmpty()) {
            holder.playTrailer.setVisibility(View.VISIBLE);
            holder.playTrailer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MovieDetailActivity.openView(mActivity, mMovies[position].id, false, true);
                }
            });
        }
    }

    private void setDescription(MovieViewHolder holder, int position) {
        String text = mMovies[position].overview;
        if(text == null || text.isEmpty()) {
       holder.descriptionText.setText(mContext.getString(R.string.no_description));
        } else if(text.length() > 200) {
            text = text.substring(0, 196) + "...";
            holder.descriptionText.setText(text);
        } else {
            holder.descriptionText.setText(text);
        }
        final int pos = position;
        holder.descriptionText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieDetailActivity.openView(mActivity, mMovies[pos].id, false, false);
            }
        });
    }

    private void setTags(MovieViewHolder holder, int position) {
        List<String> tags = Arrays.asList(mMovies[position].tags);
        if(tags.size() > 0) {
            CastAndTagsFeedAdapter adapter = new CastAndTagsFeedAdapter(tags,
                mContext, ((FragmentActivity) mActivity).getSupportFragmentManager());
            holder.tags.setItemMargin(10);
            holder.tags.setAdapter(adapter);
        } else {
            holder.tags.setVisibility(View.GONE);
        }
    }

    // class view holder that provide us a link for each element of list
    public static class MovieViewHolder extends RecyclerView.ViewHolder {
        TextView movieTitleTextView, movieInfoTextView, descriptionText, reasonsCountText;
        TextView reasonText1, reasonAuthor1, reasonText2, reasonAuthor2;
        EditText commentsInputEdit;
        Button likeButton, saveButton, playTrailer, sendCommentButton;
        ImageButton ignoreMovie;
        ImageView posterImageView, userPicture1, userPicture2;
        TwoWayView tags;
        RelativeLayout mainReasonsLayout, firstReasonLayout, secondReasonLayout, reasonsListLayout;
        RelativeLayout commentsLayout;
        LinearLayout likeAndSaveButtonLayout;
        ProgressBar progressBar;

        public MovieViewHolder(View view) {
            super(view);
            movieTitleTextView = (TextView)view.findViewById(R.id.movie_title_text);
            movieInfoTextView = (TextView)view.findViewById(R.id.movie_info_text);
            descriptionText = (TextView)view.findViewById(R.id.text_description);
            reasonsCountText = (TextView)view.findViewById(R.id.reason_count);
            reasonText1 = (TextView)view.findViewById(R.id.reason_text_1);
            reasonAuthor1 = (TextView)view.findViewById(R.id.author_1);
            reasonText2 = (TextView)view.findViewById(R.id.reason_text_2);
            reasonAuthor2 = (TextView)view.findViewById(R.id.author_2);
            commentsInputEdit = (EditText)view.findViewById(R.id.comment_input);
            likeButton = (Button)view.findViewById(R.id.like_button);
            saveButton = (Button)view.findViewById(R.id.save_button);
            playTrailer = (Button)view.findViewById(R.id.play_trailer_button);
            sendCommentButton = (Button)view.findViewById(R.id.send_button);
            ignoreMovie = (ImageButton)view.findViewById(R.id.ignore_movie_imagebutton);
            posterImageView = (ImageView)view.findViewById(R.id.poster_image);
            userPicture1 = (ImageView)view.findViewById(R.id.user_picture_1);
            userPicture2 = (ImageView)view.findViewById(R.id.user_picture_2);
            tags = (TwoWayView)view.findViewById(R.id.list_view_feed_tags);
            mainReasonsLayout = (RelativeLayout)view.findViewById(R.id.reasons_main_layout);
            firstReasonLayout = (RelativeLayout)view.findViewById(R.id.first_reason);
            secondReasonLayout = (RelativeLayout)view.findViewById(R.id.second_reason);
            reasonsListLayout = (RelativeLayout)view.findViewById(R.id.reasons_list);
            commentsLayout = (RelativeLayout)view.findViewById(R.id.comments_layout);
            likeAndSaveButtonLayout = (LinearLayout)view
                .findViewById(R.id.like_and_save_buttons_layout);
            progressBar = (ProgressBar)view.findViewById(R.id.centered_progress_bar);
        }
    }
}

例外:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{42319ed8 position=1 id=-1, oldPos=0, pLpos:0 scrap tmpDetached no parent}
 at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4166)
 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4297)
 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4278)
 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1947)
 at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:434)
 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1322)
 at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
 at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
 at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2627)
 at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2971)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:562)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
07-30 12:48:22.688    9590-9590/com.Filmgrail.android.debug W/System.err? at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 at android.view.View.layout(View.java:15746)
 at android.view.ViewGroup.layout(ViewGroup.java:4867)
 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6630)
 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
 at android.view.Choreographer.doCallbacks(Choreographer.java:603)
 at android.view.Choreographer.doFrame(Choreographer.java:573)
 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
 at android.os.Handler.handleCallback(Handler.java:733)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5479)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
 at dalvik.system.NativeStart.main(Native Method)

我该如何解决这个问题?

33 个答案:

答案 0 :(得分:158)

此问题是由RecyclerView在不同线程中修改的数据引起的。最好的方法是检查所有数据访问。解决方法是包装LinearLayoutManager

以前的回答

RecyclerView中确实存在一个错误,支持23.1.1仍然没有修复。

对于解决方法,请注意回溯堆栈,如果我们可以在某个类中捕获此Exception,则可能会跳过此崩溃。对我来说,我创建了一个LinearLayoutManagerWrapper并覆盖onLayoutChildren

public class WrapContentLinearLayoutManager extends LinearLayoutManager {
    //... constructor
    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            Log.e("TAG", "meet a IOOBE in RecyclerView");
        }
    }
}

然后将其设置为RecyclerView

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));

实际上抓住这个例外,似乎没有任何副作用。

此外,如果您使用GridLayoutManagerStaggeredGridLayoutManager,则必须为其创建包装。

注意:RecyclerView可能处于错误的内部状态。

答案 1 :(得分:58)

这是使用全新内容刷新数据的示例。 您可以轻松修改它以满足您的需求。 在我的情况下,我通过致电:

解决了这个问题
notifyItemRangeRemoved(0, previousContentSize);

之前:

notifyItemRangeInserted(0, newContentSize);

这是正确的解决方案,AOSP项目成员在this post中也提到了这一点。

答案 2 :(得分:25)

新答案: 将DiffUtil用于所有RecyclerView更新。这将有助于性能和上面的错误。 See Here

上一个答案: 这对我有用。关键是不要使用notifyDataSetChanged()并以正确的顺序做正确的事情:

public void setItems(ArrayList<Article> newArticles) {
    //get the current items
    int currentSize = articles.size();
    //remove the current items
    articles.clear();
    //add all the new items
    articles.addAll(newArticles);
    //tell the recycler view that all the old items are gone
    notifyItemRangeRemoved(0, currentSize);
    //tell the recycler view how many new items we added
    notifyItemRangeInserted(0, newArticles.size());
}

答案 3 :(得分:19)

我曾经遇到过这个问题,我通过包装LayoutManager和禁用预测动画解决了这个问题。

这是一个例子:

public class LinearLayoutManagerWrapper extends LinearLayoutManager {

  public LinearLayoutManagerWrapper(Context context) {
    super(context);
  }

  public LinearLayoutManagerWrapper(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
  }

  public LinearLayoutManagerWrapper(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  public boolean supportsPredictiveItemAnimations() {
    return false;
  }
}

并将其设为RecyclerView

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManagerWrapper(context, LinearLayoutManager.VERTICAL, false);

答案 4 :(得分:14)

我遇到了类似的问题。

以下错误代码中的问题:

int prevSize = messageListHistory.size();
// some insert
adapter.notifyItemRangeInserted(prevSize - 1, messageListHistory.size() -1);

<强>解决方案:

int prevSize = messageListHistory.size();
// some insert
adapter.notifyItemRangeInserted(prevSize, messageListHistory.size() -prevSize);

答案 5 :(得分:13)

根据this issue,问题已得到解决,可能会在2015年初的某个时候发布。A quote from that same thread

  

它与调用notifyDataSetChanged有关。 [...]

     

顺便说一下,我强烈建议不要使用notifyDataSetChanged,因为它会杀死动画和性能。同样对于这种情况,使用特定的通知事件将解决此问题。

如果您仍然遇到最新版本的支持库的问题,我建议您在适配器中查看对notifyXXX的调用(具体来说,您使用notifyDataSetChanged),以确保正在坚持(有些微妙/模糊)RecyclerView.Adapter合同。另外一定要在主线程上发出这些通知。

答案 6 :(得分:10)

我遇到了同样的问题。 这是因为我推迟了关于项目插入的适配器的通知。

但是ViewHolder试图在其视图中重新绘制一些数据,并开始RecyclerView测量和重新计算子项数 - 此时它崩溃了(项目列表,它的大小已经更新,但适配器尚未通知)。

答案 7 :(得分:7)

当您为notifyItemChanged指定错误的位置时会发生这种情况, notifyItemRangeInserted等。对我来说:

之前:(错误的)

public void addData(List<ChannelItem> list) {
  int initialSize = list.size();
  mChannelItemList.addAll(list);
  notifyItemRangeChanged(initialSize - 1, mChannelItemList.size());
 } 

之后:(正确)

 public void addData(List<ChannelItem> list) {
  int initialSize = mChannelItemList.size();
  mChannelItemList.addAll(list);
  notifyItemRangeInserted(initialSize, mChannelItemList.size()-1); //Correct position 
 }

答案 8 :(得分:7)

发生此问题的另一个原因是当您使用错误的索引调用这些方法时(未插入或删除索引的索引)

-notifyItemRangeRemoved

-notifyItemRemoved

-notifyItemRangeInserted

-notifyItemInserted

检查这些方法的索引参数,并确保它们准确无误。

答案 9 :(得分:6)

这个错误在23.1.1中仍未修复,但常见的解决方法是捕获异常。

答案 10 :(得分:4)

原因引起了此问题:

  1. 启用项目动画后,Recycler中的一个内部问题
  2. 在另一个线程中修改Recycler数据
  3. 以错误的方式调用通知方法

解决方案:

-----------------解决方案1 ​​---------------

  • 正在捕获异常(不建议使用,尤其是原因3)

创建如下的自定义LinearLayoutManager并将其设置为ReyclerView

    public class CustomLinearLayoutManager extends LinearLayoutManager {

            //Generate constructors

            @Override
            public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

                try {

                    super.onLayoutChildren(recycler, state);

                } catch (IndexOutOfBoundsException e) {

                    Log.e(TAG, "Inconsistency detected");
                }

            }
        }

然后将RecyclerVIew布局管理器设置如下:

recyclerView.setLayoutManager(new CustomLinearLayoutManager(activity));

-----------------解决方案2 ---------------

  • 禁用项目动画(修复导致原因1的问题):

同样,创建一个自定义线性布局管理器,如下所示:

    public class CustomLinearLayoutManager extends LinearLayoutManager {

            //Generate constructors

             @Override
             public boolean supportsPredictiveItemAnimations() {
                 return false;
             }
        }

然后将RecyclerVIew布局管理器设置如下:

recyclerView.setLayoutManager(new CustomLinearLayoutManager(activity));

-----------------解决方案3 ---------------

  • 此解决方案解决了由原因3引起的问题。您需要确保以正确的方式使用notify方法。另外,也可以使用DiffUtil以智能,轻松和流畅的方式处理更改。 Using DiffUtil in Android RecyclerView

-----------------解决方案4 ---------------

  • 由于原因2,您需要检查对回收者列表的所有数据访问,并确保对另一个线程没有任何修改。

答案 11 :(得分:4)

  

这个问题是由RecyclerView Data修改的不同引起的   螺纹

可以确认线程是一个问题,因为我遇到了问题并且RxJava变得越来越受欢迎:确保在您拨打.observeOn(AndroidSchedulers.mainThread())时使用notify[whatever changed]

来自适配器的

代码示例:

myAuxDataStructure.getChangeObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<AuxDataStructure>() {

    [...]

    @Override
    public void onNext(AuxDataStructure o) {
        [notify here]
    }
});

答案 12 :(得分:4)

在我的情况下,每当我调用notifyItemRemoved(0)时,它就崩溃了。原来我设置了setHasStableIds(true)并且在getItemId我刚刚返回了项目位置。 我最终更新它以返回项目的hashCode()或自定义的唯一ID,这解决了问题。

答案 13 :(得分:3)

仅在以下情况下才出现问题:

我使用空列表创建了适配器。 然后我插入了项目并调用了notifyItemRangeInserted

<强>解决方案:

我通过在获得第一块数据并立即用它初始化后创建适配器来解决这个问题。然后可以插入下一个块并且notifyItemRangeInserted调用没有问题。

答案 14 :(得分:3)

就我而言,我遇到了这个问题,因为从服务器获取数据更新(我正在使用Firebase Firestore),而第一组数据正在后台由DiffUtil处理,另一组数据更新到来并导致启动另一个DiffUtil的并发问题。

简而言之,如果您在后台线程上使用DiffUtil然后返回到主线程以将结果分派给RecylerView,那么当多个数据更新在短时间内到达时,您就有机会获得此错误。 / p>

我按照这个精彩解释中的建议解决了这个问题: https://medium.com/@jonfhancock/get-threading-right-with-diffutil-423378e126d2

只是解释解决方案是在当前的更新运行到Deque时推送更新。然后,deque可以在当前更新完成后运行挂起的更新,从而处理所有后续更新,同时避免不一致错误!

希望这会有所帮助,因为这个让我抓狂!

答案 15 :(得分:3)

在我的情况下,我正在使用mRecyclerView.post(新的Runnable ...)更改先前在线程内的数据,然后再次更改UI线程中的数据,这导致了不一致。

答案 16 :(得分:2)

错误可能是由于您的更改与您通知的内容不一致而导致的。就我而言:

myList.set(position, newItem);
notifyItemInserted(position);

我当然要做的事情:

myList.add(position, newItem);
notifyItemInserted(position);

答案 17 :(得分:2)

在我的情况下,问题是当新加载的数据量小于初始数据时我使用了notifyDataSetChanged。 这种方法对我有所帮助:

adapter.notifyItemRangeChanged(0, newAmountOfData + 1);
adapter.notifyItemRangeRemoved(newAmountOfData + 1, previousAmountOfData);

答案 18 :(得分:2)

我的问题是,即使我清除包含回收站视图的数据模型的数组列表,我也没有通知适配器该更改,因此它具有来自先前模型的陈旧数据。这引起了对观点持有者立场的困惑。要解决此问题,请始终在再次更新之前通知适配器数据集已更改。

答案 19 :(得分:1)

我遇到了同样的问题。

我的应用程序使用导航组件以及包含我的recyclerView的片段。第一次加载该片段时,我的列表显示得很好...但是导航离开并再次出现此错误。

浏览时,片段生命周期仅通过onDestroyView进行,返回时,其生命周期始于onCreateView。但是,我的适配器已在片段的onCreate中初始化,返回时未重新初始化。

解决方法是在onCreateView中初始化适配器。

希望这可能对某人有所帮助。

答案 20 :(得分:0)

我遇到了同样的问题而且我已经读到这只发生在三星手机上......但实际情况表明,这种情况发生在很多品牌中。

经过测试,我意识到只有当您快速滚动RecyclerView然后使用后退按钮或向上按钮返回时才会发生这种情况。所以我把里面的按钮和onBackpressed放在下面的片段:

someList = new ArrayList<>();
mainRecyclerViewAdapter = new MainRecyclerViewAdapter(this, someList, this);
recyclerViewMain.setAdapter(mainRecyclerViewAdapter);
finish();

使用此解决方案,您只需将新的Arraylist加载到适配器,将新适配器加载到recyclerView,然后完成活动。

希望它有助于某人

答案 21 :(得分:0)

我收到此错误是因为我不小心两次调用了“ notifyItemInserted”。

答案 22 :(得分:0)

就我而言,列表中有5000多个项目。 我的问题是,在滚动回收器视图时,有时会在“ myCustomAddItems”方法更改列表时调用“ onBindViewHolder”。

我的解决方案是将“ synchronized(syncObject){}”添加到更改数据列表的所有方法中。 这样,在任何时候,只有一种方法可以读取此列表。

答案 23 :(得分:0)

就我而言,适配器数据已更改。我错误地将notifyItemInserted()用于这些更改。当我使用notifyItemChanged时,错误消失了。

答案 24 :(得分:0)

我收到此错误是因为我错误地调用了一种方法来多次从recyclelerview中删除特定行。我有一个方法:

void removeFriends() {
    final int loc = data.indexOf(friendsView);
    data.remove(friendsView);
    notifyItemRemoved(loc);
}

我不小心调用了这个方法三次而不是一次,所以第二次loc为-1,并且在尝试删除它时给出了错误。这两个修复是为了确保方法只被调用一次,并且还要添加如下的健全性检查:

void removeFriends() {
    final int loc = data.indexOf(friendsView);
    if (loc > -1) {
        data.remove(friendsView);
        notifyItemRemoved(loc);
    }
}

答案 25 :(得分:0)

在列表中删除和更新项目时,我遇到了同样的问题... 经过几天的调查,我认为我终于找到了解决方案。

您需要做的是先执行列表中的所有notifyItemChanged,然后再按降序执行所有notifyItemRemoved

我希望这将对遇到相同问题的人们有所帮助...

答案 26 :(得分:0)

我正在使用游标,所以我不能使用流行答案中建议的DiffUtils。为了使它对我有用,我在列表不空闲时禁用了动画。这是解决此问题的扩展名:

 fun RecyclerView.executeSafely(func : () -> Unit) {
        if (scrollState != RecyclerView.SCROLL_STATE_IDLE) {
            val animator = itemAnimator
            itemAnimator = null
            func()
            itemAnimator = animator
        } else {
            func()
        }
    }

然后您可以像这样更新适配器

list.executeSafely {
  adapter.updateICursor(newCursor)
}

答案 27 :(得分:0)

如果在多点触摸后出现问题,您可以通过以下方式禁用多点触摸

android:splitMotionEvents="false" 

在布局文件中。

答案 28 :(得分:0)

当RecyclerView滚动时有更新数据时,我遇到了同样的问题。 然后使用以下解决方案对其进行修复:

  1. 在更新数据之前停止滚动我们的RecyclerView。
  2. 自定义布局管理器喜欢上面的答案。
  3. 使用DiffUtils确保更新数据正确。

答案 29 :(得分:0)

感谢@Bolling的想法 我有支持避免List中任何可为空的工具

public void setList(ArrayList<ThisIsAdapterListObject> _newList) {
    //get the current items
    if (ThisIsAdapterList != null) {
        int currentSize = ThisIsAdapterList.size();
        ThisIsAdapterList.clear();
        //tell the recycler view that all the old items are gone
        notifyItemRangeRemoved(0, currentSize);
    }

    if (_newList != null) {
        if (ThisIsAdapterList == null) {
            ThisIsAdapterList = new ArrayList<ThisIsAdapterListObject>();
        }
        ThisIsAdapterList.addAll(_newList);
        //tell the recycler view how many new items we added
        notifyItemRangeInserted(0, _newList.size());
    }
}

答案 30 :(得分:0)

通过将回收器视图更新为 last version

为我解决了
implementation "androidx.recyclerview:recyclerview:1.2.1"

答案 31 :(得分:-1)

如果您的数据发生了很大变化,您可以使用

 mAdapter.notifyItemRangeChanged(0, yourData.size());

或数据集中的某些项目发生变化,您可以使用

 mAdapter.notifyItemChanged(pos);

有关详细的方法用法,您可以在某种程度上参考doc,尽量不要直接使用mAdapter.notifyDataSetChanged()

答案 32 :(得分:-1)

我遇到了同样的问题。像这样重新创建新列表可以很好地解决问题:

// 适配器

public void updateItems(List<ComponentEntity> newItems) {
    List<ComponentEntity> updatedItems = new ArrayList<>();
    updatedItems = newItems;
    items = updatedItems;
    notifyDataSetChanged();
}