From 0ea97753965c30df7de487405dbd9abbff96317a Mon Sep 17 00:00:00 2001 From: Floens Date: Wed, 14 Jun 2017 16:08:50 +0200 Subject: [PATCH] Don't change the recyclerview adapter in layout passes. As requested by the big warning that was logged. --- .../chan/ui/layout/ThreadListLayout.java | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/ui/layout/ThreadListLayout.java b/Clover/app/src/main/java/org/floens/chan/ui/layout/ThreadListLayout.java index 741da8ac..50bc0c64 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/layout/ThreadListLayout.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/layout/ThreadListLayout.java @@ -21,6 +21,8 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; +import android.os.Handler; +import android.os.Looper; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -77,6 +79,15 @@ public class ThreadListLayout extends FrameLayout implements ReplyLayout.ReplyLa private int lastPostCount; private int recyclerViewTopPadding; + private Handler mainHandler = new Handler(Looper.getMainLooper()); + + private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + onRecyclerViewScrolled(); + } + }; + public ThreadListLayout(Context context, AttributeSet attrs) { super(context, attrs); } @@ -102,24 +113,7 @@ public class ThreadListLayout extends FrameLayout implements ReplyLayout.ReplyLa postAdapter = new PostAdapter(recyclerView, postAdapterCallback, postCellCallback, statusCellCallback); recyclerView.setAdapter(postAdapter); - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - // onScrolled can be called after cleanup() - if (showingThread != null) { - int[] indexTop = getIndexAndTop(); - - showingThread.loadable.setListViewIndex(indexTop[0]); - showingThread.loadable.setListViewTop(indexTop[1]); - - int last = getCompleteBottomAdapterPosition(); - if (last == postAdapter.getItemCount() - 1 && last > lastPostCount) { - lastPostCount = last; - ThreadListLayout.this.callback.onListScrolledToBottom(); - } - } - } - }); + recyclerView.addOnScrollListener(scrollListener); attachToolbarScroll(true); @@ -128,6 +122,30 @@ public class ThreadListLayout extends FrameLayout implements ReplyLayout.ReplyLa searchStatus.getPaddingRight(), searchStatus.getPaddingBottom()); } + private void onRecyclerViewScrolled() { + // onScrolled can be called after cleanup() + if (showingThread != null) { + int[] indexTop = getIndexAndTop(); + + showingThread.loadable.setListViewIndex(indexTop[0]); + showingThread.loadable.setListViewTop(indexTop[1]); + + int last = getCompleteBottomAdapterPosition(); + if (last == postAdapter.getItemCount() - 1 && last > lastPostCount) { + lastPostCount = last; + + // As requested by the RecyclerView, make sure that the adapter isn't changed + // while in a layout pass. Postpone to the next frame. + mainHandler.post(new Runnable() { + @Override + public void run() { + ThreadListLayout.this.callback.onListScrolledToBottom(); + } + }); + } + } + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);