1. 程式人生 > >給Fragment傳遞引數 —— FragmentArgumentsSupport

給Fragment傳遞引數 —— FragmentArgumentsSupport

/**
 * Demonstrates a fragment that can be configured through both Bundle arguments
 * and layout attributes.
 */
//展示了一個可以通過屬性和budle來配置fragment顯示的例子
public class FragmentArgumentsSupport extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_arguments_support);

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


    public static class MyFragment extends Fragment {
        CharSequence mLabel;

        /**
         * Create a new instance of MyFragment that will be initialized
         * with the given arguments.
         * 建立一個可以被給定的arguments初始化的fragment
         */
        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.
         * Fragment從xml中解析出來之後會走到這個oninfalte方法,
         * 引數中的attr就會把屬性引數帶到程式碼中,
         * 可以通過解析獲取
         */
        @Override
        public void onInflate(Activity activity, AttributeSet attrs,
                              Bundle savedInstanceState) {
            super.onInflate(activity, attrs, savedInstanceState);

            TypedArray a = activity.obtainStyledAttributes(attrs,
                    R.styleable.FragmentArguments);
            mLabel = a.getText(R.styleable.FragmentArguments_android_label);
            a.recycle();
        }

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

            Bundle args = getArguments();
            if (args != null) {
                //從這解析出arguments中的label引數
                CharSequence label = args.getCharSequence("label");
                if (label != null) {
                    mLabel = label;
                }
            }
        }

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

}
xml程式碼
R.layout.fragment_arguments_support  :
</pre>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip">

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

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


        <fragment
            android:id="@+id/embedded"
            class="com.example.android.supportv4.app.FragmentArgumentsSupport$MyFragment"
            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>

R.layout.helloworld:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/hello_world"/>



圖片: