私はアンドロイドのフラグメントの専門家ではありません。私の最後のプロジェクトではTabActivityを使用していましたが、廃止されたため、Fragmentsを使用してActionBarの実装を開始しています。
これが私のタブクラスのコードです:
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);
}
}
}
表示/非表示を優先するアタッチ/デタッチ方法に関するコメントに注意してください。これは、2 番目のタブで 2 回目に入力したときに例外が発生するためです (2 番目のタブ フラグメントの設計が悪いため)。ここで例外:
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_
(タグと id を削除し<fragment android:name="it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_" />
て問題を解決します..しかし、彼のアダプターを更新するためにそれを取得する方法が必要です!したがって、接続/切断の代わりにフラグメントを表示/非表示にします)。幸いなことに、向きを修正する必要があるため、表示/非表示を使用して向きを変更してもフラグメントが重なる問題はありません。
これがFragmentクラスを拡張したDatiFragmentの最初のタブ表示です。
そして、これは大丈夫そうです!
これは StrumentiFragment を表示する SECOND タブにあります。レイアウトは次のとおりです。
<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 のスピナーから要素を追加すると、FragmentList B が更新されます。フラグメント リスト B から要素を選択すると、フラグメント C の詳細が更新されます。
問題はネストされたフラグメントによって引き起こされる可能性があると思います..しかし、私はそれを別の方法でどのように設計するのかわかりません..おそらく、実際よりも難しいと思います..
私が ActionBarSherlock を使用していることをコードで述べたり示したりすることを省略しましたが、それは私の問題の原因ではないと思うからです!
最後に..質問は次のとおりです。
2 番目の図に示されているような機能的なレイアウトを取得する必要があります。しかし、フラグメントをネストすることによってのみ取得しました..それを避ける必要があります。どのようにできるのか?
返信ありがとうマルコ