我在网上搜索了很多关于在一个操作栏标签中有多个片段的可能性。
This question最接近我的需要,但代码不起作用。
是否有其他可能性在一个选项卡中包含多个片段?
这是StartActivity
public class StartActivity extends FragmentActivity implements ActionBar.TabListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(
actionBar.newTab()
.setText("Karte")
.setTabListener(new MapFragmentListener(this)));
}
对应的布局acticity_start
包含用于将被放置在标签中的两个片段的两个帧布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/fragment_realtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
TabListener
看起来像这样:
private class TabListener implements ActionBar.TabListener {
private static final String fragment1_tag = "fragment1_tag";
private static final String fragment2_tag = "fragment2_tag";
private FragmentActivity activity;
private RealtimeFragment fragment1;
private RealtimeFragment fragment2;
public TabListener(FragmentActivity activity) {
this.activity = activity;
android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
fragment1 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment1_tag);
if (fragment1 != null && !fragment1.isDetached()) {
ft.detach(fragment1);
}
fragment2 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment2_tag);
if (fragment2 != null && !fragment2.isDetached()) {
ft.detach(fragment2);
}
ft.commit();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
android.support.v4.app.FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
if(fragment1 == null) {
fragment1 = new RealtimeFragment();
fragmentTransaction.add(R.id.fragment_map, fragment1, fragment1_tag);
} else {
fragmentTransaction.attach(fragment1);
}
if(fragment2 == null) {
fragment2 = new RealtimeFragment();
fragmentTransaction.add(R.id.fragment_realtime, fragment2, fragment2_tag);
} else {
fragmentTransaction.attach(fragment2);
}
fragmentTransaction.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
android.support.v4.app.FragmentTransaction fragementTransaction = activity.getSupportFragmentManager().beginTransaction();
if(fragment1_tag != null)
fragementTransaction.detach(fragment1);
if(fragment2 != null)
fragementTransaction.detach(fragment2);
fragementTransaction.commit();
}
}
类RealtimeFragment
和相应的布局fragment_realtime
如下所示:
public class RealtimeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_realtime, container, false);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
在一个选项卡中,现在应该显示RealtimeFragment
类的两个片段。这些片段只显示一个按钮,但什么也没有显示!为什么?
1条答案
按热度按时间c9x0cxw01#
我在寻找同一主题的解决方案,发现了这个,它派上了用场。
所以引用你的片段并膨胀它,就像一个普通的片段一样