From 5f3a7fd74ae0d1851f5bf8d8de56452e277a8643 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 18 Nov 2019 14:33:35 +0800 Subject: [PATCH] Hide the FloatingActionButton in ViewPostDetailActivity when scrolling down but not when scroll down comments by clicking the volume down key or the fab. Fixed more comments cannot be fetched after orientation change in ViewPostDetailActivity. --- .../Activity/ViewPostDetailActivity.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 11b99830..691eef31 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -170,6 +170,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private boolean showToast = false; private boolean isSortingComments = false; private boolean mVolumeKeysNavigateComments; + private boolean mIsSmoothScrolling = false; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller mSmoothScroller; @@ -255,6 +256,61 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mLinearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); + + if (children != null && children.size() > 0) { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (!mIsSmoothScrolling) { + if (dy > 0) { + fab.hide(); + } else { + fab.show(); + } + } + + if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { + int visibleItemCount = mLinearLayoutManager.getChildCount(); + int totalItemCount = mLinearLayoutManager.getItemCount(); + int firstVisibleItemPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + + if ((visibleItemCount + firstVisibleItemPosition >= totalItemCount) && firstVisibleItemPosition >= 0) { + fetchMoreComments(); + } + } + } + + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + mIsSmoothScrolling = false; + } + } + }); + } else { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (!mIsSmoothScrolling) { + if (dy > 0) { + fab.hide(); + } else { + fab.show(); + } + } + } + + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + mIsSmoothScrolling = false; + } + } + }); + } + mSmoothScroller = new LinearSmoothScroller(this) { @Override protected int getVerticalSnapPreference() { @@ -563,10 +619,18 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAdapter.addComments(expandedComments, hasMoreChildren); if (children.size() > 0) { + mRecyclerView.clearOnScrollListeners(); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); + if (!mIsSmoothScrolling) { + if (dy > 0) { + fab.hide(); + } else { + fab.show(); + } + } if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { int visibleItemCount = mLinearLayoutManager.getChildCount(); @@ -578,6 +642,13 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } } + + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + mIsSmoothScrolling = false; + } + } }); } } @@ -636,10 +707,18 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAdapter.addComments(expandedComments, hasMoreChildren); if (children.size() > 0) { + mRecyclerView.clearOnScrollListeners(); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); + if (!mIsSmoothScrolling) { + if (dy > 0) { + fab.hide(); + } else { + fab.show(); + } + } if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { int visibleItemCount = mLinearLayoutManager.getChildCount(); @@ -651,6 +730,13 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } } + + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + mIsSmoothScrolling = false; + } + } }); } if (changeRefreshState) { @@ -991,6 +1077,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } mSmoothScroller.setTargetPosition(nextParentPosition); if (mLinearLayoutManager != null) { + mIsSmoothScrolling = true; mLinearLayoutManager.startSmoothScroll(mSmoothScroller); } } @@ -1007,6 +1094,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } mSmoothScroller.setTargetPosition(nextParentPosition); if (mLinearLayoutManager != null) { + mIsSmoothScrolling = true; mLinearLayoutManager.startSmoothScroll(mSmoothScroller); } }