没有“标准”的方式来解决这个问题,因为不鼓励这种设计风格。不过,我找到了一种让它发挥作用的方法:您将手动设计导航。
您的应用程序应该有一个 Activity,即 FragmentActivity。它有一个 FragmentTabHost 将保存您的每个 TabFragment。
TabFragment 是我创建的抽象类,用于在 TabSpec 中表示您的选项卡。它将管理选项卡内片段的导航和交换。
然后,您创建的各个片段可以在 TabFragment 对象中交换。代码如下:
活动
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
//(TabFragment)s will set this property when created so the Activity can communicate with it
public TabFragment activeFragment;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create tabHost based on .xml layout
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
//add each of your tabs to the tabHost. These are all (TabFragment)s
tabHost.addTab(tabHost.newTabSpec("New Tab").setIndicator("New Tab"),
ExampleTabFragment.class, null);
}
/*override the onBackPressed method, so that your application does not close every
time the user navigates back. Instead, calls the activeFragment to determine behavior*/
@Override
public void onBackPressed() {
activeFragment.onBackPressed();
}
//method for TabFragment to call when the user navigates out of the app
public void close(){
super.onBackPressed();
}
}
TabFragment
public abstract class TabFragment extends Fragment {
@Override
public void onResume(){
//sets the property in the Activity so we can reference this from it.
((MainActivity) getActivity()).activeFragment=this;
super.onResume();
}
//this will be the method called when the back button is pressed. It will navigate.
public abstract void onBackPressed();
}
TabFragment 实例
TabFragment 的实例内部应该是一个 FrameLayout,可以将子 Fragment 附加到该 FrameLayout 上。第一次单击选项卡时,它将启动onCreate() 中指定的片段。从另一个选项卡切换回它后,它将恢复上次显示的任何 Fragment。如果需要分层导航,应使用 onBackPressed() 方法在片段中导航。我使用字节属性 (tabContentIndex) 来确定如何导航。如果您添加一个构造函数来获取此 TabFragment 的实例,则 Fragments 可以将自己交换为其他 Fragment。他们将通过访问start(Example)Fragment() 方法来做到这一点。请记住,“返回”按钮最终必须退出应用程序。
public class NewTrailTabContent extends TabFragment {
//to determine what child Fragment is active
byte tabContentIndex;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//a simple FrameLayout in this case. Child Fragments will be attached.
return inflater.inflate(R.layout.example_fragment, container,
false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
//The tab starts with this Fragment
startDiologFragment();
super.onCreate(savedInstanceState);
}
public void startExampleFragment(){
/*Fragment's constructor allows us to reference the parent to navigate. In effect, this
Fragment will be able to call these navigation methods.*/
ExampleFragment newFragment = new ExampleFragment(this);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
//this Resource is the FrameLayout
fragmentTransaction.replace(R.id.example_contentpane,
newFragment);
fragmentTransaction.commit();
//this is set so the onBackPressed() method knows how to operate.
tabContentIndex =0;
}
public void startTestFragment(){
Fragment testFragment = new TestFragment(this);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.example_contentpane,
testFragment);
fragmentTransaction.commit();
//different contentIndex
tabContentIndex = 1;
}
//this method called by the Activity
@Override
public void onBackPressed() {
//this will close the app because we are at the top of the hierarchy
if(tabContentIndex==0){
((MainActivity)getActivity()).close();
//this will switch to the original content fragment.
}else if(tabContentIndex==1||tabContentIndex==2){
startExampleFragment();
}
}
}