[Android] dynamic creation of fragment, androidfragment
In a commercial software, there will be many interfaces. If no interface corresponds to an activity, there will be a lot of activity and the list files will be very messy, google introduced a new concept named fragment after android3.0.
Fragment does not need to be configured in the list file. The lightweight activity is configured in the activity layout file to which it belongs.
Use the fragment control in the layout File
Add a <fragment> node and set the name to the full path of the class pointed to by fragment.
Other properties are the same as other controls.
When you create a new class Fragment1 that inherits the Fragment class of the system, an error is reported because the minimum compatible android system version is 8 and fragment is introduced only in version 11, you can change the compatible version to 11 first.
Override the onCreateView () method. When the fragment is created, call back the method, return the current View object, and pass in a LayoutInflate object,
Call the inflate () method of the LayoutInflate object to obtain the View object, parameters: resource, and ViewGroup object.
Fragment was initially designed to adapt to the large screen of the tablet. For example, if a listview on the left and a fragment on the right click different entries of the ListView, the fragment content on the right changes accordingly, and the user experience is better.
Dynamic Creation
Implementation: when the user holds the mobile phone vertically, a page is displayed, and when the mobile phone is taken horizontally, another interface is displayed.
Determine the orientation of the current mobile phone by comparing the screen width and height.
Call getWindowManager (). getDefaultDisplay (). getWidth () to obtain the width.
Call getWindowManager (). getDefaultDisplay (). getHeight () to obtain the height
If the height is greater than the width, It is a portrait screen.
If the width is greater than the height, it is a horizontal screen.
Create Fragment1 and Fragment2 classes and inherit the Fragment class of the system.
Get the Fragment object and use new
Get the FragmentManager object by using the getFragmentManager () method
Call the beginTransaction () method of the FragmentManager object to start the transaction and obtain the FragmentTransaction object.
Call the replace () method of the FragmentTransaction object and replace the current interface with Fragment. parameters: containerViewId and Fragment object
ContainerViewId is the container id. You can use android. R. id. content to represent the id of the current interface.
Call the commit () method of the FragmentTransaction object to submit the transaction.
Simulator ctrl + f11 switch landscape
MainActivity. java
Package com. tsh. myfragment; import android. app. activity; import android. app. fragmentManager; import android. app. fragmentTransaction; import android. OS. bundle; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Fragment1 f1 = new Fragment1 (); Fragment2 f2 = new Fragment2 (); // judge whether the screen is landscape or landscape int width = getWindowManager (). getdefadisplay display (). getWidth (); int height = getWindowManager (). getdefadisplay display (). getHeight (); FragmentManager fm = getFragmentManager (); FragmentTransaction fr = fm. beginTransaction (); if (width
Fragment1.java
package com.tsh.myfragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); }}