Dynamic creationfragmentThe process
1.0 Create a new oneClass inheritancefragment.
2.0 Method of copying oncreateVIew in a custom fragment
3.0 Use inflate filler in the onCreateVIew method
4.0 Return the View object to get inflate through the Return method
5.0 Call the getFragmentManager method in the activity using fragment to obtain the fragmentManager object
6.0 Manage objects through fragment and enable transactions
7.0 Using transaction objects, calling replace method, replacing fragment, is the essence of dynamic use of fragment
8.0 Use transaction objects to commit.
The process of creating fragments dynamically can be compatible with lower versions of Android systems
1.0 import packages are all under V4 packages
2.0 Regarding the activity you want to use fragment, you must inherit fragmentActivity
3.0 When managing objects in fragments, the method you use is the getSupportFragmentManager static method.
fragment is part of the activity, it relies on the activity
The fragment depends on the activity and cannot exist alone. The life cycle of the fragment is affected by the life cycle of the activity.
The first step is to inherit the Fragment from new class.
The second step is to rewrite the onCreateView method
The third step is carried out in the onCreateView method, and use inflater to convert the layout layout file into a View object
Step 4: In the return method of onCreateView, return our View object.
Step 5: In the layout where you want to use activity, define our fragment into ViewGroup (that is, the layout)
Steps to dynamically use fragment:
The first step is to inherit the Fragment
The second step is to rewrite the onCreateView method
The third step is carried out in onCreateView, and use inflater to convert the layout layout file into a View object
Step 4. In the return method of onCreateVIew, return our View object out
Step 5. Get fragmentManager management through the static method getFragmentManager in the java code
Step 6: Get the transaction object through the beginningTransaction of fragmentManager
Step 7: Call the .replace method through the transaction object and replace the control with fragment
Step 8: Use transaction objects to submit commit
Use of fragments under v4 compatible packages (it is basically no longer used for development now)
1.0 Inherit the fragment under the v4 package in the custom fragment class. Remember that all import packages must be consistent when using fragments.
2.0 Your custom activity must inherit FragmentActivity
3.0 When obtaining the FragmentManager object, you must use the getSupportFragmentManager method instead of the getFragmentManager.
Here is a small demo I made
It realizes communication between various activities in a page. Click the button on the left and the corresponding Activity interface appears on the right. At the same time, on-off realizes communication between two activities.
The first step is to set the button button and text in the layout file main_Activity. Then add the layout file FrameLayout.
xmlns:tools="/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="horizontal"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a1"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a2"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a3"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a4"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a5"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a6"/>
android:
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a7"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data from Activity"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callMe"/>
android:
android:layout_width="wrap_content"
android:background="@drawable/bg"
android:layout_height="wrap_content"
android:text="@string/a8"/>
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content">
Write code in MainActivity.
First write a method initview() to implement initialization.
Then start writing Fragment based on swich. There are seven Fragments here. Each Fragment has similar code content, and one is used as a representative.
Here, the Activity code of Fragment1 is the most complex.
private voidinitview() {
Btton_a1= (Button) findViewById(.Btton_a1);
Btton_a2= (Button) findViewById(.Btton_a2);
Btton_a3= (Button) findViewById(.Btton_a3);
Btton_a4= (Button) findViewById(.Btton_a4);
Btton_a5= (Button) findViewById(.Btton_a5);
Btton_a6= (Button) findViewById(.Btton_a6);
Btton_a7= (Button) findViewById(.Btton_a7);
Activity_tv= (TextView) findViewById(.Activity_tv);
Activity_et= (EditText) findViewById(.Activity_et);
Activity_callChild= (Button) findViewById(.Activity_callChild);
temp= (FrameLayout) findViewById();
Btton_a1.setOnClickListener(this);
Btton_a2.setOnClickListener(this);
Btton_a3.setOnClickListener(this);
Btton_a4.setOnClickListener(this);
Btton_a5.setOnClickListener(this);
Btton_a6.setOnClickListener(this);
Btton_a7.setOnClickListener(this);
Activity_callChild.setOnClickListener(this);
}
@Override
public voidonClick(View v) {
fragmentManager=();
FragmentTransaction beginTransaction =();
switch(()) {
.Btton_a1:
extracted();
break;
.Btton_a2:
fragment2 fragement2 =newfragment2();
(, fragement2);
break;
.Btton_a3:
fragment3 fragment3 =newfragment3();
(, fragment3);
break;
.Btton_a4:
fragment4 fragment4 =newfragment4();
(, fragment4);
break;
.Btton_a5:
fragment5 fragment5 =newfragment5();
(, fragment5);
break;
.Btton_a6:
fragment6 fragment6 =newfragment6();
(, fragment6);
break;
.Btton_a7:
fragment7 fragment7 =newfragment7();
(, fragment7);
break;
.Activity_callChild:
break;
}
();
}
Fragment1's layout code and Activity code:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/a2"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3C6"
android:text="
Standing on the dangerous building, the wind is soft, looking at the sorrow of spring,
Darkness blooms in the sky.
In the lingering grass and smoke, who can rely on the railings in silence?
I intend to get drunk with Shu Kuang and sing with wine.
Strong joy is tasteless.
I will never regret my clothes getting looser and I will become haggard for her. "/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data from Activity"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callYou"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click event"/>
Communication has been added to the Activity here, so you need to read it carefully
;
;
;
;
;
;
;
;
;
;
;
;
;
/**
* Created by Administrator on 2016/10/1.
*/
public classfragment1extendsFragment {
privateActivityhomeActivity;
privateEditTextactivity_et;
privateViewview;
privateTextViewfragment_tv;
privateEditTextfragment_et;
privateTextViewactivity_tv;
@Nullable
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view= (.fragment1,null);
homeActivity= getActivity();
ActivityInit();
fragemtnInit();
returnview;
}
private voidfragemtnInit() {
fragment_tv= (TextView)(.fragment_tv);
fragment_et= (EditText)(.fragment_et);
activity_tv= (TextView)(.Activity_tv);
Button fragment_callChild = (Button)(.fragment_callChild);
fragment_callChild.setOnClickListener(() {
@Override
public voidonClick(View v) {
String trim =fragment_et.getText().toString().trim();
if((trim)){
(homeActivity,"cannot be empty",Toast.LENGTH_LONG).show();
return;
}
activity_tv.setText(trim);
}
});
}
private voidActivityInit() {
Button Activity_callChild =(Button)(.Activity_callChild);
activity_et= (EditText)(.Activity_et);
Activity_callChild.setOnClickListener(() {
public voidonClick(View v) {
String trim =activity_et.getText().toString().trim();
if((trim)){
(homeActivity,"cannot be empty",Toast.LENGTH_LONG).show();
return;
}
fragment_tv.setText(trim);
}
});
}
}
Other Fragment layout files and Activity are similar:
;
;
;
;
;
;
;
;
/**
* Created by Administrator on 2016/10/1.
*/
public classfragment2extendsFragment {
privateTextViewtv_temp;
@Nullable
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = (.fragment2,null);
//To search for fragment layout controls, you need to use the VIew object obtained by inflater.
returnview;
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/a2"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3C6"
android:text="
Standing on the dangerous building, the wind is soft, looking at the sorrow of spring,
Darkness blooms in the sky.
In the lingering grass and smoke, who can rely on the railings in silence?
I intend to get drunk with Shu Kuang and sing with wine.
Strong joy is tasteless.
I will never regret my clothes getting looser and I will become haggard for her. "/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data from Activity"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callYou"/>
android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click event"/>
This is the address of my code, for reference only./ZoeSj/FourFragment/tree/master/hulufragment