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.