A Brief Introduction to Fragment in android and a detailed column for creating tabs, androidfragment

Source: Internet
Author: User

A Brief Introduction to Fragment in android and a detailed column for creating tabs, androidfragment

 

Fragment key points Design Philosophy

Android 3.0 introduced the fragments concept to support more dynamic and flexible UI design on large screen devices, such as tablets. The screen size of a tablet is much larger than that of a mobile phone. More space is available for more UI components, and more interaction is generated between these components. Fragment allows such a design without requiring you to manage the complex changes of viewhierarchy yourself. By spreading the layout of the activity to fragment, you can modify the appearance of the activity at runtime and save those changes in the back stack managed by the activity. (http://developer.android.com/guide/topics/fundamentals/fragments.html)

 

For example, a news application can use a fragment on the left side of the screen to display a list of articles, then, use another fragment on the right side of the screen to display an article-two fragment are displayed side by side in the same activity, and each fragment has its own set of lifecycle callback methods, and process their own user input events. Therefore, instead of using one activity to select an article and another activity to read the article, you can select an article in the same activity and read it ,:

Fragment should be a modular and reusable component in your application. that is, because fragment defines its own layout and uses its own lifecycle callback method to define its own behavior, you can include fragment into multiple activities. this is especially important because it allows you to adapt your user experience to different screen sizes. for example, you may only include multiple fragment in an activity when the screen size is large enough. If this is not the case, another independent one will be started, use different fragment activities.

Continue with the previous news example-when running on A very large screen (such as A tablet), the app can embed two fragment entries in Activity. However, on A normal screen (such as A mobile phone), there is not enough space for two fragment at the same time. Therefore, Activity A only contains the fragment of the article list, when you select an article, it starts ActivityB, which contains fragment for reading the article. therefore, the application can support two design modes at the same time.

To create a Fragment, you must create a fragment subclass (or inherit from an existing subclass). The Code of the Fragment class looks like Activity. It contains callback methods similar to activity, such as onCreate (), onStart (), onPause (), and onStop (). In fact, if you want to convert a ready-made Android app to fragment, you may simply move the code from the callback method of your activity to the callback method of your fragment.

Generally, the following lifecycle methods should be implemented at least:

  • OnCreate ()
    This method is called when fragment is created.
    In the implementation code, you should initialize the necessary components that you want to maintain in fragment. After the fragment is paused or stopped, it can be restored.
  • OnCreateView ()
    When fragment draws its user interface for the first time, the system will call this method. to draw the fragment UI, this method must return a View, which is the root view of your fragment layout. if fragment does not provide the UI, null can be returned.
  • OnPause ()
    When the user is about to leave fragment, the system calls this method as the first indication (however, it does not always mean that fragment will be destroyed .) before the end of the current user session, you should submit any persistent changes here (because the user may not return ).
Its lifecycle diagram is as follows:

Most applications should implement at least these three methods for each fragment, but there are other callback methods you should also use to process various stages of the fragment lifecycle. all Lifecycle callback methods will be discussed in Handlingthe Fragment Lifecycle.

In addition to inheriting the base class Fragment, you may inherit some subclasses:

 

  • DialogFragment
    A floating dialog box is displayed.
    Using this class to create a dialog box is a good choice outside the toolmethods of the Activity class dialog box,
    Because you can merge a fragment dialog box into the fragment back stack of activity management, allowing users to return to a previously abandoned fragment.
  • ListFragment
    Displays a list of projects managed by an adapter (such as SimpleCursorAdapter), similar to ListActivity.
    It provides some methods to manage a list view, such as onListItemClick () callback to handle click events.
  • PreferenceFragment
    Displays a list of Preference object hierarchies, similar to PreferenceActivity.
    This is useful when creating a "set" activity for your application.
    All right, it's all official columns. I won't talk much about them. Go to my fragment to create a detailed column on the tab:
    MainActivity
import com.menglifang.frag.FragmentOne;import com.menglifang.frag.FragmentTwo;import com.menglifang.frag.Fragmentfour;import com.menglifang.frag.Fragmentfree;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.View.OnClickListener;import android.widget.FrameLayout;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends FragmentActivity implements OnClickListener{    FrameLayout frameLayout;     TextView text1,text2,text3,text4;    FragmentManager fm;     private FragmentTransaction ft;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        frameLayout=(FrameLayout ) findViewById(R.id.frag_view);        text1=(TextView) findViewById(R.id.tab_one);        text2=(TextView) findViewById(R.id.tab_two);        text3=(TextView) findViewById(R.id.tab_free);        text4=(TextView) findViewById(R.id.tab_four);                text1.setOnClickListener(this);        text2.setOnClickListener(this);        text3.setOnClickListener(this);        text4.setOnClickListener(this);        fm=this.getSupportFragmentManager();        ft=fm.beginTransaction();        ft.replace(R.id.frag_view, new FragmentOne());        ft.commit();            }    @Override    public void onClick(View arg0) {        ft=fm.beginTransaction();        switch(arg0.getId()){        case R.id.tab_one:            ft.replace(R.id.frag_view, new FragmentOne());            break;        case R.id.tab_two:            ft.replace(R.id.frag_view, new FragmentTwo());            break;        case R.id.tab_free:            ft.replace(R.id.frag_view, new Fragmentfree());            break;        case R.id.tab_four:            ft.replace(R.id.frag_view, new Fragmentfour());            break;                    }        ft.commit();    }    }

