gpt4 book ai didi

java - 每当选择相邻选项卡时都会调用选项卡的 onCreateView 吗?

转载 作者:行者123 更新时间:2023-12-02 03:49:00 30 4
gpt4 key购买 nike

我遇到了一个奇怪的问题,onCreateView每次我导航到 TabLayout 中的相邻选项卡时都会被调用。这是我的代码:

news_feed.java:

private static TabLayout tabLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Public"));
tabLayout.addTab(tabLayout.newTab().setText("Friends"));
tabLayout.addTab(tabLayout.newTab().setText("My Tabs"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

PublicTab.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Creating a new view");
fragmentView = inflater.inflate(R.layout.public_tab, container, false);
progressOverlay = fragmentView.findViewById(R.id.progress_overlay);
AndroidUtils.animateView(progressOverlay, View.VISIBLE, 0.9f, 200);
getPublicPosts(progressOverlay, fragmentView);
return fragmentView;
}

我还有另外 3 个与 PublicTab.java 完全相同的选项卡但布局不同。我不知道为什么onCreateView被如此频繁地调用。我认为在生命周期中,onCreateView仅因 2 个原因被调用: 1.当我们第一次初始化选项卡时 2.如果我们从onStop出来或onPause方法。

但是,当在实际选项卡旁边的选项卡之间切换时,println消息的调用方式如上面所示,这不是我想要的,因为我不想更新 View该 fragment 如此频繁地出现:仅在上述前两个原因期间。有谁知道为什么会发生这种情况?任何帮助,将不胜感激!谢谢!

最佳答案

使用setoffScreenPageLimitAndroid 文档:

public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed

The default value is set to 1.

关于java - 每当选择相邻选项卡时都会调用选项卡的 onCreateView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056371/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com