第一步先看看怎么创建一个fragment------------------
创建一个继承自Fragment(或者Fragment子类的子类)的类,
Generate重写OnCreate方法,需要写好一个布局文件。本例子
创建了fragment类。
public class ListFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/**
* 第一个参数是布局文件.xml
* 第二个参数是ViewGroup对象,直接使用container/或者不使用容器,如下
* View view=inflater.inflate(R.layout.fragment,null);
* 第三个参数是false
*/
View view=inflater.inflate(R.layout.fragment,container,false);
return view;
}
}
创建的fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment内容"/>
</LinearLayout>
-----------------------------我是华丽丽的分割线------------------------------
--------------------已经初步能够创建一个fragment------------------------
第二步在activity·增加fragment -----------------------
在Android中,有两种方法增加fragment,
一:直接在布局文件增加Fragment。
二:java代码中,在activity运行当中动态的增加Fragment。
*** --------第一种:直接在布局文件增加Fragment***
我们先看看在布局文件增加Fragment,非常简单,只需要使用fragment标签,使用name属性,标注一下对应的Fragment就可以,也就是下面的
android:name="com.dede.myapplication.ListFragment"这句,包名加Fragment类路径。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="com.dede.myapplication.ListFragment"
android:id="@+id/list"/>
</LinearLayout>
*** --------第二种:在activity运行当中动态的增加Fragment---------***
先搞定一个Fragment
创建一个Fragment类
public class DetailFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/**
* 第一个参数是布局文件.xml
* 第二个参数是ViewGroup对象,直接使用container
*第三个参数是false
*/
View view=inflater.inflate(R.layout.fragment_detail,container,false);
return view;
}
}
其对应的xml布局文件为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment内容:detail"/>
</LinearLayout>
来看看MainActivity的布局文件
为什么要设置一个FrameLayout,先放着,后面说。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/left"
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="activity"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fl">
</FrameLayout>
</LinearLayout>
看MainActivity的内容
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取一个DetailFragment示例
DetailFragment detailFragment=new DetailFragment();
//获取一个Transaction实例
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
/**
* 第一个参数,放入的容器,我们可以使用安卓默认的android.R.id.content,
* 也可以自己创建容器,例如在MainActivity的xml布局文件写入
* 一个帧布局管理器,写上Id,第二个是要显示的Fragment
*/
ft.add(R.id.fl,detailFragment);
ft.commit();//提交事务
}
}
这就是为什么要增加一个FrameLayout了,因为要设置Fragment到父容器里面,而这个决定了它在页面的什么位置。
如果要替换fragment,采用如下的语句
ft.replace(R.id.fl,fragmentObject)//两个参数,第一个是父容器,第二是要替换的新建的fragment对象,
ft.commit();