天天看點

Android Api Demos登頂之路(三十五)Fragment-->Argument

這個demo示範了向fragment對象中傳遞資料的兩種方式,一種是通過屬性設定

* 一種是在建立fragment對象時通過bundle進行傳遞。

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/hello_world" />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <fragment 
            class="com.fishtosky.fragmentarguments.MainActivity$MyFragment"
            android:id="@+id/fragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="這是從屬性中擷取到的資料!"/>
        <FrameLayout 
            android:id="@+id/frameLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </FrameLayout>

    </LinearLayout>

</LinearLayout>
           

MyFragment的布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello"
    android:id="@+id/text" >
</TextView>
           

在values的 attrs.xml檔案中定義用來傳遞資料的屬性。這裡有個問題,發現Fragment不支援自定義屬性。按說在建立Fragment對象時,在oninflate方法中能讀出系統定義的屬性,也應該能夠讀出自定義的屬性才對啊。但研究了兩天,網上也沒找到相關資料,最後還是沒找到為Fragment自定義屬性的方法。有知道的請不吝賜教。至今沒有找到原因。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FragmentArguments">
        <!-- android:label已經在系統中定義,這裡隻是引用了一下
        目前fragment還不支援自定義屬性 -->
        <attr name="android:text"/>
    </declare-styleable>
</resources>
           

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 擷取事務向framelayout中添加一個fragment
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        MyFragment frg = MyFragment.getInstance("這是從bundle中傳遞過來的資料!");
        ft.add(R.id.frameLayout, frg);
        ft.commit();
    }

    /*
     * 定義一個Fragment内部類
     */
    public static class MyFragment extends Fragment {
        private String label;

        /*
         * 擷取Framgment的靜态方法,并通過Bundle向fragment對象傳遞資料
         */
        public static MyFragment getInstance(String text) {
            MyFragment frg = new MyFragment();
            Bundle bundle = new Bundle();
            bundle.putString("label", text);
            frg.setArguments(bundle);
            return frg;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.hello, container, false);
            TextView tv = (TextView) v.findViewById(R.id.text);
            tv.setText(label);
            tv.setBackground(getResources().getDrawable(
                    android.R.drawable.gallery_thumb));
            return v;
        }

        /*
         * 重寫onInflate方法,取出在屬性中設定的值
         */
        @Override
        public void onInflate(Activity activity, AttributeSet attrs,
                Bundle savedInstanceState) {
            super.onInflate(activity, attrs, savedInstanceState);
            TypedArray ta = activity.obtainStyledAttributes(attrs,
                    R.styleable.FragmentArguments);
            label = (String) ta
                    .getText(R.styleable.FragmentArguments_android_text);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 檢查bundle中是否有資料,如果有則取出資料
            // 預設值設為屬性中設定的資料
            Bundle args = getArguments();
            if (args != null) {
                label = args.getString("label", label);
            }
        }
    }
}
           

繼續閱讀