原文链接:http://www.orlion.ga/493/

一、碎片

碎片(Fragment)是一种可以嵌入在活动当中的 UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛。虽然碎片对你来说应该是个全新的概念,但我相信你学习起来应该毫不费力,因为它和活动实在是太像了,同样都能包含布局,同样都有自己的生命周期。你甚至可以将碎片理解成一个迷你型的活动,虽然这个迷你型的活动有可能和普通的活动是一样大的。

那么究竟要如何使用碎片才能充分地利用平板屏幕的空间呢?想象我们正在开发一个新闻应用,其中一个界面使用 ListView展示了一组新闻的标题,当点击了其中一个标题,就打开另一个界面显示新闻的详细内容。如果是在手机中设计,我们可以将新闻标题列表放在一个活动中,将新闻的详细内容放在另一个活动中,如图:

可是如果在平板上也这么设计,那么新闻标题列表将会被拉长至填充满整个平板的屏幕,而新闻的标题一般都不会太长,这样将会导致界面上有大量的空白区域,

因此,更好的设计方案是将新闻标题列表界面和新闻详细内容界面分别放在两个碎片中,然后在同一个活动里引入这两个碎片,这样就可以将屏幕空间充分地利用起来了,如图

二、体验碎片布局

首先创建一个android项目,然后创建左侧碎片布局left_fragment.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6. <Button
  7.     android:id="@+id/button"
  8.     android:layout_width="wrap_content"
  9.     android:layout_height="wrap_content"
  10.     android:layout_gravity="center_horizontal"
  11.     android:text="Button"/>
  12. </LinearLayout>

然后创建右侧碎片布局right_fragment.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#00ff00"
  6.     android:orientation="vertical" >
  7.     <TextView
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_gravity="center_horizontal"
  11.         android:textSize="20sp"
  12.         android:text="This is right fragment"/>
  13. </LinearLayout>

接着新建一个LeftFragment类,继承自Fragment:

  1. package ga.orlion.fragmentdemo;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. public class LeftFragment extends Fragment {
  10.  
  11. @Override
  12. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  13.  
  14. View view = inflater.inflate(R.layout.left_fragment , container , false);
  15. return view;
  16. }
  17. }

这里只重写了Fragment的onCreateView()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态的加载进来,然后再创建一个RightFragment,代码:

  1. package ga.orlion.fragmentdemo;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. public class RightFragment extends Fragment {
  10.  
  11. @Override
  12. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  13.  
  14. View view = inflater.inflate(R.layout.right_fragment , container , false);
  15. return view;
  16. }
  17. }

然后修改activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.  
  6.     <fragment 
  7.         android:id="@+id/left_fragment"
  8.         android:name="ga.orlion.fragmentdemo.LeftFragment"
  9.         android:layout_width="0dp"
  10.         android:layout_height="match_parent"
  11.         android:layout_weight="1"/>
  12.     
  13.     <fragment 
  14.         android:id="@+id/right_fragment"
  15.         android:name="ga.orlion.fragmentdemo.RightFragment"
  16.         android:layout_width="0dp"
  17.         android:layout_height="match_parent"
  18.         android:layout_weight="1"/>
  19. </LinearLayout>

运行结果:

三、动态添加碎片

新建another_right_fragment.xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#ffff00"
  6.     android:orientation="vertical" >
  7.     
  8. <TextView
  9.     android:layout_width="wrap_content"
  10.     android:layout_height="wrap_content"
  11.     android:layout_gravity="center_horizontal"
  12.     android:textSize="20sp"
  13.     android:text="This is another right fragment"/>
  14.  
  15. </LinearLayout>

然后创建AnotherRightFragment:

  1. package ga.orlion.fragmentdemo;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. public class AnotherRightFragment extends Fragment{
  10.  
  11. @Override
  12. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  13.  
  14. View view = inflater.inflate(R.layout.another_right_fragment , container , false);
  15. return view;
  16. }
  17. }

接下来就是把这个碎片动态加载到活动中。修改activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.  
  6.     <fragment 
  7.         android:id="@+id/left_fragment"
  8.         android:name="ga.orlion.fragmentdemo.LeftFragment"
  9.         android:layout_width="0dp"
  10.         android:layout_height="match_parent"
  11.         android:layout_weight="1"/>
  12.     
  13.     <FrameLayout 
  14.         android:id="@+id/right_layout"
  15.         android:layout_width="0dp"
  16.         android:layout_height="match_parent"
  17.         android:layout_weight="1">
  18.         
  19.         <fragment
  20.             android:id="@+id/right_fragment"
  21.             android:name="ga.orlion.fragmentdemo.RightFragment"
  22.             android:layout_width="match_parent"
  23.             android:layout_height="match_parent" />
  24.     </FrameLayout>
  25. </LinearLayout>

