一、碎片的概念(Fragment)

1、碎片:是一种可以嵌入在活动当中的UI片段,他能让程序更加合理和充分地利用大屏幕的空间。
2、碎片可以被认为是迷你型的活动,在一个活动中可以引用多个碎片,这样就可以充分利用活动界面空间

二、碎片的使用

2.1 碎片的创建和实现

1、新建一个左侧碎片布局left_fragment.xml和右侧碎片布局right_fragment,并添加一些控件
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="Button"/></LinearLayout>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:background="#00ff00"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/></LinearLayout>

2、分别创建一个LeftFragment类和RightFragment类,均继承Fragment类,重写onCreateView()方法,这样就创造了两个碎片

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

3、在主活动布局activity_main.xml中实现代码

    <fragmentandroid:id="@+id/left_fragment"android:name="com.example.testfragment.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight = "1"/><fragmentandroid:id="@+id/right_fragment"android:name="com.example.testfragment.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight = "1"/>
 android:name 用于显示指明要添加的碎片的类在包下所处的位置

2.2 动态地添加碎片

1、在上述操作的基础上,再新建一个another_right_fragment.xml布局文件,添加控件

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:background="#ffff00"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid: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>

2、新建一个AnotherRightFragment类作为另一个右侧碎片,继承Fragment,实现代码

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

3、在activity_main.xml中,实现代码

     <fragmentandroid:id="@+id/left_fragment"android:name="com,example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight = "1"/><FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight = "1"></FrameLayout>
我们将平板中的右侧碎片换成一个FrameLayout布局

4、在主活动中,动态添加碎片

public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button)findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());//换前碎片,直接在屏幕上显示}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.botton:replaceFragment(new AnotherRightFragment());//换前碎片,点击按钮后显示break;default:break;}}private void replaceFragment(Fragment fragment){//2、构造placeFragment()方法,获取FragmentManager,在活动中调用getSupportFragmentManager()方法得到FragmentManager fragmentManager = getSupportFragmentManager();//3、开启一个事务,调用beginTransaction();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//4、向容器内添加或替换碎片,用replace()实现fragmentTransaction.replace(R.id.right_layout,fragment);//5、调用commit()方法提交事务fragmentTransaction.commit();}
}
 1、创建一个replaceFragment()方法,在onCreate()方法中调用该方法并传入一个Fragment对象,动态添加对象的实例2、构造placeFragment()方法,获取FragmentManager,在活动中调用getSupportFragmentManager()方法得到3、开启一个事务,调用beginTransaction();4、向容器内添加或替换碎片,用replace()实现5、调用commit()方法提交事务

2.3 在碎片中模拟返回栈

在一般情况下,碎片中摁下Back键,程序会直接退出;如若想实现摁下back键回到碎片前一个碎片,即实现返回栈功能,则需要在replaceFragment()方法提交事务前添加下面这一行代码:

        fragmentTransaction.addToBackStack(null);//该方法可以接受一个名字用于描述返回栈的状态,此时摁下back键,程序会回到前一个碎片界面

三、碎片的声明周期

3.1 碎片的状态和回调

1、状态

  1. 运行状态 :当一个碎片可见时,且所关联的活动正处于运行状态时,该碎片也处于运行状态
  2. 暂停状态:当一个活动进入暂停状态,与他相关联的可见碎片就会进入暂停状态
  3. 停止状态:当一个活动进入停止状态,与它相关联的碎片就会进入停止状态
  4. 销毁状态:活动被销毁时,碎片也会跟着被销毁

2、回调方法

  1. onAttach():当碎片和活动建立关联时调用
  2. onCreateView():为碎片创建视图时调用
  3. onActivityCreated():确保与碎片相关联的活动一定已经创建完毕时调用
  4. onDestroyView():当与碎片关联的视图被移除时调用
  5. onDetach():当碎片与活动解除关联时调用

3、碎片完整的生命周期示意图:

四、动态加载布局的技巧

4.1 使用限定符

限定符:用于判断程序应该使用单页模式还是双页模式
举例实现:
1、修改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"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.testfragment.LeftFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

2、在res目录下新建layout_large文件夹,在这个文件夹下新建一个布局,且名字也叫做activity_main.xml,代码如下:

     <?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"tools:context=".MainActivity"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.testfragment.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name="com.example.testfragment.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>

3、通过比较,发现第一个同名布局文件只包含了一个碎片,即单页模式;第二个同名布局文件包含了两个碎片,即双页模式。其中layout-large文件夹下的large是个限定符,那些屏幕被认为时large的设备就会自动加载layout-large文件夹下的布局,反之也是一样。因此若是在手机模拟器上运行,则会引用第一个布局文件,若是在平板模拟器上运行,则会引用第二个布局文件
4、Android系统中一些常见的限定符参考博客 常见限定符

