Android -- Fragment introduction and two basic usage methods

Source: Internet
Author: User

Android -- Fragment introduction and two basic usage methods

 

Today, when we call ViewPager, we feel that ViewPager + Fragment is more flexible. Therefore, we can sort out the Fragment and try again.

 

I. Why Fragment?

 

1. Flexible Layout

Fragment represents an action or part of the UI in an Activity. Multiple fragment can be combined and placed in a single activity to create a multi-Interface UI, and a fragment can be reused in multiple activities. Think of fragment as a modular area of an activity. It has its own life cycle, receives input events belonging to it, and can be added or deleted during activity running. After Fragment is added, the activity interface can be assembled at runtime or even re-assembled according to the needs of users or devices.

In addition, in terms of implementation, using fragment instead of activity for app UI management can bypass the restrictions of Android activity rules.

 

2. Considering the large screen device

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 activity layout to fragment, you can modify the activity appearance at runtime and save the changes in the back stack managed by the activity.

 

2. Two basic usage methods of Fragment

 

1. Introduce fragment in Layout mode

 

Next we will use fragment to write a simple layout, where two fragment pieces are distributed to an activity:

First, the first fragment:

 

<Framelayout android: background = "# AFEEEE" android: layout_height = "match_parent" android: layout_width = "match_parent" tools: context = "com. example. liuhuichao. projecttest. exampleFragment "xmlns: android =" http://schemas.android.com/apk/res/android "xmlns: tools =" http://schemas.android.com/tools ">
 </Framelayout>

 

In oncreate, load the layout file of the fragment and initialize the view:

 

 

public class ExampleFragment extends Fragment {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        return inflater.inflate(R.layout.fragment_example, container, false);    }}

 

 

The second fragment is similar to the following:

 

 

<Framelayout android: background = "# 00FA9A" android: layout_height = "match_parent" android: layout_width = "match_parent" tools: context = "com. example. liuhuichao. projecttest. exampleFragmentTwo "xmlns: android =" http://schemas.android.com/apk/res/android "xmlns: tools =" http://schemas.android.com/tools ">
 </Framelayout>

 

 

 

public class ExampleFragmentTwo extends Fragment {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_example_fragment_two, container, false);    }}

 

 

Activity layout:

 

 

 
     
      
   
  
 

 

 

The activity is associated with two fragment:

 

 

Public class MainFragmentActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. mainfragment); // obtain the FragmentManager, and then obtain the FragmentTransaction FragmentManager fm = getFragmentManager (); FragmentTransaction transaction = fm. beginTransaction (); // use Fragment to dynamically replace transaction in the layout file. replace (R. id. fragmentOne, new ExampleFragment (); transaction. replace (R. id. fragmentTwo, new ExampleFragmentTwo (); // submit the transaction. commit ();}}

 

Run:

 

 

 

2. Dynamic Layout of fragment

 

First of all, this is a promotion method, and it seems flexible.

 

Fragment still uses the following two items. Modify the activity layout:

 

 

 
 
  
  <Framelayout android: id = "@ + id/fragment_container" android: layout_height = "match_parent/" android: layout_width = "match_parent"> </framelayout>
 

 

Activity Code:

 

Public class MainFragmentActivity extends AppCompatActivity {private Button btn_show_fragment1; private Button btn_show_fragment2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. fragmentmaintwo); btn_show_fragment1 = (Button) findViewById (R. id. btn_show_fragment1); btn_show_fragment2 = (Button) findViewById (R. id. btn_show_fragment2); btn_show_fragment1.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {FragmentManager manager = getFragmentManager (); FragmentTransaction ft = manager. beginTransaction (); ExampleFragment ef1 = new ExampleFragment ();/** add is to add a fragment instance to the top layer of the Activity * replace to replace the fragment instance in containerViewId. * Note, it first deletes all fragment in containerViewId, and then adds it to the current fragment **/ft. add (R. id. fragment_container, ef1); ft. commit () ;}}); btn_show_fragment2.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {FragmentManager manager = getFragmentManager (); FragmentTransaction ft = manager. beginTransaction (); ExampleFragmentTwo ef2 = new ExampleFragmentTwo (); ft. add (R. id. fragment_container, ef2); ft. commit () ;}});/* setContentView (R. layout. mainfragment); // obtain the FragmentManager, and then obtain the FragmentTransaction FragmentManager fm = getFragmentManager (); FragmentTransaction transaction = fm. beginTransaction (); // use Fragment to dynamically replace transaction in the layout file. replace (R. id. fragmentOne, new ExampleFragment (); transaction. replace (R. id. fragmentTwo, new ExampleFragmentTwo (); // submit the transaction. commit ();*/}}

 

It seems like a thing when FragmentManager and FragmentTransaction are used to manage fragment. Baidu's principle is really interesting.

 

 

 

 

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.