Fragment的两种创建方式

    xiaoxiao2026-03-01  8

    一.静态加载的方式

    自定义一个类继承Fragment实现onCreateView()方法 当系统第一次绘制fragment的用户界面时回调的方法.onCreateView()方法中加载XML文件,返回当前Fragment布局转化的View对象把Fragment添加到Activity中

    1.自定义一个类FragmentLeft继承系统的Fragment

    public class FragmentLeft extends Fragment{ public View onCreateView(LayoutInflater inflater ,ViewGroup container , Bundle savedInstanceState){ View view = inflater.inflate(R.laryout.activity_left); return view; } }

    FragmentLeft的布局

    <?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:gravity="center" android:background="#5f00" android:orientation="vertical" > <TextView android:id="@+id/textView_fragmentleft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是FramgnetLeft"/> </LinearLayout>

    2.再自定义一个FragmentRight继承系统Fragment

    public class FragmentRight extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * 1.要加载的布局的资源id * 2.当前布局的父布局 * 3.是否需要追加到父布局 */ View view = inflater.inflate(R.layout.activity_fragmentright, container, false); return view; } }

    FragmentRight的布局

    <?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:gravity="center" android:background="#50f0" android:orientation="vertical" > <TextView android:id="@+id/textView_fragmentright" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是FramgnetRight"/> </LinearLayout>

    3.MainActivity

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }

    MainActivity布局 在main_activity中注册fragment标签 ==注意:它不是一个控件,它是一个引用型的标签==

    <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="horizontal" > <fragment android:id="@+id/fragemntleft" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.ljavadroid.day16_fragmentstatics.FragmentLeft" android:tag="fragment1_tag"/> <fragment android:id="@+id/fragemnt2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.ljavadroid.day16_fragmentstatics.FragmentRight" android:tag="fragment2_tag"/> </LinearLayout>

    二.动态加载Fragment

    1.自定义类FragmentLeft继承Fragment

    public class FragmentLeft extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_left, container,false); return view; } }

    FragmentLeft布局

    <?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:gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment_left" /> </LinearLayout>

    2.自定义FragmentRight继承Fragment

    public class FragmentRight extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_right, container,false); return view; } }

    FragmentRight布局

    <?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:gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragmentright" /> </LinearLayout>

    3.MainActivity

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取Fragment的管理者对象 FragmentManager:用来在Framgent和Activity之间交互的接口 FragmentManager fragmentManager = getFragmentManager(); //将add remove replace等等操作称为事务 FragmentTransaction transaction = fragmentManager.beginTransaction(); //参数1表示fragment添加到Activity的区域的id 2.表示需要具体添加的Fragment对象 transaction.add(R.id.layout_container_leftFragment, new FragmentLeft()); transaction.add(R.id.layout_container_rightFragment, new FragmentRight()); /** *transaction.remove(fragment); *transaction.replace(containerViewId, fragment); *transaction.hide(fragment) *transaction.show(fragment); */ //提交事务--->保存 transaction.commit(); }

    在activity_main布局里面添加两个LinearLayout用于放Fragment

    <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="horizontal" > <LinearLayout android:id="@+id/layout_container_leftFragemnt" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:background="#5f00" > </LinearLayout> <LinearLayout android:id="@+id/layout_container_rightFragemnt" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:background="#500f" > </LinearLayout> </LinearLayout>

    transaction的方法主要有以下几种:

    transaction.add() 向Activity中添加一个Fragmenttransaction.remove() 从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁transaction.replace() 使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体transaction.hide() 隐藏当前的Fragment,仅仅是设为不可见,并不会销毁transaction.show() 显示之前隐藏的Fragmentdetach() 会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护attach() 重建view视图,附加到UI上并显示ransatcion.commit() 提交事务

    注意:在add/replace/hide/show以后都要commit其效果才会在屏幕上显示出来

    文/Mr_LJ(简书作者) 原文链接:http://www.jianshu.com/p/f34c273da0ad 著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
    转载请注明原文地址: https://ju.6miu.com/read-1307496.html
    最新回复(0)