我将解释我应用的结构:
活动 - >片段A(在片段(A)内部我有3个片段(B,C,D)(viewpager)) - >在第一个片段(B)中,它包含一个列表视图。该列表中的每个项目都会启动一个片段E。
所以我在这里面临一个问题。
当我第一次启动我的应用程序时,一切看起来都很好。
但是当片段E在屏幕上可见时,标签会变得奇怪,就像:
叶氏!他们得到了重复。 当我点击列表视图时,我提交了活动中回调的2个片段之间的转换:
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, PF);
transaction.addToBackStack(null);
transaction.commit();
当我想回到之前的片段时:
getFragmentManager().popBackStack();
先前的片段是否已重新创建?我能做些什么来解决它?
更新 - 代码: 活动:
public class MainActivity extends FragmentActivity implements
IslamToolsFragment.OnToolsSelectedListener {
FragmentTransaction transaction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.content_frame) != null) {
if (savedInstanceState != null) {
return;
}
PagerActivity firstFragment = new PagerActivity();
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, firstFragment).commit();
}
}
@Override
public void OnToolSelected(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
PrayerFragment PF = new PrayerFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, PF);
transaction.addToBackStack(null);
transaction.commit();
break;
case 1:
QiblaFragment QF = new QiblaFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, QF);
transaction.addToBackStack(null);
transaction.commit();
break;
}
}
片段A(包含3个片段):
public class PagerActivity extends Fragment implements ActionBar.TabListener {
int NUM_PAGES = 5;
ActionBar actionBar;
private ViewPager mPager;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mPager.setAdapter(null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
return;
} else {
actionBar = this.getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
getChildFragmentManager());
mPager.setAdapter(mAppSectionsPagerAdapter);
int icons[] = { R.drawable.ic_action_storage,
R.drawable.ic_action_overflow, R.drawable.ic_action_person };
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setIcon(icons[i])
.setTabListener(this));
}
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View tabsview = inflater.inflate(R.layout.pager_activity, container,
false);
mPager = (ViewPager) tabsview.findViewById(R.id.pager);
return tabsview;
}
Fragnent B:
public class IslamToolsFragment extends Fragment {
OnToolsSelectedListener mCallback;
ListView islamtools;
Fragment PF = new PrayerFragment();
public interface OnToolsSelectedListener {
public void OnToolSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View islamtoolsview = inflater.inflate(R.layout.lvislamtools,
container, false);
if (savedInstanceState != null) {
return islamtoolsview;
}
islamtools = (ListView) islamtoolsview.findViewById(R.id.lvislamtools);
String[] title = { "Prayer Times", "Qibla", "Ahadith", "Quran",
"Hijri Calendar", "99 Names" };
IslamToolsAdapter ITA = new IslamToolsAdapter(this.getActivity(), title);
islamtools.setAdapter(ITA);
islamtools.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
mCallback.OnToolSelected(arg2);
}
});
return islamtoolsview;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnToolsSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
谢谢!它与saveinstancestate有关吗?
答案 0 :(得分:1)
您的代码设计有点不对,片段实际上是一些活动,在这种情况下创建一个包含更多片段的片段对我没有意义,您应该做的是为viewpager创建一个活动,把你的3个片段放到那个活动中。
我有一个类似的应用程序,这就是我处理标签之间导航的方式:
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
上面的代码更改了操作栏中选择的项目但未更改视图寻呼机选项卡下方的视图,您需要执行以下操作来执行此操作:
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}