Search code examples
androidandroid-recyclerviewgridlayoutmanagerlinearlayoutmanager

GridLayoutManager scrollToPosition doesn't scroll


I am passing the position of the RecyclerView from first FirstFragment to SecondFragment and it works. After scrolling in that one, I get the position again and I am passing it to the FirstFragment. But, when FirstFragment gets the position, the list is not scrolled to that particular index.

FirstFragment

public class FirstFragment extends BaseFragment implements FirstAdapter.OnItemInteractionListener {

FragmentFirstBinding mBinder;
List<MyItem> mMyItemList;
GridLayoutManager mManager;
MyItem mMyItem;
FirstAdapter mAdapter;
int mPosition;

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String GET_SECOND_FRAGMENT = "get_second_fragment";

public static FirstFragment newInstance() {
    FirstFragment fragment = new FirstFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMyItemList = new ArrayList<>();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mBinder = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false);
    populate();
    mManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
    mAdapter = new FirstAdapter(getContext(), mMyItemList);
    mBinder.rvFirst.setLayoutManager(mManager);
    mBinder.rvFirst.setAdapter(mAdapter);
    mAdapter.addOnItemInteractionListener(this);
    EventBus.getDefault().register(this);
    return mBinder.getRoot();
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    EventBus.getDefault().unregister(this);
}

private void populate() {
    for (int i = 0; i < 30; i++) {
        mMyItemList.add(i, mMyItem);
    }
}

private void sendAction(String action, List<MyItem> myItemList, int position) {
    if (mListener == null) {
        return;
    }
    Bundle bundle = new Bundle();
    bundle.putString(Constants.ACTION, action);
    bundle.putParcelable(Constants.DATA1, Parcels.wrap(myItemList));
    bundle.putInt(Constants.POSITION, position);
    mListener.onFragmentInteraction(bundle);
}

@Override
public void onItemClick(int position) {
    sendAction(GET_SECOND_FRAGMENT, mMyItemList, position);
}

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onMessage(SendPos event) {
    mPosition = event.getPos();
    mBinder.rvFirst.scrollToPosition(mPosition);
    mBinder.rvFirst.getLayoutManager().scrollToPosition(mPosition);
    EventBus.getDefault().removeStickyEvent(event);
}

}

Second Fragment

public class SecondFragment extends BaseFragment {

FragmentSecondBinding mBinder;
List<MyItem> mMyItemList;
LinearLayoutManager mManager;
Parcelable mParcelable;
SecondAdapter mAdapter;
int mPosition;

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String ACTION_BACK = "back";

public static SecondFragment newInstance(List<MyItem> myItemList, int position) {
    SecondFragment fragment = new SecondFragment();
    Bundle args = new Bundle();
    args.putParcelable(ARG_PARAM1, Parcels.wrap(myItemList));
    args.putInt(ARG_PARAM2, position);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParcelable = getArguments().getParcelable(ARG_PARAM1);
        mMyItemList = Parcels.unwrap(mParcelable);
        mPosition = getArguments().getInt(ARG_PARAM2);
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mBinder = DataBindingUtil.inflate(inflater, R.layout.fragment_second, container, false);
    mManager = new LinearLayoutManager(getContext());
    mAdapter = new SecondAdapter(getContext(), mMyItemList);
    mBinder.rvSecond.setLayoutManager(mManager);
    mBinder.rvSecond.setAdapter(mAdapter);
    mManager.scrollToPosition(mPosition);
    setUIListeners();
    return mBinder.getRoot();
}

private void setUIListeners() {
    mBinder.back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPosition = mManager.findFirstVisibleItemPosition();
            sendPos(mPosition);
            sendActionToActivity(ACTION_BACK);
        }
    });
}

private void sendActionToActivity(String action) {
    Bundle bundle = new Bundle();
    bundle.putString(Constants.ACTION, action);
    mListener.onFragmentInteraction(bundle);
}

private void sendPos(int pos) {
    SendPos sendPos = new SendPos();
    sendPos.setPos(pos);
    EventBus.getDefault().postSticky(sendPos);
}

}


Solution

  • Have you try to use the RecyclerView as well ? for example :

    mRecyclerView.scrollToPosition(mPosition);
    mRecyclerView.getLayoutManager().scrollToPosition(mPosition);
    

    Don't forget to register the EventBus : EventBus.getDefault().register(this);

    EDIT:

    You can use RecyclerView.smoothScrollToPosition(int position) instead

    Hope this helps. Sorry for my english.