Android how can I get current positon on recyclerview that user scrolled to item

后端 未结 5 1498
清歌不尽
清歌不尽 2020-12-25 11:21

In my RecyclerView I have some items that user can scroll and see that. Now I want to save this position and scroll that after come back. This below code return

5条回答
  •  萌比男神i
    2020-12-25 11:55

    I made 7 changes in my MainActivity and got pixel-perfect results. My recyclerview ALWAYS remembers its previous state when returning to it from some other Activity (e.g. DetailActivity).

    Step 1: Initialize the layoutManager like so:

        // Initialize the layout manager
        moviePosterLayoutManager = (moviePosterRecyclerView.getLayoutManager() == null) ?
        new GridLayoutManager(this, numberOfColumns) :  moviePosterRecyclerView.getLayoutManager();
                // create a new layoutManager                       // reuse the existing layoutManager
    

    Step 2: Inside your MainActivity class, create a STATIC variable to hold your layout manager state as you transition between activities:

    private static Parcelable layoutManagerState;
    

    Step 3: Also create this constant inside of the MainActivity () as well:

    public static final String KEY_LAYOUT_MANAGER_STATE = "layoutManagerState";

    Step 4: Inside the onCreate method for your MainActivity perform the following test, (i.e.

    protected void onCreate(@Nullable final Bundle savedInstanceState) {....
    
        if (layoutManagerState != null) {
            //moviePosterLayoutManager
            moviePosterLayoutManager.onRestoreInstanceState(layoutManagerState);
            moviePosterRecyclerView.setLayoutManager(moviePosterLayoutManager);
    
        } else {
    
            moviePosterRecyclerView.setLayoutManager(moviePosterLayoutManager);
            moviePosterRecyclerView.scrollToPosition(moviePosition);
                                     // the default position
    
        }
    

    Step 5: In your onSaveInstanceState method, be sure to save your layoutManagerState as a parcelable like so:

    protected void onSaveInstanceState(Bundle outState) {

       layoutManagerState = moviePosterLayoutManager.onSaveInstanceState();
    
        // Save currently selected layout manager.            
       outState.putParcelable(KEY_LAYOUT_MANAGER_STATE,layoutManagerState);
    }
    

    Step 6: Place similar code in the onResume() method of MainActivity:

    protected void onResume() {
        super.onResume();
    
    
        if (layoutManagerState != null) {
            moviePosterLayoutManager.onRestoreInstanceState(layoutManagerState);
        }
    
    }
    

    Step 7: Remember that all of this code was placed inside of my MainActivity.java file It is important to remember that the layoutManager is responsible for maintaining the scroll position of the recycler view. So, saving the state of the layoutManager is of paramount importance. The code could modularized and placed inside a custom recyclerview, but I found this to be simpler for me to implement.

提交回复
热议问题