美文网首页
Fragment的两种创建方式

Fragment的两种创建方式

作者: Mr_LJ | 来源:发表于2016-07-12 12:42 被阅读669次

一.静态加载的方式

  • 自定义一个类继承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>

相关文章

网友评论

      本文标题:Fragment的两种创建方式

      本文链接:https://www.haomeiwen.com/subject/cdzjjttx.html