我对android的Fragment不是很专业。在我的上一个项目中,我正在使用TabActivity,但由于它已经弃用,我现在开始使用Fragments实现ActionBar。
以下是我的Tab类的代码:
public class TabInterventoClass extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar.newTab()
.setText("Dati")
.setTabListener(new TabListener<DatiFragment_>(this, "dati", DatiFragment_.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Sistemi alimentazione")
.setTabListener(new TabListener<SistemaAlimentazioneFragment_>(this, "Sistemi alimentazione", SistemaAlimentazioneFragment_.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Home")
.setTabListener(new TabListener<HomeFragment_>(this, "home", HomeFragment_.class));
actionBar.addTab(tab);
}
我以这种方式实现了ActionBar.TabListener:
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final FragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
//ft.attach(mFragment);
ft.show(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
//ft.detach(mFragment);
ft.hide(mFragment);
}
}
}
请注意附加/分离方法的注释,以支持显示/隐藏。这是因为在我的第二个选项卡中,我第二次输入时会出现异常(当然是第二个Tab片段的设计不好)。这里例外:
10-18 11:10:13.093: E/AndroidRuntime(3777): Caused by: java.lang.IllegalArgumentException: Binary XML file line #38: Duplicate id 0xffffffff, tag sistemiList, or parent id 0x0 with another fragment for it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_
(删除标签和<fragment android:name="it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_" />
的ID解决问题..但我需要一种方法来获取它以刷新他的适配器!所以我显示/隐藏片段而不是附加/分离)。幸运的是,我必须修改方向,所以当使用show / hide进行方向更改时,我没有问题重叠片段。
这是第一个选项卡显示DatiFragment,它扩展了Fragment类。
这似乎没问题!
这是在SECOND选项卡上,显示StrumentiFragment。这是布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:text="Sistema di alimentazione:" />
<Spinner
android:id="@+id/sistemiDiAlimentazione"
android:layout_height="fill_parent"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Aggiungi"
android:id="@+id/addSistemaAlimentazione" />
</LinearLayout>
<LinearLayout
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment android:name="it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_"
android:layout_width="0dp"
android:tag="sistemiList"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout android:id="@+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
在片段A中添加Spinner中的元素更新FragmentList B.从片段列表B中选择元素更新片段C中的详细信息。
我认为这个问题可能是由嵌套的片段造成的......但是我不知道我是如何用不同的方式设计它的...也许我认为它比实际上更难......
我省略了说并在代码中显示我正在使用ActionBarSherlock,只是因为我认为这不是我的问题的原因!
所以最后......问题是:
我需要获得第二张图片中显示的功能布局。但我只是通过嵌套片段获得它。我必须避免这样做。我该怎么办?
感谢您的回复 马可
答案 0 :(得分:3)
我已经解决了将每个Frame放在FrameLayout中,在需要时显示/隐藏。
我还为我创建ActionTab的类定义了一个自定义布局:
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+android:id/navtabcontent"
android:layout_width="0dp"
android:layout_height="600dp"
android:layout_weight="1"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="0dp"
android:layout_height="600dp"
android:layout_weight="3"/>
</LinearLayout>
</LinearLayout>
我希望这对某人有用!