Issue
I am displaying a go to top button on scroll up on a RecyclerView
. If user scrolls up the button is displayed.
Here is the xml code which is nested in a RelativeLayout
.
<android.support.v7.widget.RecyclerView
android:id="@+id/userFeedsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_goto_top"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="@string/icon_up_arrow"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:visibility="gone"
android:textSize="10sp"
android:layout_centerHorizontal="true"
android:background="@drawable/round_button"/>
Here is the java code
btnGotoTop = (Button) findViewById(R.id.btn_goto_top);
Const.setTypeFace(btnGotoTop, getApplication());
btnGotoTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userFeedsRecyclerView.smoothScrollToPosition(0);
view.setVisibility(View.GONE);
}
});
userFeedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy < 0){
btnGotoTop.setVisibility(View.VISIBLE);
}
else{
btnGotoTop.setVisibility(View.GONE);
}
}
});
Inside the onClick()
method of btnGotoTop
, the RecyclerView
scrolls to the top properly but the button does not disappear.
I tried changing view.setVisibility(View.GONE)
to btnGotoTop.setVisibility(View.GONE)
but the button is displayed even after scrolling is complete.
Solution
Here is how I solved it. Changed visibility of button in onScrollStateChanged() of RecyclerView's OnScrollListener
btnGotoTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
userFeedsRecyclerView.smoothScrollToPosition(0);
view.setVisibility(View.GONE);
headerAppBarLayout.setExpanded(true);
}
});
private boolean isUserFeedRecyclerViewAtTop(){
int numColumns = 2;
int pos[] = new int[numColumns];
((StaggeredGridLayoutManager)(userFeedsRecyclerView.getLayoutManager())).findFirstCompletelyVisibleItemPositions(pos);
return pos[0] == 0;
}
userFeedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState){
case RecyclerView.SCROLL_STATE_IDLE:
if(isUserFeedRecyclerViewAtTop()) {
btnGotoTop.setVisibility(View.GONE);
}
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
break;
case RecyclerView.SCROLL_STATE_SETTLING:
break;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy < 0 && btnGotoTop.getVisibility() == View.GONE){
btnGotoTop.setVisibility(View.VISIBLE);
}
else if (dy > 0 && btnGotoTop.getVisibility() == View.VISIBLE){
btnGotoTop.setVisibility(View.GONE);
}
}
});
Answered By - Chandan Pednekar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.