Don't change the recyclerview adapter in layout passes.

As requested by the big warning that was logged.
multisite^2
Floens 8 years ago
parent 67a230223c
commit 0ea9775396
  1. 54
      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);

Loading…
Cancel
Save