给Fragment传递参数 —— FragmentArgumentsSupport

来源:互联网 发布:ajax传递对象数组参数 编辑:程序博客网 时间:2024/04/30 17:12
/** * 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"/>



图片:

0 0
原创粉丝点击