什么是碎片
碎片是一种可以放到活动里面的一个UI片段,是活动的一部分,使活动更加的模块化设计。我们可以认为碎片是一种子活动。
碎片往往可以使程序更加好运用大屏幕的空间,比如平板。
虽然碎片是新的概念,但是它和活动非常相识
- 碎片拥有自己的布局,自己的行为及自己的生命周期回调
- 当活动在运行的时候,你可以在活动中添加或者移除碎片
- 可以合并多个碎片在一个单一的活动中来构建多栏的UI
- 碎片可以被用在多个活动中
- 碎片的生命周期和它的宿主活动紧密关联。这意味着活动被暂停,所有活动中的碎片被停止
- 碎片可以实现行为而没有用户界面组件
- 碎片是 Android API 版本11中被加入到 Android API[1]采集于
如果设计出来的界面没有运用碎片,那么在平板上看会给用户带来不好的体验
碎片的使用
本次的实例也是部分采集于《第一行代码》[2]书本链接
首先准备一个FragmentTest项目,和一个平板的虚拟机
然后新建两个左右布局文件
left_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the first fragment!"/>
</LinearLayout>
right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the second fragment!"/>
</LinearLayout>
两个布局没啥不同,放了两句不一样的话,并把背景颜色进行了修改
创建好了布局文件之后,就要创建相应的Fragment了,也就是两个绑定的类
新建一个LeftFragment和RightFragment
都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的
这样更有利于程序的兼容性.
LeftFragment:
public class LeftFragment extends Fragment {
@Nullable
@Override
//重写Fragment中的onCreateView()方法,通过inflate()将定义的left_fragment布局动态加载进来
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_frangment,container,false);//得到对应的布局文件
return view;
}
}
RightFragment:
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//在这个片段构造它的用户接口视图(即布局)时调用。
Log.d(TAG,"onCreateView");
View view = inflater.inflate(R.layout.right_fragment,container,false);//得到对应的布局文件
return view;
}
也很简单,只是重写了onCreateView方法来加载不同的布局文件.
接下来就应该在主页面添加这两个碎片了
修改activity_main的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/main_firstfragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<fragment
android:id="@+id/main_secondfragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
</LinearLayout>
在主布局中使用标签在布局中添加碎片
一定要记得通过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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right fragment"
/>
</LinearLayout>
其实就是把背景改成黄色(我怀疑你搞黄色!
然后记得像上面的一样新建一个类作为右边碎片
然后修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/main_firstfragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<FrameLayout>
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_perent"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>
我们将右碎片换成了一个FrameLayout,我们等下需要往这个布局里面添加碎片
修改MainActivity的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册左碎片中的按钮
Button button = (Button) findViewById(R.id.button);
//注册按钮监听
button.setOnClickListener(this);
//调用replaceFragment动态加载原有的Right碎片
replaceFragment(new RightFragment());
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button:
//当点击按钮的时候又会把replaceFragment替换成AnotherRightFragment
replaceFragment(new AnotherRightFragment());
break;
default:
break;
}
}
//动态添加碎片需要5步
private void replaceFragment(Fragment fragment){
//获取一个FragmentManager,直接调用getSupportFragmentManager()
FragmentManager fragmentManager = getSupportFragmentManager();
//开启一个事务,通过调用beginTransaction()得到
FragmentTransaction transaction = fragmentManager.beginTransaction();
//向容器添加或替换碎片,用replace()实现,传入容器ID和代添加的碎片实例
transaction.replace(R.id.right_layout,fragment);
//调用addToBackStack()实现类似于返回伐的模式
transaction.addToBackStack(null);
//提交事务,调用commit()
transaction.commit();
}
}
