私はフラグメントを学習しています。以下に私の最初のフラグメントプログラムを示します。2つの画面がある単純なプロジェクト。最初の画面の次のボタンをクリックすると、2番目のボタンが表示される必要があります。
Android 2.1以降をターゲットにしており、互換性パッケージを使用しています
AppMainFragmentActivity.java
public class AppMainFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.app_main_layout);
}
}
app_main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/fragment_container">
<fragment class="com.research.fragmentstudy.FirstFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/id_first_fragment"/>
</LinearLayout>
FirstFragment.java
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment_layout
, container,false);
Button nextButton =(Button) view.findViewById(R.id.button);
nextButton.setOnClickListener(nextListener);
return view;
}
private OnClickListener nextListener = new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = ((AppMainFragmentActivity)FirstFragment.this
.getActivity()).getSupportFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragment);
ft.commit();
}
};
}
first_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/first_fragment_root">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Fragment 1" android:gravity="center_horizontal" />
<Button android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content" android:id="@+id/button"
android:text="Next" />
</LinearLayout>
SecondFragment.java
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_fragment_layout,
container,false);
return view;
}
}
second_fragment_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/first_fragment_root">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Fragment 2" android:gravity="center_horizontal" />
</LinearLayout>
さて、私は最初の画面を大丈夫にしています。今、
フラグメントの理解から期待したこと
- 画面1の[次へ]ボタンをクリックすると、SecondFragmentが作成され、それ
onCreate()
がonCreateView()
呼び出されます。 - SecondFragmentが表示され、FirstFragmentが破棄されます(バックスタックに追加していないため)。デフォルトのフラグメントトランザクションにはアニメーションがないため、アニメーションはありません。
何が起こっている
- SecondFragmentは問題なく作成されて
onCreate()
おり、onCreateView()
呼び出されます。 - ただし、FirstFragmentは画面に残り、2番目のFragmentは表示されません。
今、フラグメントの私の理解は間違っている可能性があります。しかし、フラグメントトランザクションをcommit()するときは、最初のフラグメントを2番目のフラグメントに置き換える必要があると思います(最初のフラグメントは非表示になるか破棄されます)。まあ何も起こっていないようです。何故ですか?最初のフラグメントを手動で破棄/非表示にする必要がありますか?
注:私はそれがそのような基本的なことのための長い質問であることを知っています。しかし、どこでコードをめちゃくちゃにしたのかわからないので、コード全体を入れました。