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
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.
Thanks for solution Joaquim Ley. This helps create recyclerView
horizontal
pager without using any library.
Init recyclerView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vocabulary_list);
// Set up the recyclerView with the sections adapter.
mRecyclerView = findViewById(R.id.list);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(new VocabularyListAdapter<>(vocabularyList));
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE){
int position = getCurrentItem();
onPageChanged(position);
}
}
});
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
}
public boolean hasPreview() {
return getCurrentItem() > 0;
}
public boolean hasNext() {
return mRecyclerView.getAdapter() != null &&
getCurrentItem() < (mRecyclerView.getAdapter().getItemCount()- 1);
}
public void preview() {
int position = getCurrentItem();
if (position > 0)
setCurrentItem(position -1, true);
}
public void next() {
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter == null)
return;
int position = getCurrentItem();
int count = adapter.getItemCount();
if (position < (count -1))
setCurrentItem(position + 1, true);
}
private int getCurrentItem(){
return ((LinearLayoutManager)mRecyclerView.getLayoutManager())
.findFirstVisibleItemPosition();
}
private void setCurrentItem(int position, boolean smooth){
if (smooth)
mRecyclerView.smoothScrollToPosition(position);
else
mRecyclerView.scrollToPosition(position);
}
This is the extension function in Kotlin:
private fun RecyclerView.getCurrentPosition() : Int {
return (this.layoutManager as LinearLayoutManager?)!!.findFirstVisibleItemPosition()
}
You can use it just invoking this function on your RecyclerView
:
val position = yourRecyclerView.getCurrentPosition()
For a similar requirement plus some action needed to be performed on scroll state changed, I did this with a custom scroll listener:
class OnScrollListener(private val action: () -> Unit) : RecyclerView.OnScrollListener() {
private var actionExecuted: Boolean = false
private var oldScrollPosition: Int = 0
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
when (newState) {
RecyclerView.SCROLL_STATE_DRAGGING -> actionExecuted = false
RecyclerView.SCROLL_STATE_SETTLING -> actionExecuted = false
RecyclerView.SCROLL_STATE_IDLE -> {
val scrolledPosition =
(recyclerView.layoutManager as? LinearLayoutManager)?.findFirstVisibleItemPosition() ?: return
if (scrolledPosition != RecyclerView.NO_POSITION && scrolledPosition != oldScrollPosition && !actionExecuted) {
action.invoke()
actionExecuted = true
oldScrollPosition = scrolledPosition
}
}
}
}
}
This is because of the pre-existing bug with RecyclerView onScroll which happens to trigger the callback 2-3 or even more times. And if there is some action to be performed, that will end up executing multiple times.
You are trying to get the info on the wrong object. It is not the RecyclerView
nor the Adapter
responsibility but the RecyclerView's LayoutManager.
Instead of the generic ViewTreeObserver.OnScrollChangedListener()
I would recommend to add instead the RecyclerView.OnScrollListener
and use the onScrollStateChanged(RecyclerView recyclerView, int newState)
callback which gives you the newState
, you should use SCROLL_STATE_IDLE
to fetch its position. Meaning:
yourRecyclerView.getLayoutManager().findFirstVisibleItemPosition();
As Rik van Velzen pointed out, you probably need to cast your
LayoutManager
to aLinearLayoutManager
orGridLayoutManager
(you have to cast to the correct type you are using) to access thesefindVisibleXXXX()
methods.
On said callback method. Hope I made this clear enough for your, you can find documentation on the classes here:
RecyclerView.OnScrollListener
yigit's (Google) response on visible positions