I. Use of Fragment1.AndroidManifest.xml files
<?xml version= "1.0" encoding= "Utf-8"?
<manifest xmlns:android= "http://schemas.android.com/apk/ Res/android "
package=" com.example.shiyanshi.learningfragment ";
<application
Android:all Owbackup= "true"
android:icon= "@mipmap/ic_launcher"
android:label= "@string/app_name"
Andro Id:supportsrtl= "true"
Android:theme= "@style/apptheme";
<activity
Android:name= " . Mainactivity "
android:label=" @string/app_name "
android:theme=" @style/apptheme.noactionbar "&G T
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>< br> </activity>
</application>
</manifest>
2. layout file (1) activity_main.xml the layout container where a frame layout is placed, with an ID of container
<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent" android:layout_height= "Match_parent"
Android:id= "@+id/container" >
</FrameLayout>
(2) Fragment_main.xml main fargment layout file, the button placed inside to pop up another fragment
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
android:orientation= "Vertical"
tools:context= "Com.example.shiyanshi.learningfragment.MainActivityFragment"
tools:showin= "@layout/activity_main" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Beijing welcomes you!"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/btnshowfragmentanother"
Android:text= "show Another Fragment"/>
</LinearLayout>
(3) Fragment_another.xml from fragment layout file, the inside button is used to support back to the previous fragment interface
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "match_parent"
android:layout_height= "Match_parent"
Android:id= "@+id/fragmentanother" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "This is another fragment"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/btnbackoperation"
android:text= "Back operation"/>
</LinearLayout>
3.Java source Files (1) Mainactivity.java
Package com.example.shiyanshi.learningfragment;
Import Android.os.Bundle;
Import Android.support.design.widget.FloatingActionButton;
Import Android.support.design.widget.Snackbar;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.Toolbar;
Import Android.view.View;
Import Android.view.Menu;
Import Android.view.MenuItem;
public class Mainactivity extends Appcompatactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
if (savedinstancestate==null) {
Getsupportfragmentmanager ()
. BeginTransaction ()
The first parameter of the. Add (r.id.container,new mainactivityfragment ())//add is the layout container, and the second parameter is the class of the primary fragment
. commit ();
}
}
}
(2) Mainactivityfragment.java
Package com.example.shiyanshi.learningfragment;
Import android.support.v4.app.Fragment;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
Fragment
{ //Note that its inheritance is
Import Android.support.v4.app.Fragment
Fragment in the
Public Mainactivityfragment () {
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View rootview=inflater.inflate (R.layout.fragment_main, container, false);//First parameter: Layout file for main fragment, second: Layout container, The third is a Boolean type Attachtoroot
Rootview.findviewbyid (R.id.btnshowfragmentanother). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Getfragmentmanager (). BeginTransaction ()
. Addtobackstack (NULL)//Add back stack to enable it to support backward function
. replace (r.id.container,new anotherfragment ())
. commit ();
}
});
return rootview;
}
}
(3) Anotherfragment.java
Package com.example.shiyanshi.learningfragment;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
/**
* Created by Shiyanshi on 2016/1/25.
*/
Fragment
{
@Nullable
@Override
Public View Oncreateview (final layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.fragment_another,container,false);
View.findviewbyid (r.id.btnbackoperation). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Getfragmentmanager (). Popbackstack ();
}
});
return view;
}
}
4. Interface Display effect
Second, the life cycle of fragment
02Android User Interface Optimization (i) Android Fragment