碎片是什么
碎片(Fragment)是一种可以嵌入在活动中的UI片段,它能让程序更加合理和充分的利用大屏幕,非常重要的是碎片还可以动态加载,可以让我们在一个活动中很方便的去切换展示的内容,这可以提高用户体验,比如微信,当我们点击下方tab bar时并没有开启一个新的活动,而是把tab bar上方展示的内容切换了,这要比每次点击都跳转到新页面的用户体验好多了。
碎片的使用方式
首先新建一个FragmentTest项目,然后开始我们的碎片探索之旅。
新建一个左侧碎片布局left_fragment.xml,代码如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
android:textAllCaps="false"></Button>
</LinearLayout>
这个布局非常简单,只放置了一个按钮,并让它水平居中显示。
然后新建右侧碎片布局right_fragment.xml,代码如下所示:
<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="#00ff00">
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="This is right fragment"></TextView>
</LinearLayout>
可以看到,我们将这个布局的背景色设置成了绿色,并放置了一个TextView用于显示一段文本。
接着新建一个LeftFragment类,并让它继承自Fragment。
现在编写一下LeftFragment中的代码,如下所示:
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
这里仅仅是重写了Fragment的onCreateView()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态加载进来。
接着我们用同样的方法再新建一个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中的代码,如下所示:
<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.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
可以看到,我们使用了<fragment>标签在布局中添加碎片,其中指定的大多数属性都是你熟悉的,只不过这里还需要通过android:name属性来显式指明要添加的碎片类名,注意一定要将类的包名也加上。
这样最简单的碎片示例就已经写好了,现在运行一下程序,效果如下图所示:
正如我们所期待的一样,两个碎片平分了整个活动的布局。不过这个例子实在是太简单了,在真正的项目中很难有什么实际的作用,因此我们马上来看一看,关于碎片更加高级的使用技巧——动态添加碎片。
动态添加碎片
碎片真正的强大之处在于,它可以在程序运行时动态地添加到活动当中。根据具体情况来动态地添加碎片,你就可以将程序界面定制得更加多样化。
我们还是在上一节代码的基础上继续完善,新建another_right_fragment.xml,代码如下所示:
<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="#ffff00">
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is another right fragment"
android:textSize="20sp"></TextView>
</LinearLayout>
这个布局文件的代码和right_fragment.xml中的代码基本相同,只是将背景色改成了黄色,并将显示的文字改了改。
然后新建AnotherRightFragment(java类)作为另一个右侧碎片,代码如下所示:
public class AnotherRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}
代码同样非常简单,在onCreateView()方法中加载了刚刚创建的another_right_fragment布局。这样我们就准备好了另一个碎片,接下来看一下如何将它动态地添加到活动当中。修改activity_main.xml,代码如下所示:
<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">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></fragment>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
FrameLayout(帧布局),所有的控件默认都会摆放在布局的左上角。由于这里仅需要在布局里放入一个碎片,不需要任何定位,因此非常适合使用FrameLayout。
下面我们将在代码中向FrameLayout里添加内容,从而实现动态添加碎片的功能。修改MainActivity中的代码,如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new RightFragment());
}
public void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
上述代码中,我们编写了一个replaceFragment方法用于改变FrameLayout中承载的碎片,在replaceFragment方法中,第一行代码获取了一个碎片管理器,第二行代码通过这个碎片管理器开启了一个事务,第三行代码就是向容器内添加或替换碎片,第四行代码是将当前事务添加到返回栈中,第五行代码就是提交事务;然后我们在活动创建的生命周期函数中通过replaceFragment方法将RightFragment碎片添加到了FrameLayout当中,这样当我们启动程序初始即在右半会显示RightFragment碎片。
我们接下来实现通过点击左侧碎片的按钮,将FrameLayout承载的RightFragment碎片切换为AnotherRightFragment碎片,这就需要我们在LeftFragment中去调用replaceFragment方法,可以通过getActivity()直接获取到当前碎片所依赖的活动,然后就通过获取到的活动实例来进行调用即可,LeftFragment的具体代码如下:
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);
Button button = view.findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.replaceFragment(new AnotherRightFragment());
}
});
return view;
}
}
这样就实现了点击左侧碎片中的按钮切换右侧FrameLayout所承载的碎片,主要是通过在碎片中去调用活动中的方法来实现的;相反我们也可以在活动中去调用碎片中的方法,在活动中获取碎片实例的代码示例如下:
LeftFragment leftFragment = (LeftFragment) getSupportFragmentManager().findFragmentById(R.id.left_fragment);