2 つの質問があります。まず、誰かに言ってもいいですか、なぜこれがうまくいかないのですか? 私はフラグメントを変更しようとしていますが、彼は何もしません。最初のフラグメントが表示されます。
主な活動:
...
if (savedInstanceState == null) {
fm.beginTransaction()
.add(R.id.firstFragment, new firstFragment()).commit();
}
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switchFragment(R.id.firstFragment, new firstFragment());
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switchFragment(R.id.secondFragment, new secondFragment());
}
});
}
private void switchFragment(int fragId, Fragment frag){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(fragId, frag);
ft.commit();
}
main.xml 内の次のようなフラグメント:
<Fragment
android:id="@+id/firstFragment"
android:name="com.example.firstFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/View01"
android:layout_below="@+id/view1" />
Fragmentclassはこれだけです:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_clublist, container, false);
return view;
}
Fragment は次のようになります。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFF0000"/>
なぜ彼がフラグメントを変更しないのかわかりません。switchFragment が実際に呼び出していることを確認してください。
2 番目の質問: これはフラグメントを変更する良い方法ですか? フラグメントが 10 個ある場合はどうなりますか?
ご協力いただきありがとうございます!:)