android中的fragment
2016-07-13 14:44
411 查看
一、静态
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
<fragment
android:id="@+id/fragment_static"
class="fragment.FragmentStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_static,container, false);
return v;
}
1、activity传数据到fragment:
获取对应得fragment:
FragmentManager fm=getFragmentManager();
FragmentStatic f=(FragmentStatic) fm.findFragmentById(R.id.fragment_static);
发送数据:fragment.setArguments(Bundle bundle);
在fragment里接收数据:this.getArguments();//Bundle
二、动态
Bundle bundle1=new Bundle();
bundle1.putString("toDynamicFragment1", "toDynamicFragment1");
FragmentDynamic1 f1=new FragmentDynamic1();
f1.setArguments(bundle1);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.ll_main_fragment_dynamic, f1);
ft.commit();
三、生命周期
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
<fragment
android:id="@+id/fragment_static"
class="fragment.FragmentStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_static,container, false);
return v;
}
1、activity传数据到fragment:
获取对应得fragment:
FragmentManager fm=getFragmentManager();
FragmentStatic f=(FragmentStatic) fm.findFragmentById(R.id.fragment_static);
发送数据:fragment.setArguments(Bundle bundle);
在fragment里接收数据:this.getArguments();//Bundle
二、动态
Bundle bundle1=new Bundle();
bundle1.putString("toDynamicFragment1", "toDynamicFragment1");
FragmentDynamic1 f1=new FragmentDynamic1();
f1.setArguments(bundle1);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.ll_main_fragment_dynamic, f1);
ft.commit();
三、生命周期
相关文章推荐
- android动画基础--旋转移动平移缩放
- Android基础知识--Preference的使用
- Android MediaPlayer 类图和调用关系
- 获取Android APK JNI库
- 4000 推荐几款实用的Android Studio 插件
- Android注解神器ButterKnife使用说明
- Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
- android搜索热词(热门标签)流式布局的实现
- 安卓学习笔记---Android颜色代码表
- textview段落首字母缩进俩个字符
- 70.Android开发知识点总结
- Android版本和API Level对应关系
- Android4.0.3/Android4.2.2 添加 Ethernet
- Android - 文件读写操作 总结
- Android跑马灯两种方式
- Android 设置ImageView宽度固定,其高度按比例缩放适应
- Andriod 中dp dip px sp pt 的 区别
- Android学QQ空间相册浏览类型横向滑动效果显示多图片MyHorizontalScrollView
- android 双service和双进程
- 判断客户端是否是iOS或者Android