Android Fragment學習筆記(一)

來源:互聯網
上載者:User

本人初學Fragment,寫有幾個簡單例子,現提出來方便大家學習和研究。

Android Fragment學習筆記(一)

本程式的重點是在一個Activity中放入了兩個Fragment。


   頁面配置如下面代碼(fragment_hide_show.xml):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:gravity="center_horizontal"    android:layout_width="match_parent" android:layout_height="match_parent">    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"        android:gravity="center_vertical|center_horizontal"        android:textAppearance="?android:attr/textAppearanceMedium"        android:text="Demonstration of hiding and showing fragments." />    <LinearLayout android:orientation="horizontal" android:padding="4dip"        android:gravity="center_vertical" android:layout_weight="1"        android:layout_width="match_parent" android:layout_height="wrap_content">        <Button android:id="@+id/frag1hide"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="Hide" />        <fragment android:name="polycom.com.cn.FirstFragment"                android:id="@+id/fragment1" android:layout_weight="1"                android:layout_width="0px" android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout android:orientation="horizontal" android:padding="4dip"        android:gravity="center_vertical" android:layout_weight="1"        android:layout_width="match_parent" android:layout_height="wrap_content">        <Button android:id="@+id/frag2hide"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="Hide" />        <fragment android:name="polycom.com.cn.SecondFragment"                android:id="@+id/fragment2" android:layout_weight="1"                android:layout_width="0px" android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

在每一個LinearLayout裡均有一個Fragment,以下是java代碼實現。主要分為三個.java檔案,其中FragmentActivity.java主要是設定了兩個按鈕監聽器,用來控制顯示和隱藏Fragment中的內容,FirstFragment.java、SecondFragment.java主要是為Fragment載入顯示頁面。

FragmentActivity.java

import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FragmentActivity extends Activity {@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_hide_show);        FragmentManager fm = getFragmentManager();                //下面的兩行代碼是在Activity中按鈕添加監聽事件,控制Fragment的顯示和隱藏        addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));        addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));    }    void addShowHideListener(int buttonId, final Fragment fragment) {    //擷取activity中的button        final Button button = (Button)findViewById(buttonId);        button.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                FragmentTransaction ft = getFragmentManager().beginTransaction();                /*為Fragment設定淡入淡出效果,Android開發網提示這裡這兩個動畫資源是android內部資源無需我們手動定義。*/                ft.setCustomAnimations(android.R.animator.fade_in,                        android.R.animator.fade_out);                                 if (fragment.isHidden()) {                    ft.show(fragment);                    button.setText("隱藏");                } else {                    ft.hide(fragment);                    button.setText("顯示");                }                ft.commit();            }        });    }}

FirstFragment.java

import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class FirstFragment extends Fragment {    TextView mTextView;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {    //從檔案 example_fragment.xml 載入了一個layout         View v = inflater.inflate(R.layout.labeled_text_edit, container, true);                View tv = v.findViewById(R.id.msg);        ((TextView)tv).setText("The fragment saves and restores this text.");        mTextView = (TextView)v.findViewById(R.id.saved);        if (savedInstanceState != null) {            mTextView.setText(savedInstanceState.getCharSequence("text"));        }        return v;    }    @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putCharSequence("text", mTextView.getText());    }}

SecondFragment.java

import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class SecondFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {    //從檔案 example_fragment.xml 載入了一個layout         View v = inflater.inflate(R.layout.labeled_text_edit, container, true);        View tv = v.findViewById(R.id.msg);        ((TextView)tv).setText("The TextView saves and restores this text.");        //另外一種TextView的儲存模式        ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);        return v;    }}

下面是Fragment中載入的布局檔案(labeled_text_edit.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:padding="4dip"    android:layout_width="match_parent" android:layout_height="wrap_content">    <TextView android:id="@+id/msg"        android:layout_width="match_parent" android:layout_height="wrap_content"        android:layout_weight="0"        android:paddingBottom="4dip" />    <EditText android:id="@+id/saved"        android:layout_width="match_parent" android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/initial_text"        android:freezesText="true">        <requestFocus />    </EditText></LinearLayout>

這隻是一個簡單的例子,僅供剛學習Fragment的人員進行參考,希望我在這裡能起到一個拋磚引玉的作用。如果需要完整的程式,可以到我空間資源裡下載,資源名字為myFragment。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.