Jak zaimplementować FragmentManager i FragmentTransaction, aby zastąpić pojedynczy fragment?
Mam aktywność z 3 fragmentami; nagłówek; Ciało; stopka (ten sam punkt, co w HTML). Bodyfragment zawiera trzy przyciski, z których każdy powinien zastąpić środkowy fragment (ciało; sam) innym, ale nie mogę dowiedzieć się, jak działa tutaj FragmentManager i FragmentTransition. Wydaje mi się, że nie mogę znaleźć żadnej spójności w pytaniach innych ludzi tutaj, w odniesieniu do sposobu, w jaki inni implementują swoje fragmenty. Wydaje się, że każdy ma swoje własne metody lub po prostu nie zawiera pełnego kodu w swoich wątkach.
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.test_frag);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TestFragment.java
public class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_frag, container, false);
}
}
BodyFragment.java
public class BodyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.body_frag, container, false);
}
}
Fragment w formacie XML
<fragment
android:id="@+id/bodyfragment"
android:name="com.example.test.BodyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/body_frag" />
Układ BodyFragment w formacie XML (przycisk x3)
<Button
android:id="@+id/bsettings"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/bgames"
android:layout_alignBottom="@+id/bgames"
android:layout_toRightOf="@+id/bgames"
android:text="SETTINGS" />