4.2 最小宽度限定符的使用

最小宽度限定符允许我们对屏幕的宽度指定一个最小值(以dp为基本单位),然后以这个最小值为临界点,屏幕宽度大于这个值的设备就加载一个布局,反之就加载小于这个宽度的布局,具体设置方法,在layout文件后面加上后缀-sw600dp(以600dp为界限)即可。

Android之碎片相关推荐

  1. android碎片功能实现,Android 列表碎片

    Android 列表碎片 列表碎片的基本实现是用来在碎片中创建项目列表 实例 这个实例解释如何基于 ArrayAdapter 来创建列表碎片.让我们按照下面的步骤开始: 步骤 描述 1 使用 Andr ...

  2. android碎片整理工具,Android的碎片整理

    Android手机与平板高级历史橡皮擦和性能的助推器 ★★★★第一轮中的新工具支付Android上最流行的新的生产力应用程序,下载吧!清除历史搜索,杀敌多余的电池寿命,增加单键的任务!"碎片 ...

  3. android碎片化的解决方法,解决 Android 设备碎片化--屏幕适配

    随着支持 Android 系统的设备(手机.平板.电视.手表)的增多,设备碎片化.品牌碎片化.系统碎片化.传感器碎片化和屏幕碎片化的程度也在不断地加深. 在当时初学 Android 的时候,就初步接触 ...

  4. android使碎片切换界面,玩转Android中的碎片Fragment

    引言:在Android开发中,我们都知道一些界面的展示经常会用到的就是Activity,但是Activity存在着很大的局限性,比如说手机上的界面显示在平板上面就会发生各种变形的问题,Activity ...

  5. Android学习——碎片(fragment)

    碎片 1.碎片是什么 2.碎片的使用方式 2.1静态加载Fragment 2.2动态添加碎片 2.3在碎片中返回栈 2.4Fragment管理与Frangment事务 2.5Fragment与Acti ...

  6. android studio 碎片,Android studio之碎片学习

    碎片是什么? 碎片( Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用得非常广泛.虽然碎片对你来说应该是个全新的概念,但我相信你学习起 ...

  7. 老式Android中碎片Fragment之间的跳转和数据传递

    随着jetpack组件的使用,fragment之间的跳转和传值已经有相应的demo了.但是有时候难免不用Navigation,或者一些老项目的维护.这里,简单记录下老式fragment怎么跳转和传值. ...

  8. android 广播单词锁屏,[android]利用碎片化时间记单词———单词锁屏

    平时有很多碎片化的时间,如果我们可以利用起来,那么一定可以收到很多的效果.例如我们经常会点亮屏幕,如果在这时记一两个单词,是不是非常不错呢. 单词锁屏就是一款这样的应用.可以设置各种级别的单词如四级. ...

  9. android碎片按钮,Android 碎片(Fragment)

    Android 碎片(Fragment) 碎片是活动的一部分,使得活动更加的模块化设计.我们可以认为碎片是一种子活动. 下面是关于碎片的重要知识点 - 碎片拥有自己的布局,自己的行为及自己的生命周期回 ...

最新文章

  1. Flex警告:framework.swc”具有默认样式并且在 library-path 中,表...
  2. Linux-find命令
  3. ST17H26上下拉电阻设置注意事项
  4. angular动态选择HTML模板,在angular2中动态加载HTML模板
  5. JAVA企业级应用TOMCAT实战视频课程
  6. 【ArcGIS风暴】ArcGIS栅格数据(分区)统计方法总结
  7. Ajax基本案例详解之$.ajax的实现
  8. 在PHP中实现中文汉字验证码
  9. oracle简单对象类型
  10. 【Java】计算符号函数的值
  11. 怎样获取 keycode/keyascii 码?
  12. vue cli 构架vux移动端模板
  13. 如何处理WordPress上传资源报HTTP错误
  14. 【03】品优购电商项目:00-品优购项目代码规范
  15. 芯动联科在科创板IPO过会:拟募资10亿元,金晓冬为实际控制人
  16. VBA多条EXCEL记录写入到WORD文档中
  17. Arduino mega2560蓝牙遥控小车简介
  18. FIDO2.0 认证注册流程
  19. 让vc编译出的程序减小体积
  20. 记一次Very Animation动画插件使用

热门文章

  1. MFI认证是什么意思,什么是MFI认证,要怎么做MFI认证
  2. 奢侈时装,不再迷信千禧一代
  3. [附源码]计算机毕业设计JAVA宠物商城
  4. android手机换机助手,S 换机助手
  5. TNEWS今日头条中文新闻(短文本)分类
  6. JavaSE之Object类
  7. FatFs-目录扫描-获取容量
  8. SPECjbb 分析与使用
  9. Jmeter- 非图形化界面运行脚本
  10. “小程序化”,一种创新的混合App开发模式