Button to go to the top of the RecyclerView screen

2

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:

  • The Button appears below the RecyclerView
  • When you click on the Button, nothing happens
  • 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?

        
    asked by Robert Gomez 31.08.2018 в 21:22
    source

    1 answer

    0

    You can put 2 FloatingButton, one of them with visibility GONE so that when you reach the end, change to VISIBLE:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.HomeActivity">
    
    <android.support.v4.widget.SwipeRefreshLayout 
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/postsRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
    
    </android.support.v4.widget.SwipeRefreshLayout>
    
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_menu_add" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="70dp"
            android:layout_marginRight="@dimen/fab_margin"
            app:srcCompat="@android:drawable/arrow_up_float" />
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    To make the Srooll use:

    fabUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    postsRecyclerView.smoothScrollToPosition(1);
                }
            });
    
        
    answered by 31.08.2018 / 23:17
    source