I want to have a button so that the user, if he has done Scroll, can press and return to the beginning of the screen, that is, to the first item.
Problems:
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.HomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Este es -->
<ImageView
android:id="@+id/arrowUpImageView"
style="@style/AppTheme.FilterIcon"
android:layout_above="@id/addFab"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_arrow_up_grey_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/addFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
app:backgroundTint="@color/progressBarColor"
app:srcCompat="@drawable/ic_add_white_24dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/postsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Code:
public class HomeActivity extends AppCompatActivity
implements ViewStub.OnClickListener {
private ImageView mArrowUpImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mArrowUpImageView = findViewById(R.id.arrowUpImageView);
mArrowUpImageView.setOnClickListener(this);
// ScrollListener
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibility = mLinearLayoutManager.findFirstCompletelyVisibleItemPosition();
if (visibility != 0) {
mArrowUpImageView.setVisibility(View.VISIBLE);
} else {
mArrowUpImageView.setVisibility(View.INVISIBLE);
}
}
});
}
@Override
public void onClick(View v) {
if (v == mArrowUpImageView) {
// No funciona
mLinearLayoutManager.scrollToPositionWithOffset(0,0);
}
}
}
The idea is that when the user has done Scroll down a little, by clicking on the button he can return to the beginning of the screen quickly. The button should appear above FloatingActionButton
.
Any ideas?