Android Api Demos登顶之路(三十五)Fragment-->Argument
2015-08-18 20:19
316 查看
这个demo演示了向fragment对象中传递数据的两种方式,一种是通过属性设置
* 一种是在创建fragment对象时通过bundle进行传递。
activity.xml
MyFragment的布局文件:
在values的 attrs.xml文件中定义用来传递数据的属性。这里有个问题,发现Fragment不支持自定义属性。按说在创建Fragment对象时,在oninflate方法中能读出系统定义的属性,也应该能够读出自定义的属性才对啊。但研究了两天,网上也没找到相关资料,最后还是没找到为Fragment自定义属性的方法。有知道的请不吝赐教。至今没有找到原因。
MainActivity
* 一种是在创建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); } } } }
相关文章推荐
- Android基础0818生命周期+LinearLayout+RelativeLayout
- android---复杂布局的学习
- 关于Android—Activity 生命周期图
- Android与JS互相调用以及注意
- Android学习笔记(七)
- Android入门
- 自定义控件
- Android调试小技巧(LogCat不输出、Log自动清空、install时timeout)
- Android插件技术——(三)加载未安装apk
- Android目录结构
- 学习小结
- Android Init进程
- Android 中的DisplayMetrics类的用法
- 【Android】android存储篇之 文件存储
- android客户端从服务器端获取json数据并解析的实现代码(重要)
- Android Studio文件分组插件
- Android Studio系列--基本设置与运行
- android动态加载控件(LinearLayout)并获取动态添加的EditText里的值
- Android开源项目汇总
- Android小方法——在Android Studio中进行单元测试