片段 detach/attach 的 Viewpager 意外行为
Viewpager unexpected behaviour with fragment detach/attach
我的主 activity 中有一个框架布局,我正在使用片段管理器 attach/detach 将片段推送到其中以保持片段状态。一切正常,但在我的一个片段中,我有一个显示内部其他片段的 viewpager。当我第一次使用事务 'add' 将此片段添加到我的框架布局时,它工作得很好,但是当我分离并重新附加它时,发生了一些奇怪的事情,viewpager 的内容不存在,当我在选项卡之间滑动时,它像这张图片一样卡住了 https://ibb.co/jw7NswY
这是我的 onViewCreated 片段:
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
groupsViewModel = new ViewModelProvider(this).get(GroupsViewModel.class);
//Setup groups pages
viewPager = view.findViewById(R.id.view_pager_groups);
viewPager.setOffscreenPageLimit(GROUPS_CATEGORIES_COUNT);
viewPager.setAdapter(new GroupsPagerAdapter());
TabLayout tabs = view.findViewById(R.id.tabs_groups);
tabs.setupWithViewPager(viewPager);
tabs.getTabAt(3).setIcon(R.drawable.ic_bookmarked);
//Show/hide add group fab
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
showHideAddGroupFab(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
这就是我在片段之间导航的方式:
private void moveToFragment(MenuItem fragmentNavItem) {
//Get toFragment given its id
int id = fragmentNavItem.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
String tag = getFragmentTagFromItemId(id);
Fragment toFragment = fragmentManager.findFragmentByTag(tag);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.detach(activeFragment);
if (toFragment == null) {
//We go to this fragment for the first time
toFragment = createFragmentFromItemId(id);
transaction.add(R.id.frame_layout_main, toFragment, tag);
} else {
//We have been in this fragment before
transaction.attach(toFragment);
}
transaction.commit();
activeFragment = toFragment;
toolbar.setTitle(fragmentNavItem.getTitle());
}
有人知道为什么会这样吗?
这似乎是 viewpager 的错误,我已迁移到 viewpager2,一切正常,如此处所述https://developer.android.com/training/animation/vp2-migration
我的主 activity 中有一个框架布局,我正在使用片段管理器 attach/detach 将片段推送到其中以保持片段状态。一切正常,但在我的一个片段中,我有一个显示内部其他片段的 viewpager。当我第一次使用事务 'add' 将此片段添加到我的框架布局时,它工作得很好,但是当我分离并重新附加它时,发生了一些奇怪的事情,viewpager 的内容不存在,当我在选项卡之间滑动时,它像这张图片一样卡住了 https://ibb.co/jw7NswY
这是我的 onViewCreated 片段:
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
groupsViewModel = new ViewModelProvider(this).get(GroupsViewModel.class);
//Setup groups pages
viewPager = view.findViewById(R.id.view_pager_groups);
viewPager.setOffscreenPageLimit(GROUPS_CATEGORIES_COUNT);
viewPager.setAdapter(new GroupsPagerAdapter());
TabLayout tabs = view.findViewById(R.id.tabs_groups);
tabs.setupWithViewPager(viewPager);
tabs.getTabAt(3).setIcon(R.drawable.ic_bookmarked);
//Show/hide add group fab
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
showHideAddGroupFab(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
这就是我在片段之间导航的方式:
private void moveToFragment(MenuItem fragmentNavItem) {
//Get toFragment given its id
int id = fragmentNavItem.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
String tag = getFragmentTagFromItemId(id);
Fragment toFragment = fragmentManager.findFragmentByTag(tag);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.detach(activeFragment);
if (toFragment == null) {
//We go to this fragment for the first time
toFragment = createFragmentFromItemId(id);
transaction.add(R.id.frame_layout_main, toFragment, tag);
} else {
//We have been in this fragment before
transaction.attach(toFragment);
}
transaction.commit();
activeFragment = toFragment;
toolbar.setTitle(fragmentNavItem.getTitle());
}
有人知道为什么会这样吗?
这似乎是 viewpager 的错误,我已迁移到 viewpager2,一切正常,如此处所述https://developer.android.com/training/animation/vp2-migration