gpt4 book ai didi

android - 返回android中的上一个选项卡时不显示数据

转载 作者:行者123 更新时间:2023-11-30 02:56:22 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 ViewPager 显示了 4 个选项卡。

tab1 | tab2 | tab3 | tab4

public class Sports_Swipe extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private Sports_SwipeAdapter mAdapter;
private ActionBarSherlock mSherlock;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sports_swipe);

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);

mAdapter = new Sports_SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}
///////////////////////////////////////////////////

public class Sports_SwipeAdapter extends FragmentPagerAdapter {

public Sports_SwipeAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int index) {

switch (index) {
case 0:
// Top Rated fragment activity
return new Tab1();
case 1:
// Games fragment activity
return new Tab2();
case 2:
// Movies fragment activity
return new Tab3();
case 3:
// Games fragment activity
return new Tab4();

case 4:
// Movies fragment activity
return new Tab5();
}

return null;
}

@Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}

/////////////////////////////////////
public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.activity_tab1, container, false);
//Here I get all the fields of Tab1
}
//I did this for remaining tabs also

我的 Activity 是扩展 FragmentActivity。每当页面加载时,我都会显示 tab1 的数据。现在,如果用户单击 tab2,我将获取数据并显示。现在用户点击 tab3 然后我显示 tab3 的数据。到此为止就好了。现在,当用户返回到 tab1 时,tab1 上的数据不可用。它显示的是空白页面。

我应该如何保存选项卡数据或选项卡状态。

谢谢

最佳答案

你只需要通过使用设置离屏页面限制

setOffscreenPageLimit(int 限制)

设置在空闲状态下 View 层次结构中当前页面任一侧应保留的页面数。

所以用

ViewPager.setOffscreenPageLimit(3);

关于android - 返回android中的上一个选项卡时不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191972/

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