天天看点

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);
            }
        }
    }
}
           

继续阅读