Когда срабатывает наблюдатель в plannerFragment, вкладка обновляется. Это приводит к тому, что представление recyclerview в localeChoosingFragment сбрасывается в первую позицию. В качестве обходного пути я попытался использовать обработчик для автоматической прокрутки представления recyclerview в нужное место (согласно ViewModel), но я смущен и обеспокоен тем, почему это происходит. Перед использованием ViewPager2 я добавил оба (localeChoosingFragment и localeExploringFragment) в макет кадра вручную и попробовал показать/скрыть и прикрепить/отсоединить, но возникла та же проблема (сброс представления recyclerview в первую позицию).
Кто-нибудь знает, почему так может быть? Это кажется мелочью, но меня беспокоит, что происходит что-то, о чем я не знаю и о чем должен знать, особенно на этой ранней стадии проекта.
Этот ViewPager 2 на самом деле является частью фрагмент, выбранный из другого ViewPager 2, но я не думаю, что это является причиной проблемы.
Я приложу очищенную версию своего кода.
Открытый класс PlannerFragment расширяет Fragment {
Код: Выделить всё
Context mContext;
JapanVeganGuideViewModel japanVeganGuideViewModel;
View plannerFragmentView;
ViewPager2 plannerFragmentViewPager2;
TabLayout tabLayout;
public static final String TAG = "JapanVeganGuideTAG";
public PlannerFragment() {
// Empty constructor for fragment
Toast.makeText(mContext, "Planner Fragment instantiated", Toast.LENGTH_SHORT).show();
}
public PlannerFragment(Context context, JapanVeganGuideViewModel viewModel) {
this.japanVeganGuideViewModel = viewModel;
this.mContext = context;
Log.d(TAG, "Planner Fragment created with context and view model.");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Planner Fragment onCreate called.");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "Planner Fragment onCreateView initiated.");
plannerFragmentView = inflater.inflate(R.layout.fragment_planner, container, false);
return plannerFragmentView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "Planner Fragment onViewCreated executed.");
plannerFragmentViewPager2 = plannerFragmentView.findViewById(R.id.plannerFragmentViewPager2);
tabLayout = plannerFragmentView.findViewById(R.id.plannerFragmentTabLayout);
PlannerFragmentViewPager2Adapter plannerFragmentViewPager2Adapter = new PlannerFragmentViewPager2Adapter(getChildFragmentManager(), getLifecycle(), mContext, japanVeganGuideViewModel);
plannerFragmentViewPager2.setAdapter(plannerFragmentViewPager2Adapter);
plannerFragmentViewPager2.setUserInputEnabled(false);
// TabLayoutMediator configuration to be continued...
публичный класс LocaleChoosingFragment расширяет фрагмент {
Код: Выделить всё
JapanGuideViewModel japanGuideViewModel;
Context mContext;
View localeChoosingFragmentView;
public static final String TAG = "JapanGuideTAG";
RecyclerView localeChoosingRecyclerView;
LinearLayoutManager recyclerViewLayoutManager;
LocaleChoosingRecyclerViewAdapter localeChoosingAdapter;
SnapHelperOneByOne snapHelper;
public LocaleChoosingFragment() {
Log.d(TAG, "Empty constructor called for LocaleChoosingFragment");
}
public LocaleChoosingFragment(Context context, JapanGuideViewModel viewModel) {
this.japanGuideViewModel = viewModel;
this.mContext = context;
Log.d(TAG, "Constructor called for LocaleChoosingFragment");
Toast.makeText(context, "Creating a new LocaleChoosingFragment", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate in LocaleChoosingFragment");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called in LocaleChoosingFragment");
localeChoosingFragmentView = inflater.inflate(R.layout.fragment_locale_choosing, container, false);
localeChoosingRecyclerView = localeChoosingFragmentView.findViewById(R.id.localeChoosingRecyclerView);
return localeChoosingFragmentView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated in LocaleChoosingFragment.");
localeChoosingAdapter = new LocaleChoosingRecyclerViewAdapter();
// ... rest of the code continues
}
Я не ожидал, что RecyclerView в LocaleChoosingFragment сбросится в первую позицию, когда наблюдатель в plannerFragment является триггом
Код: Выделить всё
your text
Подробнее здесь: https://stackoverflow.com/questions/782 ... ab-updated