[英]How to trigger onCreateView inside Viewpager when navigating through BottomNavigation?
Problem: Whenever I navigate through my BottomNavigation "onCreateView" is only triggered first time once on my green child fragment.问题:每当我浏览我的底部导航时,“onCreateView”只会在我的绿色子片段上第一次触发一次。 However it works flawlessly on my ParentFragment.但是它在我的 ParentFragment 上完美运行。
Red is the Main Activity with the BottomNavigation.红色是底部导航的主要活动。 Blue is the ParentFragment with the viewpager inside and the green one is the ChildFragment which changes according to the date.蓝色是里面有viewpager的ParentFragment,绿色的是根据日期变化的ChildFragment。 So if I swipe right I get different times for "August 30".因此,如果我向右滑动,我会得到“8 月 30 日”的不同时间。
Now if I navigate to "Settings Section" and then navigate again to "Home Section", "onCreateView" is only triggered on blue ParentFragment but not on green ChildFragment.现在,如果我导航到“设置部分”,然后再次导航到“主页部分”,则“onCreateView”仅在蓝色 ParentFragment 上触发,而不在绿色 ChildFragment 上触发。 However what I want to achieve is everytime when "onCreateView" is triggered on blue ParentFragment it should also be triggered on green ChildFragment.然而,我想要实现的是每次在蓝色 ParentFragment 上触发“onCreateView”时,它也应该在绿色 ChildFragment 上触发。 And if I set "OffscreenPageLimit to 1 it should also be triggered on the Fragments right and left to ChildFragment. I couldn't find a solution for my problem.如果我将“OffscreenPageLimit”设置为 1,它也应该在片段的右侧和左侧触发到 ChildFragment。我找不到解决我的问题的方法。
That's the code snippet of my blue ParentFragment:这是我的蓝色 ParentFragment 的代码片段:
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(requireActivity());
ViewPager2 viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setCurrentItem(1000, false);
viewPager.post(new Runnable() {
public void run() {
// always start at item 1000, so I can swipe left to previous days and swipe right to next days
viewPager.setCurrentItem(1000, false);
//notifyDataSetChanged doesnt trigger "onCreateView" sadly
sectionsPagerAdapter.notifyDataSetChanged();
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// If I swipe right Date changes to August 30 and vice versa it changes to August 28
LocalDate pagerdate = LocalDate.now();
LocalDate days = pagerdate.plusDays(position - 1000);
String month2 = days.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
String dayOfMonth2 = String.valueOf(days.getDayOfMonth());
String monthAndDay2 = month2 + " " + dayOfMonth2;
monthDayText.setText(monthAndDay2);
}
});
viewPager.setOffscreenPageLimit(1);
Thats the code of my Adapter:那是我的适配器的代码:
public class SectionsPagerAdapter extends FragmentStateAdapter {
public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@Override
public int getItemCount() {
// I have 2000 items to "pretend" to have unlimited items because nobody will swipe 1000 times to the left or right side
return 2000;
}
@NonNull
@Override
public Fragment createFragment(int position) {
LocalDate pagerdate = LocalDate.now();
LocalDate days = pagerdate.plusDays(position - 1000);
//I am passing the date to my Fragment because the time is set according to the date
return TabTodayFragment.newInstance(days.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
} }
If anyone has the same problem I found the solution.如果有人有同样的问题,我找到了解决方案。 I replaced my SectionsPagerAdapter Constructor with我将 SectionsPagerAdapter 构造函数替换为
public SectionsPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
Now it works fine!现在它工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.