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.

This commit is contained in:
Alex Ning 2019-11-18 14:33:35 +08:00
parent d4bf238345
commit 5f3a7fd74a

View File

@ -170,6 +170,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
private boolean showToast = false; private boolean showToast = false;
private boolean isSortingComments = false; private boolean isSortingComments = false;
private boolean mVolumeKeysNavigateComments; private boolean mVolumeKeysNavigateComments;
private boolean mIsSmoothScrolling = false;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private CommentAndPostRecyclerViewAdapter mAdapter; private CommentAndPostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller mSmoothScroller; private RecyclerView.SmoothScroller mSmoothScroller;
@ -255,6 +256,61 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mLinearLayoutManager = new LinearLayoutManager(this); mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager); 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) { mSmoothScroller = new LinearSmoothScroller(this) {
@Override @Override
protected int getVerticalSnapPreference() { protected int getVerticalSnapPreference() {
@ -563,10 +619,18 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mAdapter.addComments(expandedComments, hasMoreChildren); mAdapter.addComments(expandedComments, hasMoreChildren);
if (children.size() > 0) { if (children.size() > 0) {
mRecyclerView.clearOnScrollListeners();
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy); super.onScrolled(recyclerView, dx, dy);
if (!mIsSmoothScrolling) {
if (dy > 0) {
fab.hide();
} else {
fab.show();
}
}
if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { if (!isLoadingMoreChildren && loadMoreChildrenSuccess) {
int visibleItemCount = mLinearLayoutManager.getChildCount(); 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); mAdapter.addComments(expandedComments, hasMoreChildren);
if (children.size() > 0) { if (children.size() > 0) {
mRecyclerView.clearOnScrollListeners();
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy); super.onScrolled(recyclerView, dx, dy);
if (!mIsSmoothScrolling) {
if (dy > 0) {
fab.hide();
} else {
fab.show();
}
}
if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { if (!isLoadingMoreChildren && loadMoreChildrenSuccess) {
int visibleItemCount = mLinearLayoutManager.getChildCount(); 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) { if (changeRefreshState) {
@ -991,6 +1077,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
} }
mSmoothScroller.setTargetPosition(nextParentPosition); mSmoothScroller.setTargetPosition(nextParentPosition);
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
mIsSmoothScrolling = true;
mLinearLayoutManager.startSmoothScroll(mSmoothScroller); mLinearLayoutManager.startSmoothScroll(mSmoothScroller);
} }
} }
@ -1007,6 +1094,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
} }
mSmoothScroller.setTargetPosition(nextParentPosition); mSmoothScroller.setTargetPosition(nextParentPosition);
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
mIsSmoothScrolling = true;
mLinearLayoutManager.startSmoothScroll(mSmoothScroller); mLinearLayoutManager.startSmoothScroll(mSmoothScroller);
} }
} }