Fragment
与 FragmentPagerAdapter
相关联,代码
public class LeaveAdapterApproval extends FragmentPagerAdapter{
private static final String TAG = LeaveAdapterApproval.class.getSimpleName();
private static final int FRAGMENT_COUNT = 4;
public LeaveAdapterApproval(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Log.d(TAG, "LOG : CURRENT FRAGMENT LeaveFragmentToAll");
return new LeaveFragmentToAll();
case 1:
Log.d(TAG, "LOG : CURRENT FRAGMENT LeaveFragmentToPending");
return new LeaveFragmentToPending();
case 2:
Log.d(TAG, "LOG : CURRENT FRAGMENT LeaveFragmentToApproved");
return new LeaveFragmentToApproved();
case 3:
Log.d(TAG, "LOG : CURRENT FRAGMENT LeaveFragmentToDenied");
return new LeaveFragmentToDenied();
}
return null;
}
@Override
public int getCount() {
return FRAGMENT_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "All";
case 1:
return "Pending";
case 2:
return "Approved";
case 3:
return "Denied";
}
return null;
}
}
并导致Logcat
09-22 15:45:21.259 27234-27234/dan.taaku D/LeaveAdapterApproval: LOG : CURRENT FRAGMENT LeaveFragmentToPending 09-22 15:45:21.259 27234-27234/dan.taaku D/LeaveAdapterApproval: LOG : CURRENT FRAGMENT LeaveFragmentToAll 09-22 15:45:21.259 27234-27234/dan.taaku D/LeaveAdapterApproval: LOG : CURRENT FRAGMENT LeaveFragmentToApproved
我们可以从 Logcat
结果中看到 Fragment
一次加载三个 Fragments
,我在Fragment
打开时自动设置为LeaveFragmentToPending
LeaveClassApproval
public class LeaveClassApproval extends Fragment {
private static final String TAG = LeaveClassApproval.class.getSimpleName();
private TabLayout tabLayout;
private ViewPager viewPager;
public LeaveClassApproval() {
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.hr_employee_leave_class, container, false);
tabLayout = view.findViewById(R.id.tabs);
viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(new LeaveAdapterApproval(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1, true); // I set it here
return view;
}
}
但为什么LeaveClassApproval
打开LeaveFragmentToAll
和LeaveFragmentToApproved
,不应该打开LeaveFragmentToPending
。这是一个错误还是一个Fragment
函数?
我搜索了Google,但未找到答案
那么如何解决呢?
答案 0 :(得分:0)
听起来您的问题是ViewPager本身的默认功能。它有一个名为offscreenPageLimit的变量,其默认值为1.表示它总是将一个页面加载到左侧,一个页面加载到右侧。并且您将无法将其设置为零,因为它背后的android源代码如下所示:
private static final int DEFAULT_OFFSCREEN_PAGES = 1;
public void setOffscreenPageLimit(int limit) {
if (limit < DEFAULT_OFFSCREEN_PAGES) {
Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
DEFAULT_OFFSCREEN_PAGES);
limit = DEFAULT_OFFSCREEN_PAGES;
}
如果你真的想要摆脱这种行为,你可以做的一件事就是根据android源代码构建你自己的Pager类,并简单地尝试删除这个功能。
答案 1 :(得分:0)
默认情况下,ViewPager
会打开当前Fragment
及其两个相邻的(即数据之前的数据和之后的数据)。如果要更改默认行为,请使用viewPager.setOffscreenPageLimit(int numberOfFragmentsToBePreloadedOnEachSideOfTheCurrentFragment)
。但请注意,ViewPager
将始终至少加载其两个邻居。
来自docs:
int getOffscreenPageLimit()
返回将保留到其中任何一侧的页数 处于空闲状态的视图层次结构中的当前页面。默认为 1。