Android中的Fragment——02(Fragment的动态加载)

来源:互联网 发布:伦敦金投资软件 编辑:程序博客网 时间:2024/05/17 22:38

动态加载和静态加载:

----所谓的静态加载,不过是用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的布局

MyFragment
public class MyFragment extends Fragment {@Overridepublic 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的布局文件

我的Activity
public 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添加进来前的效果:




0 0
原创粉丝点击