From c6c9d01e96863d3a9ccaf3678f9dbc4a80bf48a2 Mon Sep 17 00:00:00 2001 From: Floens Date: Sun, 26 Jul 2015 12:55:10 +0200 Subject: [PATCH] Don't smooth scroll if a post is more than 20 posts away --- .../chan/ui/layout/ThreadListLayout.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 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 9bf47eb9..3376c6cb 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 @@ -51,6 +51,8 @@ import static org.floens.chan.utils.AndroidUtils.getAttrColor; * A layout that wraps around a {@link RecyclerView} to manage showing posts. */ public class ThreadListLayout extends LinearLayout implements ReplyLayout.ReplyLayoutCallback { + public static final int MAX_SMOOTH_SCROLL_DISTANCE = 20; + private ReplyLayout reply; private TextView searchStatus; private RecyclerView recyclerView; @@ -96,10 +98,10 @@ public class ThreadListLayout extends LinearLayout implements ReplyLayout.ReplyL if (showingThread != null) { switch (postViewMode) { case LIST: - showingThread.loadable.listViewIndex = Math.max(0, ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition()); + showingThread.loadable.listViewIndex = Math.max(0, getTopAdapterPosition()); break; case CARD: - showingThread.loadable.listViewIndex = Math.max(0, ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition()); + showingThread.loadable.listViewIndex = Math.max(0, getTopAdapterPosition()); break; } } @@ -261,13 +263,13 @@ public class ThreadListLayout extends LinearLayout implements ReplyLayout.ReplyL switch (postViewMode) { case LIST: - if (((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition() == 0) { + if (getTopAdapterPosition() == 0) { View top = layoutManager.findViewByPosition(0); return top.getTop() != 0; } break; case CARD: - if (((GridLayoutManager) layoutManager).findFirstVisibleItemPosition() == 0) { + if (getTopAdapterPosition() == 0) { View top = layoutManager.findViewByPosition(0); return top.getTop() != dp(8); // 4dp for the cards, 4dp for this layout } @@ -342,6 +344,11 @@ public class ThreadListLayout extends LinearLayout implements ReplyLayout.ReplyL position = recyclerView.getAdapter().getItemCount() - 1; } + int difference = Math.abs(position - getTopAdapterPosition()); + if (difference > MAX_SMOOTH_SCROLL_DISTANCE) { + smooth = false; + } + if (smooth) { recyclerView.smoothScrollToPosition(position); } else { @@ -376,6 +383,16 @@ public class ThreadListLayout extends LinearLayout implements ReplyLayout.ReplyL callback.requestNewPostLoad(); } + private int getTopAdapterPosition() { + switch (postViewMode) { + case LIST: + return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); + case CARD: + return ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); + } + return -1; + } + public interface ThreadListLayoutCallback { void showThread(Loadable loadable);