1

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();
}

}

  • check this. for using onBackPressed() in Fragment. https://stackoverflow.com/a/46425415. and save current Position in onBackPressed() in Fragment – S T Nov 11 '20 at 06:45

3 Answers3

2

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

Piyush Maheswari
  • 635
  • 3
  • 18
1

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.

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
1

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.

Liridon Sadiku
  • 309
  • 3
  • 7
  • do you have any solution for this case? I have the same problem at the moment, but I cant remove the NestesScrollView – dudi Mar 18 '22 at 15:25
  • Maybe you can use RecyclerView with different view types and remove NestedScrollView, in that way I managed to fix this – Liridon Sadiku Mar 22 '22 at 15:03
  • @dudi try to use RecyclerView with different view types, I don't see any other solution for now! – Liridon Sadiku Aug 29 '22 at 11:28
  • You can use this solution and it's so simple: https://stackoverflow.com/questions/44407092/saving-scroll-state-of-nestedscrollview – A.Mamode Oct 04 '22 at 08:23