user14575892
user14575892

Reputation:

Is there any way to retain recyclerview scroll position on back pressed

I have loaded recycler view on main activity with Framelayout if recycler item is click it go to another activity when i back press recyclerview scroll to top automatic but i want previous scroll position help

Please tell me with code i am sooooo newbie add solution in my code please

public class Defence_Fragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";


private String mParam1;
private String mParam2;
RecyclerView recyclerView;
Defence_Adapter defence_adapter;

public Defence_Fragment() {

}

public static Defence_Fragment newInstance(String param1, String param2) {
    Defence_Fragment fragment = new Defence_Fragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_defence_, container, false);

    recyclerView = view.findViewById(R.id.rcviewdef);
    recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));

    FirebaseRecyclerOptions<Items_Models> options =
            new FirebaseRecyclerOptions.Builder<Items_Models>()
                    .setQuery(FirebaseDatabase.getInstance().getReference().child("Defence"), Items_Models.class)
                    .build();

    defence_adapter = new Defence_Adapter(options,getContext());
    recyclerView.setAdapter(defence_adapter);

    return view;
}
@Override
public void onStart() {
    super.onStart();
    defence_adapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    defence_adapter.stopListening();
}

}

Upvotes: 1

Views: 1788

Answers (3)

Liridon Sadiku
Liridon Sadiku

Reputation: 349

Check if your RecyclerView is inside NestedScrollView, if so remove NestedScrollView! In my case that was causing to go to top of RecyclerView on back press while working with Android Navigation Component.

Upvotes: 2

ΓDΛ
ΓDΛ

Reputation: 11080

RecyclerView.Adapter.StateRestorationPolicy

Defines how this Adapter wants to restore its state after a view reconstruction (e.g. configuration change).

Doc

Usage

adapter.stateRestorationPolicy = StateRestorationPolicy.PREVENT_WHEN_EMPTY

The StateRestorationPolicy enum has 3 options:

ALLOW — the default state, that restores the RecyclerView state immediately, in the next layout pass PREVENT_WHEN_EMPTY — restores the RecyclerView state only when the adapter is not empty (adapter.getItemCount() > 0). If your data is loaded async, the RecyclerView waits until data is loaded and only then the state is restored. If you have default items, like headers or load progress indicators as part of your Adapter, then you should use the PREVENT option, unless the default items are added using MergeAdapter. MergeAdapter waits for all of its adapters to be ready and only then it restores the state. PREVENT — all state restoration is deferred until you set ALLOW or PREVENT_WHEN_EMPTY.

Upvotes: 1

Piyush Maheswari
Piyush Maheswari

Reputation: 644

You can use onSavedInstanceState() and onRestoreInstanceState() using parcelable and update layoutManager in the onResume() method.

Upvotes: 2

Related Questions