我正在传递position
从RecyclerView
第一个FirstFragment
到SecondFragment
它的工作原理。滚动那个之后,我又得到了position
,我将它传递给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);
}
}
第二个片段
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);
}
}