- Fragments简介
Fragment是Activity中用户界面的一个行为或者是一部分。你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用。你可以认为Fragment是Activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在Activity运行时添加或者删除。
两个概念:
Fragment、宿主
fragment的生命周期直接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也被暂停,一旦activity被销毁,它里面所有的fragment也被销毁。
- 设计原理
有了fragment,你可以不必去管理视图体系的复杂变化。通过将activity的布局分割成若干个fragment,可以在运行时编辑activity的呈现,并且那些变化会被保存在由activity管理的后台栈里面。
生命周期图示
将fragment静态添加到Activity里去
在activity的布局文件里声明fragment
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<fragment
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/title_fragment"
android:name="com.example.fragment.TitleFragment"
/>
<fragment
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/content_fragment"
android:name="com.example.fragment.ContentFragment"
/>
</LinearLayout>
创建对应的类和布局文件
TitleFragment.java
public class TitleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.title_layout,container,false);
return view;
}
}
ContentFragment.java
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.content_layout,container,false);
return view;
}
}
对应的布局文件
title_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#423737">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
content_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ddff">
<TextView
android:id="@+id/textView"
android:layout_width="484dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="not data"
android:gravity="center"/>
</LinearLayout>
效果图如下
使用代码添加fragment
布局文件修改一下,这里标题还是用静态添加,content使用代码添加方式
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="horizontal">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/title_fragment"
android:name="com.example.fragment.TitleFragment"
android:layout_weight="1"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/content_layout"
android:layout_weight="3"/>
</LinearLayout>
方法
private void addContentLayout(){
FragmentManager fm=getSupportFragmentManager();
//开启一个事务
FragmentTransaction ft= fm.beginTransaction();
ContentFragment content =new ContentFragment();
//添加fragment
ft.add(R.id.content_layout,content);
// ft.remove();//删除
// ft.replace();//替换
ft.commit();//提交
}
效果和之前一样,这里就不展示了。
- 管理Fragment
想要管理activity中的fragment,可以使用FragmentManager。可以通过在activity中调用getFragmentManager()获得。
使用FragmentManager可以做如下事情,包括:
(1)使用findFragmentById()(用于在activity布局中提供有界面的fragment)或者findFragmentByTag()获取activity中存在的fragment(用于有界面或者没有界面的fragment)。
(2)使用popBackStack()(模仿用户的BACK命令)从后台栈弹出fragment。
(3)使用addOnBackStackChangedListener()注册一个监听后台栈变化的监听器 - fragment出入栈操作
新建一个Fragment文件
public class PopBackFragment extends Fragment {
public PopBackFragment(){}
/**
* fragment的传参方法
* @param title
* @return
*/
public static PopBackFragment getInstance(String title){
PopBackFragment p=new PopBackFragment();
Bundle b=new Bundle();
b.putString("title",title);
p.setArguments(b);
return p;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_pop_back, container, false);
TextView tv=view.findViewById(R.id.textView_text);
tv.setText(title);
return view;
}
}
自动生成的布局文件中对TextView进行小改变
主布局文件
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PopBackTaskActivity">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:onClick="oneClick"
android:layout_marginTop="0dp"
android:text="one" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="twoClick"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:text="two" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="57dp">
</FrameLayout>
</RelativeLayout>
按钮事件
public void oneClick(View v){
PopBackFragment p1=PopBackFragment.getInstance("one");
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content,p1);
//把当前Fragment添加到Activity栈
ft.addToBackStack(null);
ft.commit();
}
public void twoClick(View v){
PopBackFragment p1=PopBackFragment.getInstance("two");
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content,p1);
//把当前Fragment添加到Activity栈
ft.addToBackStack(null);
ft.commit();
}
效果如下
传递参数
public static PopBackFragment getInstance(String title){
PopBackFragment p=new PopBackFragment();
Bundle b=new Bundle();
b.putString("title",title);
p.setArguments(b);
return p;
}