我正在尝试使用this教程将我的应用转换为TabNavigation布局。第一个和最后一个标签布局始终相同(只是内容更改),每个应用中只有第二个不同。所以我希望从第二个标签管理其他标签,例如在第一个是imageView,从第二个选项卡我可以firstTab.imageView.setImageResources(/ somePic)来显示一些图片。它工作正常,但我注意到应用程序从tab1开始并显示图片,然后我切换到tab2并返回到tab1,它仍然存在,但当我转到tab3-> tab1或tab3-> tab2- > tab1 pictire已经不见了,所以我必须去tab2-> tab1 再次设置图片。为什么会发生这种情况以及如何避免这种情况?我是android的初学者,所以非常感谢你的帮助。
TabActivity:
public class TabActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabReselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Podgląd").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Dane").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Rozwiązanie").setTabListener(tabListener));
}
}
TabPagerAdapter:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new Review();
case 1:
//Fragment for Ios Tab
return new Data();
case 2:
//Fragment for Windows Tab
return new Solution();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs
}
}
数据类(第二个标签)
public class Data extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.ios_frag, container, false);
((TextView)ios.findViewById(R.id.textView)).setText("iOS");
Review.imageView.setImageResource(R.drawable.ic_launcher);
return ios;
}}
复习课程(第一个标签)
public class Review extends Fragment {
public static ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.android_frag, container, false);
//((TextView)android.findViewById(R.id.textView)).setText("Android");
imageView = (ImageView)android.findViewById(R.id.imageView);
return android;
} }
解决方案类(第三个标签)
public class Solution extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.windows_frag, container, false);
((TextView)windows.findViewById(R.id.textView)).setText("Windows");
return windows;
}}
我现在已经阅读了有关使用Bundle发送数据的原因,因为它保存了应用状态,但它不起作用,我认为这是因为标签类没有扩展Activity,我错了吗?