GridLayoutManager scrollToPosition不滚动

时间:2017-07-07 07:36:06

标签: android android-recyclerview gridlayoutmanager linearlayoutmanager

我将position的{​​{1}}从第一个RecyclerView传递到FirstFragment并且有效。在滚动之后,我再次获得SecondFragment并将其传递给position。但是,当FirstFragment获得FirstFragment时,列表不会滚动到该特定索引。

FirstFragment

position

}

第二片段

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

}

1 个答案:

答案 0 :(得分:0)

您是否也尝试使用RecyclerView?例如:

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

不要忘记注册EventBusEventBus.getDefault().register(this);

修改

您可以使用RecyclerView.smoothScrollToPosition(int position)代替

希望这会有所帮助。 抱歉我的英文。