(1)Fragment
允许将Activity拆分成多个完全独立封装的可重用组件
**Fragment依附于Activity ,不独立存在,
一个Activity中可以包含多个Fragment,一个Fragment可以被包含在多个Activity中**
创建Fragment时,需要实现
将Fragment加载到Activity中主要有两种方式:
静态加载:将Fragment加载到布局文件中
动态加载:在Activity代码中动态添加Fragment
讲台加载Fragment的步骤:
1. 生成Fragment类文件: 在java 中 new fragment 选择 Blank Fragment
2. 把新城生成的Fragment类中没用的内容删掉
3.把Fragment 加载到Activity中:
<fragment
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.chapter4ruanyi.fragment.QianrushiFragment">
</fragment>
注意1:添加 android:name属性,值是生成的Fragment类
注意2:必须 添加 android:id属性
动态加载方式:
1. 生成Fragment类文件: 在java 中 new fragment 选择 Blank Fragment
2. 把新城生成的Fragment类中没用的内容删掉
3. 在Activity 的布局文件中,添加出发动作,比如 按钮
4. 添加按钮的绑定函数
5. 在绑定函数中添加Fragment:
① 获取Fragment管理器:FragmentManager fragmentManager = getSupportFragmentManager();
② 获取 事务:FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
③ 创建Fragment对象
④ 把创建的对象添加到事务中
⑤ 对事物进行提交
动态删除Fragment步骤:
① 获取Fragment管理器:FragmentManager fragmentManager = getSupportFragmentManager();
② 获取 事务:FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
③ 获取要删除的fragment :fragmentManager.findFragmentByTag("abc");
④ 调用删除方法:fragmentTransaction.remove(fg);
⑤ 对事物进行提交
替换Fragment的步骤:
① 获取Fragment管理器:FragmentManager fragmentManager = getSupportFragmentManager();
② 获取 事务:FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
③ 生成新的Fragmengt对象
④ 调用替换方法:fragmentTransaction.replace(R.id.frameMid,qfg2);
⑤ 对事物进行提交
代码:
=====================================================================================
---------------------------activity_main_qianrushi.java----------------
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivityQianrushi">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左Frame"></TextView>
<Button
android:id="@+id/btadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="add">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove"
android:onClick="remove">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:onClick="change">
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/frameMid"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<fragment
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.chapter4ruanyi.fragment.QianrushiFragment">
</fragment>
</LinearLayout>
</LinearLayout>
-----------------------------MainActivityQianrushi .java----------------------------------------
public class MainActivityQianrushi extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_qianrushi);
}
public void add(View v){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
QianrushiFragment qianrushiFragment = new QianrushiFragment();
QianrushiFragment2 qianrushiFragment2 = new QianrushiFragment2();
fragmentTransaction.add(R.id.frameMid,qianrushiFragment,"abc");
fragmentTransaction.add(R.id.frameMid,qianrushiFragment2,"abc");
fragmentTransaction.addToBackStack("aaa");
fragmentTransaction.commit();
}
public void remove(View v){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
Fragment fg = fragmentManager.findFragmentByTag("abc");
fragmentTransaction.remove(fg);
fragmentTransaction.commit();
}
public void change(View v){
QianrushiFragment2 qfg2 = new QianrushiFragment2();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameMid,qfg2);
fragmentTransaction.commit();
}
}
-------------------------QianrushiFragment .java----------------------------------
public class QianrushiFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_qianrushi, null, false);
}
}
-----------------------------fragment_qianrushi.xml-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.QianrushiFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
------------------------QianrushiFragment2.java ------------------------------
public class QianrushiFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Button bt = getActivity().findViewById(R.id.btadd);
bt.setText("new add");
return inflater.inflate(R.layout.fragment_qianrushi2, null, false);
}
}
------------------------------fragment_qianrushi2.xml----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.QianrushiFragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第二个fragment" />
</FrameLayout>
Fragment生命周期:活动、暂停、停止、销毁