APIDemo分析:fragment的使用方法

public class FragmentArguments extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("_____tag", "onCreate in main avtivity___1");
		setContentView(R.layout.fragment_arguments);

		Log.e("_____tag", "onCreate in main avtivity____2");
		if (savedInstanceState != null) {
			// First-time init; create fragment to embed in activity.
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			Fragment newFragment = MyFragment.newInstance("From Arguments");
			ft.add(R.id.created, newFragment);
			ft.commit();
			Log.e("_____tag", "onCreate in main avtivity____3");
		}
	}

	public static class MyFragment extends Fragment {
		CharSequence mLabel;

		/**
		 * Create a new instance of MyFragment that will be initialized with the
		 * given arguments.
		 */
		static MyFragment newInstance(CharSequence label) {
			MyFragment f = new MyFragment();
			Bundle b = new Bundle();
			b.putCharSequence("label", label);
			f.setArguments(b);
			return f;
		}

		/**
		 * Parse attributes during inflation from a view hierarchy into the
		 * arguments we handle.
		 */
		@SuppressLint("NewApi")
		@Override
		public void onInflate(Activity activity, AttributeSet attrs,
				Bundle savedInstanceState) {
			super.onInflate(activity, attrs, savedInstanceState);

			Log.e("_____tag", "onInflate in MyFragment______1");
			TypedArray a = activity.obtainStyledAttributes(attrs,
					R.styleable.FragmentArguments);
			mLabel = a.getText(R.styleable.FragmentArguments_android_label);
			Log.e("_____tag", (String)mLabel + "in onInflate in MyFragment______2");
			a.recycle();
		}

		/**
		 * During creation, if arguments have been supplied to the fragment then
		 * parse those out.
		 */
		@SuppressLint("NewApi")
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			Log.e("_____tag", "onCreate in fragment");
			Bundle args = getArguments();
			Log.e("_____tag", (String)mLabel + "onCreate in MyFragment____1");
			if (args != null) {
				mLabel = args.getCharSequence("label", mLabel);
				Log.e("_____tag", (String)mLabel + "in onCreate in MyFragment____2");
			}
		}

		/**
		 * Create the view for this fragment, using the arguments given to it.
		 */
		@SuppressWarnings("deprecation")
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.e("_____tag", "onCreateView in MyFragment____1");
			View v = inflater.inflate(R.layout.hello_world, container, false);
			View tv = v.findViewById(R.id.text);
			Log.e("_____tag", (String)mLabel + "in onCreateView in MyFragment____2");
			((TextView) tv).setText(mLabel != null ? mLabel : "(no label)");
			tv.setBackgroundDrawable(getResources().getDrawable(
					android.R.drawable.gallery_thumb));
			return v;
		}
	}

}


好吧這是APIDemo的源碼,裏面加了點log用來分析的。

setContentView(R.layout.fragment_arguments);這個顯示用戶界面信息,來看看fragment_arguments.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:padding="4dip"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="top|center_horizontal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/fragment_arguments_msg" />

    <LinearLayout android:orientation="horizontal" android:padding="4dip"
        android:layout_width="match_parent" android:layout_height="wrap_content">


        <fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
                android:id="@+id/embedded"
                android:layout_width="0px" android:layout_height="wrap_content"
                android:layout_weight="1"
                android:label="@string/fragment_arguments_embedded" />


        <FrameLayout
                android:id="@+id/created"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

LinearLayout就不說了,主要是fragment 和FrameLayout這2個標籤. 第一個fragment指定了類MyFragment,就是在Activity裏面定義的內部類。指定了類那麼在setContentView的時候再內部調用了MyFragment類中複寫的一些方法就可以顯示一個fragment提供的UI 佈局(From Attributes這個框框)。

水平佈局layout_weight=1,在From Attributes普遍還有一個FrameLayout目前還是沒有UI界面顯示出來

if (savedInstanceState != null) {
			// First-time init; create fragment to embed in activity.
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			Fragment newFragment = MyFragment.newInstance("From Arguments");
			ft.add(R.id.created, newFragment);
			ft.commit();
			Log.e("_____tag", "onCreate in main avtivity____3");
		}

本來savedInstanceState == null是這樣的條件,在第一次初始化的時候savedInstanceState 這個值應該是空的!可以進入IF,一個MyFragment添加到了R.id.created,commit提交後,就可以顯示在屏幕上了。

每使用MyFragment創建一個對象用來顯示在屏幕上,MyFragment的生命週期就會執行,即MyFragment複寫的幾個函數會執行。


這裏發現了2類實用Fragment的不同方法。

1.直接在xml文件裏面添加自定義的fragment類的標籤實用,

2.在xml裏面佈局framelayout標籤,獲取ID後,將MyFragment使用FragmentTransaction對象的 add方法添加的指定的ID,commit提交後顯示界面


思考:自定義的MyFragment出現在xml裏面的時候只是可以使用例程中的方法指定類名嗎,直接使用類名作爲標籤可以嗎?

            在xml裏面佈局的標籤不使用framelayout,使用其他標籤例如Linraelayout,可以在裏面commit自定義的fragment 嗎?

    FragmentTransaction可以用來管理MyFragment,還有關於back stack的處理是和它相關的看看其他例子FragmentTransaction是怎麼管理Fragment的。。。










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章