Android中的Fragment——02(Fragment的动态加载)
2016-07-08 00:50
489 查看
动态加载和静态加载:
----所谓的静态加载,不过是用xml定义的方式,将Fragment加载进内存---- 而动态加载,不过是通过代码 将一个Fragment加载进内存罢了
----add(..) : 添加一个Fragment(指定要添加的Fragment和要插入的View ), 与此类似的还有 remove(..)移除,replace(..)替换等
动态加载的步骤:
1,准备好Fragment类2,准备好Fragment对象
3,开启一个处理Fragment的事务
4,执行对Fragment的添加add(..),替换replace(..),remove(..)移除 等操作
5,对事务进行提交
注意:
如果允许用户通过按下BACK键来返回到当前Fragment添加进来前的状态,调用commit()之前,可以加入addToBackStack()方法addToBackStack(String name) :点击back键后,让手机返回到该Fragment加载进来之前的状态,参数可以是个name,也可以是null
我的动态加载的Demo:
Fragment类以及Fragment的布局
MyFragmentpublic class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_fragment, null); return view; } }layout_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="150dp" android:layout_height="150dp" android:background="@android:color/black" android:layout_centerInParent="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是个Fragment" android:textColor="@android:color/white" android:layout_centerInParent="true" android:textSize="20sp" android:id="@+id/tv_fragment"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:text="点击按钮" android:id="@+id/bt_fragment"/> </RelativeLayout> </RelativeLayout>
我的Activity类,以及Activity的布局文件
我的Activitypublic class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { /* * 如果允许用户通过按下BACK键来返回到之前的一个Fragment状态,调用commit()之前,可以加入addToBackStack()方法 * * addToBackStack(String name) * 让手机返回到该Fragment加载进来之前的状态,参数可以是个name,也可以是null */ //开始进行动态加载 //2,准备好Fragment对象 MyFragment myFragment = new MyFragment(); //3,开启一个处理Fragment的事务 FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); //4,执行对Fragment的添加add(..),替换replace(..),remove(..)移除 等操作 beginTransaction.add(R.id.container, myFragment); beginTransaction.addToBackStack(null);//让手机返回到该Fragment加载进来之前的状态,参数可以是个name,也可以是null //5,对事务进行提交 beginTransaction.commit(); } }
Activity的布局文件 activity_main.xml
<RelativeLayout 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" tools:context="com.xulunpeng.dongtaidown.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动态加载Fragment" android:layout_centerHorizontal="true" android:onClick="onClick"/> <LinearLayout android:layout_width="300dp" android:layout_height="300dp" android:orientation="vertical" android:background="@android:color/holo_green_dark" android:layout_centerInParent="true" android:id="@+id/container"> </LinearLayout> </RelativeLayout>
注意:
---- 如果在 commit() 之前添加了addBackToStack(),那么点击back键时,将会回到当前Fragment 添加进来前的状态效果图:
点击了动态加载后的效果:点击了back键后,返回到该Fragment添加进来前的效果:
相关文章推荐
- ANR机制
- Android RecyclerView 实现快速滑动
- [置顶] Android仿美团外卖点菜联动列表
- Android加载动画系列——BatteryLoading
- Android组件----RelativeLayout相对布局的属性详解
- Android中的Drawable的状态
- Android MVP初试
- android 开发时候出现的错误异常集
- android:layout_weight计算
- APK瘦身记,如何实现高达53%的压缩效果
- Android图片处理 - ImageView
- Android之分享篇(SharedSDK一键分享):简单易行的N多平台一键分享
- Android中获取内置SD卡和外置SD卡路径的方法
- Android Context 上下文 你必须知道的一切
- 常用的adb命令
- Android
- 自定义View注意点
- Android之Button
- Android开发之自定义无背景Dialog的实现
- Android 获取文字的高度