gpt4 book ai didi

android - 重新选择选项卡时 fragment 为空白

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:14 25 4
gpt4 key购买 nike

最初 fragment 返回第一个和第二个选项卡对应的 ListView 。但是为第三个选项卡返回空 fragment 。当我再次选择第一个选项卡时,将显示空 fragment 。列表适配器没问题.当我在第二个选项卡中设置适配器时,它工作正常。问题出在 Mainactivity 和 Fragment 中

主要 Activity

public class SwipemainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
private TextView displayTextView;
public static SwipemainActivity instance;

TabLayout tabLayout;
ViewPager viewPager;
Context context;
FragmentAdapterClass fragmentAdapter;

public static Context getInstance() {
return instance;
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipemain);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
instance = this;

tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);

setSupportActionBar(toolbar);

tabLayout.addTab(tabLayout.newTab().setText("NEW"));
tabLayout.addTab(tabLayout.newTab().setText("CONFIRMED"));
tabLayout.addTab(tabLayout.newTab().setText("FINISHED"));

fragmentAdapter = new FragmentAdapterClass(getSupportFragmentManager(), tabLayout.getTabCount());

viewPager.setAdapter(fragmentAdapter);

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab LayoutTab) {

viewPager.setCurrentItem(LayoutTab.getPosition());
}


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

}

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

}
});
}
}

fragment

public class FragmentAdapterClass extends FragmentStatePagerAdapter {

int TabCount;

public FragmentAdapterClass(FragmentManager fragmentManager, int CountTabs) {
super(fragmentManager);

this.TabCount = CountTabs;

}

@Override
public Fragment getItem(int position) {

switch (position) {

case 0:
Tab1 tab1 = new Tab1();
return tab1;

case 1:
Tab2 tab2 = new Tab2();
return tab2;

case 2:
Tab3 tab3 = new Tab3();
return tab3;

default:

return null;
}
}

@Override
public int getCount() {
return TabCount;
}

}

最佳答案

我从不使用此监听器来更改 viewpager fragment 。并使用 setOffScreenPageLimit 告诉 viewpager 需要在内存中保存多少 fragment 。这就是我始终使用 tabbedlayout 和 viewpager 设置 fragment 的方式。

viewPager.setOffscreenPageLimit(AMOUNT_OF_FRAGMENTS);

关于android - 重新选择选项卡时 fragment 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484783/

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