What's Fragment?
Fragmentation (Fragment) is a UI fragment that can be embedded in an activity.
One, the simple use of fragments
Create two layout files:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" match_parent "
android:orientation=" vertical ">
<button
Android:id= "@+id/button"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center_horizontal"
android:text= "button"
/>
</linearlayout >
//left_fragment.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" match_parent "
android:background=" #00ff00 "
android:orientation=" Vertical ">
<textview
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "
android:layout_gravity=" center_horizontal "
android:textsize=" 20sp "
android:text=" this are right Fragment "
/>
</LinearLayout>
//right_fragment.xml
All custom Fragment need to inherit the Fragment class:
public class Leftfragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.left_fragment, container, FALSE); return view;
}
public class Rightfragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.right_fragment, container, FALSE); return view;
}
Final definition Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" match_parent ">
<fragment
android:id=" @+id/left_fragment
" Android:name= "Com.example.fragmenttest.LeftFragment"
android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"/>
<fragment
android:id= "@+id/right_fragment"
Android:name= "Com.example.fragmenttest.RightFragment"
android:layout_width= "0DP"
android:layout_ height= "Match_parent"
android:layout_weight= "1"/>
</LinearLayout>
Ii. Dynamic addition of fragments
You can add fragmentation dynamically in your code:
@Override public
void OnClick (View v) {
switch (V.getid ()) {case
R.id.button:
anotherrightfragment fragment = new Anotherrightfragment ();
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction transaction = Fragmentmanager.
BeginTransaction ();
Transaction.replace (r.id.right_layout, fragment);
Transaction.commit ();
break;
Default: Break
;
}}
Dynamic add fragmentation is mainly divided into 5 steps.
1, create the fragment instance that you want to add.
2, obtain to Fragmentmanager, in the activity can call Getfragmentmanager () method to obtain directly.
3, open a transaction, by invoking the BeginTransaction () method to open.
4, to add debris to the container, generally using the Replace () method implementation, you need to pass in the container ID and the fragment instance to be added.
5, commit the transaction, call the Commit () method to complete.
Simulate the return stack in fragments
By clicking on the button to add a fragment, then press the back key program will exit directly.
public class Mainactivity extends activity implements Onclicklistener {
...
@Override public
void OnClick (View v) {
switch (V.getid ()) {case
R.id.button:
anotherrightfragment fragment = new Anotherrightfragment ();
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction transaction = Fragmentmanager.
BeginTransaction ();
Transaction.replace (r.id.right_layout, fragment);
Transaction.addtobackstack (null);
Transaction.commit ();
break;
Default: Break;}}}
Iv. communication between debris and activities
To facilitate communication between fragmentation and activity, Fragmentmanager provides a method similar to Findviewbyid () that is specifically used to obtain an instance of a fragment from a layout file, as shown in the following code:
Rightfragment rightfragment = (rightfragment) Getfragmentmanager ()
. Findfragmentbyid (r.id.right_fragment);
In each fragment, you can get the active instance associated with the current fragment by calling the Getactivity () method, as shown in the following code:
Mainactivity activity = (mainactivity) getactivity ();
V. The life cycle of debris
Run state: When a fragment is visible, and the activity it is associated with is running, the fragment is also in the running state.
Paused state: When an activity enters a paused state (because another activity that is not covered by the screen is added to the top of the stack), the visible fragments associated with it enter the paused state.
Stop state: When an activity enters a stop state, the fragment associated with it enters the stop state.
Destruction Status: Fragmentation is always attached to the activity, so when the activity is destroyed, the fragment associated with it enters the destruction state.
Here are some of the callback methods for fragmentation:
- Onattach () Called when fragmentation and activity are associated.
- Called when Oncreateview () creates a view (load layout) for fragmentation.
- Onactivitycreated () Make sure that the activity associated with the fragment must have been created when it is called.
- Ondestroyview () Called when the view associated with the fragment is removed.
- Ondetach () Called when fragmentation and activity are disassociate.
This is all about the fragmented fragment of Android, and hopefully it will help you learn.