背景

Fragment(碎片)是Android3.0提出来的概念,过后的次要目标是为了适配平板,筹备将Activity碎片化,每个局部独自解决。fragment相较于Activity更加轻量,然而展现的成果与Actvity差不多,这给页面布局带来了更多的灵活性。因为Androidx的强制推广,以前的文章过于老旧,所以写下这一系列文章总结一下。

Fragment的劣势

  • 模块化 能够不必将所有代码写在Activity中,而是写在Fragment中,升高耦合。
  • 可复用 fragment能够被多个Activity重用。
  • 可适配 能够对不同大小的屏幕进行适配。

Fragment的简略应用

办法一:动态增加

动态增加就是将fragment当作一个控件内嵌在Activity的布局文件之中,这种办法并不提倡,这就义了Fragment的灵活性,没有施展出Fragment应有的作用。应用如下
两个Fragment FragmentOne和FragmentTwo的布局文件如下

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:background="@color/colorAccent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是FragmentOne"        android:layout_centerInParent="true">    </TextView></RelativeLayout><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是FragmentTwo"        android:layout_centerInParent="true">    </TextView></RelativeLayout>

两个Fragment的类文件,留神继承Androidx包下的Fragment,通过重写onCreateView办法返回Fragment视图,对于Fragment的生命周期将会下一篇文章重点介绍。

public class FragmentOne extends Fragment {    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_one, container, false);        return view;    }}public class FragmentTwo extends Fragment {    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_two, container, false);    }}

接下来将Fragment当作控件退出MainActivity布局文件,留神这里的id和name必填,否则会报错

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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">   <LinearLayout       android:orientation="vertical"       android:layout_width="match_parent"       android:layout_height="match_parent">       <fragment           android:id="@+id/fragment_one"           android:layout_width="match_parent"           android:layout_weight="1"           android:name="com.snow.fragmentdemo.FragmentOne"           android:layout_height="match_parent">       </fragment>       <fragment           android:id="@+id/fragment_two"           android:layout_width="match_parent"           android:layout_height="match_parent"           android:layout_weight="1"           android:name="com.snow.fragmentdemo.FragmentTwo">       </fragment>   </LinearLayout></FrameLayout>

成果如下

办法二:动静增加



动静加载是应用FragmentManager和FragmentTransaction提供的API实现的,在Androidx中通过getSupportFragmentManager()获取FragmentManager。

  FragmentManager fm = getSupportFragmentManager();  FragmentTransaction transaction = fm.beginTransaction();

下表列出了罕用的API.

FragmentMananger

返回值函数形容
FragmentfindFragmentById通过ID来获取Fragment
FragmentfindFragmentByTag通过Tag来获取Fragment
List< Fragment>getFragments获取所有被增加到FragmentManager中的Fragment
FragmentTransactionbeginTransaction获取FragmentTransaction

FragmentTransaction

返回值函数形容
FragmentTransactionadd向container中退出一个Fragment
FragmentTransactionremove移除一个Fragemnt
FragmentTransactionreplace替换Fragment
FragmentTransactioncommit提交事务
FragmentTransactionshow展现Fragment
FragmentTransactionhide暗藏Fragment
FragmentTransactionaddToBackStack将事物退出回退栈
事项留神
  • replace是将FragmentManager中所有的Fragment先移除再退出Fragment
  • 每一个事物只能提交一次,否则回报错反复提交
  • 调用addToBackStack后,按物理回退建会回滚,fragment页面内容也不会变,如TextView里的内容,没有调用,按物理回退建,间接退出Activity。
  • 一个commit能够蕴含多个操作,如add,remove等
要害代码
public class MainActivity extends AppCompatActivity implements  View.OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        findViewById(R.id.button1).setOnClickListener(this);        findViewById(R.id.button2).setOnClickListener(this);    }    @Override    public void onClick(View v) {        FragmentManager fm = getSupportFragmentManager();        switch (v.getId()) {            case R.id.button1:                FragmentTransaction transaction = fm.beginTransaction();                transaction.replace(R.id.content, new FragmentOne());                transaction.commit();                break;            case R.id.button2:                FragmentTransaction transaction2 = fm.beginTransaction();                transaction2.replace(R.id.content, new FragmentTwo()).commit();                break;        }    }}

最初

点赞就是最大的反对,更多信息关注公众号QStack,追寻最纯正的技术,享受编程的高兴。