how to prevent recyclerview scroll to top automatically in fragment onResume

Viewed 876

I faced a problem with my recyclerview and fragments . It made me crazy . My English is not so good but I try to explain it. I have a view pager with 2 different fragments and these two fragments load a same fragments with different data . this inner fragment load data to a recyclerview . loading data run on inner fragment OnResume() method (first checked if adapter is null then load data to recyclerview . ) The problem is here when I clicked on recyclerview items it start new activity and when I back from activity to fragment recyclerview jump to top of the list . However it is not loading new data . I tried to save recyclerview state on onPause method and restore it on onResume but it is not work . if I do it with delay it works but first it jump to top and then back to last item and it is not good at all .bellow code

/* new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               // if(state !=null)
                recyclerViewFragmentChannel.getLayoutManager().onRestoreInstanceState(mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE));
            }
        }, 1000);*/

I am wondering if anybody can help me to overcome with this problem .I want to prevent recyclerview jump to top. I attached my inner fragment codes to make my explain more clear .

my inner fragment :

public class Fragment_Channels extends Fragment {

private static final String ARG_COUNT = "param1";
private Integer counter;
private Adapter adapter;
private View view;
private List<Fragment> fragmentsList;
private RecyclerView recyclerViewFragmentChannel;
private GridLayoutManager layoutManagerPortrait, layoutManagerLandScape;
private Parcelable state;
private Bundle mBundleRecyclerViewState;
private String KEY_RECYCLER_STATE = "recycler_state";

public Fragment_Channels() {
    // Required empty public constructor
}

public static Fragment_Channels newInstance(Integer counter) {
    Fragment_Channels fragment = new Fragment_Channels();
    Bundle args = new Bundle();
    args.putInt(ARG_COUNT, counter);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    fragmentsList = getActivity().getSupportFragmentManager().getFragments();

    if (getArguments() != null) {
        counter = getArguments().getInt(ARG_COUNT);
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (view == null) {

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_channels, container, false);

        recyclerViewFragmentChannel = view.findViewById(R.id.recyclerViewFragmentChannel);
        
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

            layoutManagerLandScape = new GridLayoutManager(view.getContext(), 5);
            recyclerViewFragmentChannel.setLayoutManager(layoutManagerLandScape);

           
        } else {
            layoutManagerPortrait = new GridLayoutManager(view.getContext(), 2);
            recyclerViewFragmentChannel.setLayoutManager(layoutManagerPortrait);
        }
    }
    return view;
}


@Override
public void onPause() {
    super.onPause();
    mBundleRecyclerViewState = new Bundle();
    state = layoutManagerPortrait.onSaveInstanceState();
}

@Override
public void onResume() {
    super.onResume();

    if ((recyclerViewFragmentChannel.getAdapter() == null)) {

    //Load data to recyclerview here
       
    } else {
        
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               // if(state !=null)
                layoutManagerPortrait.onRestoreInstanceState(mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE));
            }
        }, 1000);
    }
}
2 Answers

If You want to make RecyclerView be in the same position as it was before going to 2nd fragment You can save the actual position in shared preferences. When You back to 1st fragment just load value from shared preferences and use method to scroll to a given position. Scroll RecyclerView programmatically

recyclerView.smoothScrollToPosition(savedPosition); // if You want smooth scroll 

recyclerView.scrollToPosition(savedPosition); // if You want instant scroll 

Add android:descendantFocusability="blocksDescendants" in outer or parent layout of recyclerview.

Related