Android-Fragment(一)

271 阅读2分钟

「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战

Android-Fragment(一)

碎片(fragment)是一种可以嵌入在活动当中的UI片段,他让程序更加合理充分的利用大屏幕的空间,因而在平板上应用的非常广泛。碎片和活动很像,同样有自己的布局和生命周期,碎片也可以理解为一个迷你型的活动,虽然这个迷你型活动有可能和普通的活动是一样大的。

那么怎么才能充分利用平板的空间呢?如果我们开发一个新闻应用,其中一个界面使用RecyclerView展示了一组新闻的标题,当点击了其中一个标题时,就打开了另一个界面显示详细内容,如果在手机中,我们可以这样设计: 1.PNG

如果平板也这样设计,就会显得标题过长,不美观,也浪费了大量空间:

2.PNG

因此,最好的方案是将新闻标题列表和详细内容放在两个碎片中,然后再同一个活动中引用这两个碎片,这样就可以将屏幕中的空间充分利用起来。

3.PNG

碎片的简单用法

首先我们先写一个最简单的碎片示例练练手,在一个活动中加载两个Fragment,并让这两个碎片平分屏幕。 新建一个左侧碎片布局left_fragment.xml,添加一个Button按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>

新建一个右侧碎片布局right_fragment.xml,添加一个TextView文本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00">
    
    <TextView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is a Fragment"
        android:textSize="20sp"/>
</LinearLayout>

接着新建一个LeftFragment类,并让它继承Fragment,并引用left_fragment.xml布局:

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

与上面操作一样,新建RightFragment类:

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

然后在activity_main.xml中,使用标签在布局中添加碎片,还需要通过android:name属性来显示要添加的碎片名:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.wenjiancunchu.LeftFragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />
    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.wenjiancunchu.RightFragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />
</LinearLayout>