Fragment使用

http://www.cnblogs.com/mengdd/archive/2013/01/08/2851368.html

 

Fragment

  Android是在Android 3.0 (API level 11)開始引入Fragment的。html

  能夠把Fragment想成Activity中的模塊,這個模塊有本身的佈局,有本身的生命週期,單獨處理本身的輸入,在Activity運行的時候能夠加載或者移除Fragment模塊。android

  能夠把Fragment設計成能夠在多個Activity中複用的模塊。編程

  當開發的應用程序同時適用於平板電腦和手機時,能夠利用Fragment實現靈活的佈局,改善用戶體驗。app

  如圖:ide

 

 

 

Fragment的生命週期

  由於Fragment必須嵌入在Acitivity中使用,因此Fragment的生命週期和它所在的Activity是密切相關的。函數

  若是Activity是暫停狀態,其中全部的Fragment都是暫停狀態;若是Activity是stopped狀態,這個Activity中全部的Fragment都不能被啓動;若是Activity被銷燬,那麼它其中的全部Fragment都會被銷燬。佈局

  可是,當Activity在活動狀態,能夠獨立控制Fragment的狀態,好比加上或者移除Fragment。ui

  當這樣進行fragment transaction(轉換)的時候,能夠把fragment放入Activity的back stack中,這樣用戶就能夠進行返回操做。this

 

 

 

Fragment的使用相關

  使用Fragment時,須要繼承Fragment或者Fragment的子類(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),因此Fragment的代碼看起來和Activity的相似。spa

 

使用Support Library

  Support Library是一個提供了API庫函數的JAR文件,這樣就能夠在舊版本的Android上使用一些新版本的APIs。

  好比android-support-v4.jar.它的完整路徑是:

  <sdk>/extras/android/support/v4/android-support-v4.jar.

  它就提供了Fragment的APIs,使得在Android 1.6 (API level 4)以上的系統均可以使用Fragment。

  爲了肯定沒有在舊版本系統上使用新版本的APIs,須要以下導入語句:

 

  import android.support.v4.app.Fragment;   import android.support.v4.app.FragmentManager;

 

  同時應該將上述的包拷入libs項目下的libs文件夾,而後在項目的Properties中添加:右鍵單擊項目,選Properties,左邊選Java Build Path,而後Add External JARs…,添加android-support-v4.jar.

 

 

 

 

  當建立包含Fragment的Activity時,若是用的是Support Library,那麼繼承的就應該是FragmentActivity而不是Activity。

 

必須實現的三個回調函數

  onCreate()

  系統在建立Fragment的時候調用這個方法,這裏應該初始化相關的組件,一些即使是被暫停或者被中止時依然須要保留的東西。

  onCreateView()

  當第一次繪製Fragment的UI時系統調用這個方法,必須返回一個View,若是Fragment不提供UI也能夠返回null。

  注意,若是繼承自ListFragment,onCreateView()默認的實現會返回一個ListView,因此不用本身實現。

  onPause()

  當用戶離開Fragment時第一個調用這個方法,須要提交一些變化,由於用戶極可能再也不返回來。

 

實現Fragment的UI

  提供Fragment的UI,必須實現onCreateView()方法。

  假設Fragment的佈局設置寫在example_fragment.xml資源文件中,那麼onCreateView()方法能夠以下寫:

  

複製代碼
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } }
複製代碼

 

  onCreateView()中container參數表明該Fragment在Activity中的父控件;savedInstanceState提供了上一個實例的數據。

  inflate()方法的三個參數:

  第一個是resource ID,指明瞭當前的Fragment對應的資源文件;

  第二個參數是父容器控件;

  第三個布爾值參數代表是否鏈接該佈局和其父容器控件,在這裏的狀況設置爲false,由於系統已經插入了這個佈局到父控件,設置爲true將會產生多餘的一個View Group。

 

把Fragment加入Activity

  當Fragment被加入Activity中時,它會處在對應的View Group中。

  Fragment有兩種加載方式:一種是在Activity的layout中使用標籤<fragment>聲明;另外一種方法是在代碼中把它加入到一個指定的ViewGroup中。

  另外,Fragment它能夠並非Activity佈局中的任何一部分,它能夠是一個不可見的部分。這部份內容先略過。

 

加載方式1:經過Activity的佈局文件將Fragment加入Activity

  在Activity的佈局文件中,將Fragment做爲一個子標籤加入便可。

  如:

複製代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
複製代碼

 

  其中android:name屬性填上你本身建立的fragment的完整類名。

  當系統建立這個Activity的佈局文件時,系統會實例化每個fragment,而且調用它們的onCreateView()方法,來得到相應fragment的佈局,並將返回值插入fragment標籤所在的地方。

  有三種方法爲Fragment提供ID:

  android:id屬性:惟一的id

  android:tag屬性:惟一的字符串

  若是上面兩個都沒提供,系統使用容器view的ID。

 

加載方式2:經過編程的方式將Fragment加入到一個ViewGroup中

  當Activity處於Running狀態下的時候,能夠在Activity的佈局中動態地加入Fragment,只須要指定加入這個Fragment的父View Group便可。

  首先,須要一個FragmentTransaction實例: 

FragmentManager fragmentManager = getFragmentManager()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

  (注,若是import android.support.v4.app.FragmentManager;那麼使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

  

  以後,用add()方法加上Fragment的對象:

ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();

 

  其中第一個參數是這個fragment的容器,即父控件組。

  最後須要調用commit()方法使得FragmentTransaction實例的改變生效。

實例

兩個Fragment類

 

package com.example.android.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            updateArticleView(mCurrentPosition);
        }
    }

    public void updateArticleView(int position) {
        TextView article = (TextView) getActivity().findViewById(R.id.article);
        article.setText(Ipsum.Articles[position]);
        mCurrentPosition = position;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

 

package com.example.android.fragments;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
    }

    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);
        
        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}

主Activity

package com.example.android.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity 
        implements HeadlinesFragment.OnHeadlineSelectedListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

首頁佈局文件,分是否large平板模式

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

large平板

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

 

運用的知識,fragment間通訊

一、使用接口

二、重載onAttach方法

public void onAttach(Activity activity)

獲取容器的Activity

相關文章
相關標籤/搜索