Four Fragement items are created. To simplify the process, the layout files of each Fragement in my four fragment items are different.

import com.menglifang.fragment.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentOne extends Fragment{    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO Auto-generated method stub        View view=inflater.inflate(R.layout.fragone, null);        return view;//super.onCreateView(inflater, container, savedInstanceState);    }}

Layout file: only the layout in MainActivity and the layout in fragment are provided here.

Layout in MainActivity: activity_main.xml

<RelativeLayout 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 = ". mainActivity "> <LinearLayout android: id =" @ + id/tab "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: orientation = "horizontal"> <TextView android: id = "@ + id/tab_one" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Latest News"/> <TextView android: id = "@ + id/tab_two" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "popular news"/> <TextView android: id = "@ + id/tab_free" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "social news"/> <TextView android: id = "@ + id/tab_four" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Liverpool News"/> </LinearLayout> <FrameLayout android: orientation = "vertical" android: id = "@ + id/frag_view" android: layout_below = "@ + id/tab" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "# ffff00"> </FrameLayout> </RelativeLayout>

Fragment layout: fragone. xml

<? 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 = "200dp" android: orientation = "vertical"> <TextView android: background = "# ff0000" android: gravity = "center" android: layout_width = "match_parent" android: layout_height = "match_parent" android: text = "This is the first frage"/> </LinearLayout>

 

 

In fact, this column is easy to understand. I checked it on the Internet and many people copied it from the official website. I think it is easy to understand it.

Many shortcomings. You are welcome to learn and exchange.


How to Use JS or PHP based on the value in the page tab and pass it to page B when you press search (page B also has a tab) requires code and Examples

For such a thing, you only need to understand the get and post parameters, as long as you get the corresponding content, and display different content according to the obtained parameters.
Write the code by yourself. If you do not understand GET and POST, you do not understand even code.

How to Use JS or PHP based on the value in the page tab and pass it to page B when you press search (page B also has a tab) requires code and Examples

Page a: <form... method = "get" action = "B page">
<Select id = "..."> <option value = "...">... </option>
<Select id = "..."> <option value = "...">... </option>
<Input id = "..." value = "...">
...
</Form>

Page B
$ Param1 = $ _ REQUEST ["..."];
$ Param2 = $ _ REQUEST ["..."];
Echo $ param1. $ param2;

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.