我在使用Fragment的add(即用hide(),和show())的方式作fragment的页面切换时,总是会出现重叠现象,一直找不到解决方法,希望各位大神能够帮助小弟找到解决方法。谢谢!
private void tableFragment(int index) { FragmentTransaction Transaction = getSupportFragmentManager() .beginTransaction(); // Fragmenthide(Transaction); // 清除选中状态 clearfragment(Transaction); switch (index) { case INDEX_GRAGB:// 抢单 tvfragb.setTextColor(this.getResources().getColor( R.color.main_background)); if (gragbasingle == null) { gragbasingle = new GragbASingleFragment(); if (!gragbasingle.isAdded()) { Transaction.add(R.id.ft_content, gragbasingle); } } else { Transaction.show(gragbasingle); } // gragbasingle = new GragbASingleFragment(); // Transaction.replace(R.id.ft_content, gragbasingle); break; case INDEX_QUERY:// 查询 tvquery.setTextColor(this.getResources().getColor( R.color.main_background)); if (queryfragment == null) { queryfragment = new QueryFragment(); if (!queryfragment.isAdded()) { Transaction.add(R.id.ft_content, queryfragment); } } else { Transaction.show(queryfragment); } // queryfragment = new QueryFragment(); // Transaction.replace(R.id.ft_content, queryfragment); break; case INDEX_APPOIN:// 预约 tvappo.setTextColor(this.getResources().getColor( R.color.main_background)); if (appoment == null) { appoment = new Appointment(); if (!appoment.isAdded()) { Transaction.add(R.id.ft_content, appoment); } } else { Transaction.show(appoment); } // appoment = new Appointment(); // Transaction.replace(R.id.ft_content, appoment); break; default: break; } Transaction.commitAllowingStateLoss(); } /** * 清除fragment状态 */ private void clearfragment(FragmentTransaction Transaction) { hideFragment(gragbasingle, Transaction); hideFragment(appoment, Transaction); hideFragment(queryfragment, Transaction); colorText(tvfragb); colorText(tvappo); colorText(tvquery); } /** * 隐藏Fragment * * @param fragment * @param Transaction */ private void hideFragment(Fragment fragment, FragmentTransaction Transaction) { if (fragment != null) { if (!fragment.isHidden()) { Transaction.hide(fragment); } } } /** * 字体颜色归原 * * @param textview */ private void colorText(TextView textview) { textview.setTextColor(this.getResources().getColor( R.color.hide_background)); }
因为没有保存的状态应用程序崩溃,前一个 fragment 又调用一次。所以要在 manifest 中添加:
<activity android:name=".yourActivity" android:c>
汗,上面漏掉了!
<activity android:name=".yourActivity" android:c>
@雾海中的那粟光: 设置的什么?
一个很笨的办法,就是将覆盖住的fragment的所用view或者根view设置成Gone或者INVISIBLE
额,用这种方法缺陷太大了。但还是要谢谢你!
可以采用
/**
* 压入栈管理
*
* @param fragment
* @param tag
*/
private void push(Fragment fragment, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_container, fragment, tag);
ft.addToBackStack(tag);
ft.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
}
protected <T> T findFragmentByTag(String tag) {
return (T) getSupportFragmentManager().findFragmentByTag(tag);
}
/**
* 主页
*/
private void goMainIndexFragment() {
if (currentItem == Navigations.MAIN_INDEX) {
return;
}
MainIndexFragment activitiesFragment = findFragmentByTag(MainIndexFragment.className);
push(activitiesFragment == null ? MainIndexFragment.newInstance() : activitiesFragment, MainIndexFragment.className);
}