android – Save scroll state in nested RecyclerView – Code Utility

Android – Save Scroll State In Nested Recyclerview – Code Utility

I’ve an issue in my utility. I’ve important record (recycler view). This record retains 20 horizontal recycler views. It seems actually related like Netflix app. I must preserve state of each “carousel” in important record. If I transfer first merchandise to proper, subsequent to left, scroll down (first and second objects are recycled), rotate the display screen, scroll up, I must have restored place of first and second aspect.

I do know that I ought to use linearLayout.saveOnInstanceState and onRestoreState however my drawback is that each carousel is in important record.

configChanges in AndroidManifest.xml cannot come into play…

Thank you!

,

As @Ixx talked about scrollX is at all times 0. You have to make use of layoutManager to avoid wasting and restore scroll place

 val scrollStates = mutableMapOf<Int, Parcelable?>()
override enjoyable onViewRecycled(holder: VH) {
    tremendous.onViewRecycled(holder)

    val key = holder.layoutPosition
    scrollStateskey = holder.childRecycler.layoutManager.onSaveInstanceState()
}

override enjoyable onBindViewHolder(holder: VH, place: Int) {
    val key = holder.layoutPosition
    val state = scrollStateskey
    if(state != null){
        holder.childRecycler.layoutManager.onRestoreInstanceState(state)
    }else{

        holder.childRecycler.layoutManager.scrollToPosition(0)
    }
}

,

You can save scrollX place if you merchandise of RecyclerView is being recycled, after which scroll that a lot when that merchandise is being bind once more:

int scrollXState = new int20; // assuming there are 20 carousel objects

@Override
public void onBindViewHolder(MyViewHolder holder, int place) {
    // setup recycler right here, then put up scrollX after recycler has been laid out
    holder.nestedRecyclerView.put up(() ->
            holder.nestedRecyclerView.setScrollX(scrollXStateholder.getAdapterPosition()));
}

@Override
public void onViewRecycled(MyViewHolder holder) {
    scrollXStateholder.getAdapterPosition() = holder.nestedRecyclerView.getScrollX();
    tremendous.onViewRecycled(holder);
}

class MyViewHolder extends RecyclerView.ViewHolder {

    RecyclerView nestedRecyclerView;

    public MyViewHolder(View merchandiseView) {
        tremendous(merchandiseView);
    }
}

Only factor it is best to deal with is that you don’t want mum or dad adapter to be destroyed and created after orientation change, make it survive orientation change. Otherwise save int into Bundle after which get it again from onRestoreInstanceState().

,

This is the answer i applied:

READ :  [Solved] requires libraries and applications that depend on it to compile against version 33 or later of the Android APIs. :app is currently compiled against android-32

1:Use format supervisor to seek out the lastVisible aspect by utilizing recyclerView.layoutmanager.findLastVisibleItemPosition()

  1. use an interface to ship this worth to your calling exercise or fragment

  2. retailer this worth in a hashmap and cross it to the outer recyclerViewAdapter

  3. cross it to the interior recyclerViewAdapter to replace it to the place saved by utilizing recyclerview.scrollToPositionhashmapofpositionsYOUR_KEY

This approach your state that you just used in the final run is conserved.

Leave a Reply

Your email address will not be published. Required fields are marked *