Fragment Static Loading for Android

Source: Internet
Author: User

Fragment Static Loading for Android
1. Fragment Knowledge Overview

Android3.0 introduces Fragment, which is mainly used on large screen devices and supports more dynamic and flexible UI design. Fragment should be a modular and reusable component in your application, because Fragment defines its own layout, and define its own behavior by using its own declared periodic callback method. Fragment can be included in multiple activities.

(1) Fragment can appear as part of the Activity interface;

(2) Multiple Fragment can appear in an Activity at the same time, and one Fragment can also be used in multiple activities;

(3) You can add, remove, or replace Fragment when the Activity is running;

(4) Fragment can respond to its own input events and has its own declaration cycle. Their life cycle is affected by the life cycle of the host Activity;

(5) When Fragment draws its user interface for the first time, the system calls the onCreateView () method, which returns a View. (If the UI is not displayed, null is returned );

Fragment supports static loading and dynamic loading.

 

2. preparation phase:

This article will introduce the Fragment knowledge using an APP in the future. The general layout is as follows:


Add color. xml to values:

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><resources>    <color name="gray">#88000000</color>    <color name="white">#ffffff</color></resources>
Add radio_pressed.xml to drawable.
    <?xml version="1.0" encoding="utf-8"?>      <selector xmlns:android="http://schemas.android.com/apk/res/android">                <item android:drawable="@color/gray" android:state_checked="true"></item>          <item android:drawable="@color/white" android:state_pressed="true"></item>          <item android:drawable="@color/white"></item>            </selector>  
Main layout:
<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <relativelayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <linearlayout android: id = "@ + id/frame" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> </linearlayout> <radiogroup android: id = "@ + id/radiogroup" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: Rule = "true" android: gravity = "center_horizontal" android: orientation = "horizontal"> <radiobutton android: id = "@ + id/first" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ drawable/radio_pressed" android: button = "@ null" android: drawabletop = "@ drawable/ic_launcher" android: gravity = "center_horizontal" android: text = "static loading"> <radiobutton android: id = "@ + id/second" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ drawable/radio_pressed" android: button = "@ null" android: drawabletop = "@ drawable/ic_launcher" android: gravity = "center_horizontal" android: text = "dynamic loading"> <radiobutton android: id = "@ + id/thrid" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ drawable/radio_pressed" android: button = "@ null" android: drawabletop = "@ drawable/ic_launcher" android: gravity = "center_horizontal" android: text = "lifecycle"> <radiobutton android: id = "@ + id/fourth" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ drawable/radio_pressed" android: button = "@ null" android: drawabletop = "@ drawable/ic_launcher" android: gravity = "center_horizontal" android: text = "value-based communication"> </radiobutton> </radiogroup> </relativelayout>
MainActivity load main:
Package com. example. fragment; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. radioGroup; import android. widget. radioGroup. onCheckedChangeListener; public class MainActivity extends Activity implements OnCheckedChangeListener {private RadioGroup group; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); group = (RadioGroup) findViewById (R. id. radiogroup); group. setOnCheckedChangeListener (this) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (checkedId) {case R. id. first: // demonstrate static loading of break; case R. id. second: // demonstrate dynamic loading of break; case R. id. thrid: // demonstrate the lifecycle break; case R. id. fourth: // demonstrate break for passing values ;}}}

 

3. Static Loading

Declare Fragment in the layout file of the Activity (Note: In The android: name attribute in the tag specifies the Fragment class instantiated in layout.) the method used to identify the Fragment is as follows:. android: The id attribute provides a unique ID; B. android: The tag attribute provides a unique string;

Add fragment. xml:

<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <textview android: id = "@ + id/text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "My Fragment"> <button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/button"> </button> </textview> </linearlayout>
Add the MyFragment class and load the fragment layout:
Package com. example. fragment; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. textView; public class MyFragment extends Fragment {@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// convert layout file to View object/*** inflater. inflate (resource, root, attachToRoot) * resource: layout file to be loaded by Fragment * root: parent ViewGroup of layout * attactToRoot: false, no parent ViewGroup */View view = inflater is returned. inflate (R. layout. fragment, container, false); TextView text = (TextView) view. findViewById (R. id. text); Button button Button = (Button) view. findViewById (R. id. button); text. setText ("static loading Fragment"); button. setText ("Get content"); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // String value = getAaa (); // Toast. makeText (getActivity (), "value =" + value, // Toast. LENGTH_SHORT ). show () ;}}); return view ;}}
Add jigntai. xml:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">        <!--{cke_protected}{C}%3C!%2D%2D%20android%3Aid%EF%BC%9A%E9%9D%99%E6%80%81%E5%8A%A0%E8%BD%BD%E5%BF%85%E9%A1%BB%E6%8C%87%E5%AE%9A%E4%B8%80%E4%B8%AAID%20%2D%2D%3E-->    <!--{cke_protected}{C}%3C!%2D%2D%20android%3Aname%EF%BC%9A%E5%AE%8C%E6%95%B4%E5%8C%85%E5%90%8D%20%2D%2D%3E-->    <fragment android:id="@+id/fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.fragment.MyFragment"></fragment></linearlayout>

Add the JingTaiActivity class:

Public class JingTaiActivity extends Activity {private TextView TV; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. jingtai); Button button = (Button) findViewById (R. id. button); TV = (TextView) findViewById (R. id. text); button. setText ("changed"); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubtv. setText ("TextView changed ");}});}}

In the main MainActivity, add the static loading part:

 

Case R. id. first: // demonstrate static loading of Intent jingtaiIntent = new Intent (MainActivity. this, JingTaiActivity. class); startActivity (jingtaiIntent); break;

The MainActivity jumps to JingTaiActivity, which loads The android: name attribute is com. example. fragment. MyFragment, And the MyFragment has its own text and button layout. Return to JingTaiActivity and load jingtai. xml in it. You can use findViewById to find the text and button in the layout File fragment of MyFragment.

That is to say, when a layout file is loaded to the Activity by statically loading Fragment, the layout file in Fragment is also shared with the Activity.

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.