FrameLayout没有任何定位,所有的空间摆放在布局的左上角,因为这里只需要在布局中放如一个碎片,因此非常适合FrameLayout。然后我们将在代码中替换FrameLayout里的内容,从而实现动态添加碎片的功能,修改MainActivity中的代码,如下所示:

  1. package ga.orlion.fragmentdemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.FragmentManager;
  5. import android.app.FragmentTransaction;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10.  
  11. public class MainActivity extends Activity implements OnClickListener{
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. Button button = (Button) findViewById(R.id.button);
  18. button.setOnClickListener(this);
  19. }
  20.  
  21. @Override
  22. public void onClick(View v) {
  23.  
  24. switch (v.getId()) {
  25. case R.id.button:
  26. AnotherRightFragment fragment = new AnotherRightFragment();
  27. FragmentManager fragmentManager = getFragmentManager();
  28. FragmentTransaction transaction = fragmentManager.beginTransaction();
  29. transaction.replace(R.id.right_layout, fragment);
  30. transaction.commit();
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36.  
  37. }

可以看到我们给左侧碎片中的按钮注册了一个点击事件然后将动态添加碎片的逻辑都放在了点击事件里。动态添加碎片主要分为5步:

1.创建待添加的碎片实例

2.获取到FragmentManager,在活动中可以直接调用getFragmentManager()方法

3.开启一个事务,通过调用beginTransaction()方法

4.向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id和待添加的碎

片实例。

5.提交事务,调用 commit()方法来完成。

Android入门(六)碎片的更多相关文章

  1. Android入门(七)碎片的生命周期与限定符

    原文链接:http://www.orlion.ga/560/ 这篇文章实际已经在上篇文章中写的差不多了,但是万恶的wordpress没保存!已经不止一次出现这种情况了! 一.碎片的生命周期 1.碎片的 ...

  2. 小猪的Android入门之路 day 1

    小猪的Android入门之路 Day 1 Android相关背景与开发环境的搭建 ------转载请注明出处:coder-pig 本节引言: 随着社会经济的发展,移动互联网的越来越热,手机APP开发显 ...

  3. 脑残式网络编程入门(六):什么是公网IP和内网IP?NAT转换又是什么鬼?

    本文引用了“帅地”发表于公众号苦逼的码农的技术分享. 1.引言 搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地址)和内网IP(即局域网IP地址),但他们的区别是什么?又有什么关系呢 ...

  4. 网络编程懒人入门(六):深入浅出,全面理解HTTP协议

    本文引用了自简书作者“涤生_Woo”的文章,内容有删减,感谢原作者的分享. 1.前言 HTTP(全称超文本传输协议,英文全称HyperText Transfer Protocol)是互联网上应用最为广 ...

  5. Android入门之文件系统操作

    Android入门之文件系统操作(二)文件操作相关指令 (转)   (一)获取总根 File[] fileList=File.listRoots(); //返回fileList.length为1 // ...

  6. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  7. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  8. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  9. android 入门 005(登录记住)

    android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

随机推荐

  1. delphi7 编译程序时报win32.indcu.a病毒的解决方法

    Delphi7用了很久一直都没问题,同一个工程文件昨天编译时mod32还不会报毒,今天重新编译时,生成的exe突然nod32报毒. 提示: “Project1.exe Win32/Induc.A 病毒 ...

  2. hdu 4982 Goffi and Squary Partition

    Goffi and Squary Partition Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Subm ...

  3. excel转化为table(去掉所有列值都为空的值一行,即返回有效值的DataTable)

    /// <summary> /// 去掉所有列值都为空的值一行,即返回有效值的DataTable /// </summary> /// <param name=" ...

  4. 欢迎访问我的最新个人技术博客http://zhangxuefei.top

    博客园已停止更新,欢迎访问我的最新个人技术博客http://zhangxuefei.top

  5. 不用asp.net MVC,用WebForm照样可以实现MVC(请看最后一句话)

    在<避开WebForm天坑,拥抱ASP.Net MVC吧>这篇博客中我讲到了ASP.net WebForm由于一些先天的“诱导犯罪”的缺陷,现在用ASP.net MVC的公司越来越多.但是 ...

  6. 记一次Redis和NetMQ的测试

    Redis是一个高速缓存K-V数据库,而NetMQ是ZeroMQ的C#实现版本,两者是完全不同的东西. 最近做游戏服务器的时候想到,如果选择一个组件来做服务器间通信的话,ZeroMQ绝对是一个不错的选 ...

  7. 关于使用ABP框架搭建的项目升级时需要注意的问题汇总

    ABP理论学习总目录 一步一步使用ABP框架搭建正式项目系列教程 ABP之Module-Zero学习目录 本篇目录 说明 升级方法 问题_01:Log4Net导致编译不成功 2015/12/18更新 ...

  8. 设计模式之美:Template Method(模板方法)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Template Method 模式结构样式代码. 意图 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. Templat ...

  9. PS 多次剪裁同一图片

    一个图品里面有两个小图,要分别抠出来. 我以前的做法是,先扣一个,重新打开文件,再扣另外一个. 今天发现一个简单的办法,不用重新打开文件. 就是在扣完第一个的时候,打开历史记录面板,双击 打开 动作, ...

  10. 案例研究:CopyToAsync

    返回该系列目录<基于Task的异步模式--全面介绍> 把一个流拷贝到另一个流是有用且常见的操作.Stream.CopyTo 方法在.Net 4中就已经加入来满足要求这个功能的场景,例如在一 ...