Android-day program: Embedded Fragment and androidfragment

Source: Internet
Author: User

Android-day program: Embedded Fragment and androidfragment

Fragment is equivalent to a small activity, because Fragment can implement all the functions of the activity. The difference is that Fragment can be embedded into the activity, and one activity can have multiple Fragment, and you can switch Fragment as needed during running, to meet the needs of different screen sizes.


This chapter describes how to embed the Fragment method in an activity. There are static and dynamic methods.

The static method is to directly embed data using xml, and the dynamic method is to embed data in Java code.


Method:

1. Create a project according to the default settings

2. Create a new class named Fragment1.java.

The Code is as follows:

package bill.su.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment1, container, false);}}
3. Create an xml file named layout. Enter the following code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#00FF00" >        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/fragment_one"        android:textSize="25sp"        android:textColor="#FF0000" /></LinearLayout>

4. Similarly, create the second class:

package bill.su.fragment;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 {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment2, container, false);}}

5. Create the xml file corresponding to layout of the second class:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#FFFE00" >        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/fragment_two"        android:textColor="#000000"        android:textSize="25sp" />    </LinearLayout>

6. You only need to embed Fragment directly in the xml file of the main activity:

<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"    tools:context="bill.su.fragment.MainActivity"    android:orientation="horizontal" >    <fragment        android:name="bill.su.fragment.Fragment1"        android:id="@+id/fragment1"        android:layout_height="match_parent"        android:layout_weight="1"        android:layout_width="0dp" />       <fragment        android:name="bill.su.fragment.Fragment2"        android:id="@+id/fragment2"        android:layout_weight="1"        android:layout_width="0sp"        android:layout_height="match_parent" /></LinearLayout>

The running results are as follows:



In this way, two different Fragment values are embedded in the same activity.

In this way, you do not need to write Java code.


If it is implemented in the Java code of the activity, you only need to modify the onCreate function:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);FragmentManager fragManager = getFragmentManager();FragmentTransaction fragTrans = fragManager.beginTransaction();// get the current display infoWindowManager wm = getWindowManager();Display d = wm.getDefaultDisplay();if (d.getWidth() < d.getHeight()) {Fragment1 frag1 = new Fragment1();fragTrans.replace(android.R.id.content, frag1);}else {Fragment2 frag2 = new Fragment2();fragTrans.replace(android.R.id.content, frag2);}fragTrans.commit();}

Knowledge point:

Use the FragmentManager and FragmentTransaction classes to dynamically load Fragment.

The WindowManager is used to obtain the length and width of the current screen. It is determined that the screen is a horizontal or Korean vertical screen. Based on this, it loads different Fragment and gets different effects.

The last FragTrans. commit () statement is required to make the Fragment load successful.


After reaching a certain level, all these items have become very easy and simple. It is not difficult to learn new knowledge points. The difficulty lies in memory, and the more difficult it is to use them flexibly, what is more difficult is how to use this knowledge to create value, and what is the most difficult is to create value that others cannot create.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.