Android上的界面展示都是通过Activity实现的。Activity实在是太经常使用了。我相信大家都已经很熟悉了,这里就不再赘述。 可是Activity也有它的局限性,相同的界面在手机上显示可能很好看,在平板上就未必了,由于平板的屏幕很大。手机的界面放在平板上可能会有过分被拉长、控件间距过大等情况。这个时候更好的体验效果是在Activity中嵌入”小Activity”。然后每个”小Activity”又能够拥有自己的布局。这就是Fragment碎片技术。


一、Fragment简单介绍

  Android是在Android 3.0 (API level 11)開始引入Fragment的。能够把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity执行的时候能够载入或者移除Fragment模块。

能够把Fragment设计成能够在多个Activity中复用的模块。

当开发的应用程序同一时候适用于平板电脑和手机时。能够利用Fragment实现灵活的布局,改善用户体验。

二、Fragment生命周期

  由于Fragment必须嵌入在Acitivity中使用。所以Fragment的生命周期和它所在的Activity是密切相关的。

  假设Activity是暂停状态。当中全部的Fragment都是暂停状态;假设Activity是stopped状态。这个Activity中全部的Fragment都不能被启动。假设Activity被销毁,那么它当中的全部Fragment都会被销毁。可是,当Activity在活动状态。能够独立控制Fragment的状态,比方加上或者移除Fragment。

  当这样进行fragment transaction(转换)的时候,能够把fragment放入Activity的back stack中。这样用户就能够进行返回操作。

  

  Activity与Fragment生命周期对照图

  

三、两个简单实例

  1. 简单的Fragment练习,Activity与Fragment通信



    布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <FrameLayout
android:layout_weight="1"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="0dp" >
</FrameLayout> <android.support.v4.app.FragmentTabHost
android:id="@+id/tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </LinearLayout>

Java文件

package com.example.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.Toast; public class MyFragment extends ListFragment{ String show1[] = {"1.1","1.2","1.3","1.4"};
String show2[] = {"2.1","2.2","2.3","2.4"};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String show[] = null;
Bundle bundle = getArguments();
if(bundle == null)
show = show1;
else {
show = show2;
Toast.makeText(getActivity(), (CharSequence) bundle.get("key"), 1).show();
}
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, show));
}
}
package com.example.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost; public class MainActivity extends FragmentActivity { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); FragmentTabHost tabHost = (FragmentTabHost) findViewById(R.id.tab);
tabHost.setup(this, getSupportFragmentManager(), R.id.content); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), MyFragment.class, null);
Bundle b = new Bundle();
b.putString("key", "I am tab2");
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher)), MyFragment.class, b); } }

执行结果





2. 比較好看的demo,模仿微信主页面



碎片布局文件fragment_1,2,3,4,5.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/mm1" >
</ImageView> </LinearLayout>

tab_item_view.xmlbutton布局

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/tab_home_btn">
</ImageView> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="10sp"
android:textColor="#ffffff">
</TextView> </LinearLayout>

主页面布局main_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/maintab_toolbar_bg"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost> </LinearLayout>

FragmentPage1,2,3,4,5.java碎片实现

package com.vhreal.myfragmenttest;

import com.vhreal.myfragmenttest.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentPage1 extends Fragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_1, null);
}
}

主页面实现MainTabActivity.java

package com.vhreal.myfragmenttest;

import com.vhreal.myfragmenttest.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; /**
* @author vhreal
* 功能描写叙述:自己定义TabHost
*/
public class MainTabActivity extends FragmentActivity {
// 定义FragmentTabHost对象
private FragmentTabHost mTabHost; // 定义一个布局
private LayoutInflater layoutInflater; // 定义数组来存放Fragment界面
private Class fragmentArray[] = { FragmentPage1.class, FragmentPage2.class,
FragmentPage3.class, FragmentPage4.class, FragmentPage5.class }; // 定义数组来存放button图片
private int mImageViewArray[] = { R.drawable.tab_home_btn,
R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
R.drawable.tab_square_btn, R.drawable.tab_more_btn }; // Tab选项卡的文字
private String mTextviewArray[] = { "首页", "消息", "好友", "广场", "很多其它" }; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_layout); initView();
} /**
* 初始化组件
*/
private void initView() {
// 实例化布局对象
layoutInflater = LayoutInflater.from(this); // 实例化TabHost对象,得到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); // 得到fragment的个数
int count = fragmentArray.length; for (int i = 0; i < count; i++) {
// 为每个Tabbutton设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
.setIndicator(getTabItemView(i));
// 将Tabbutton加入进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
// 设置Tabbutton的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.selector_tab_background);
}
} /**
* 给Tabbutton设置图标和文字
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextviewArray[index]); return view;
}
}

执行结果

四、參考引用

Android Fragment 简单实例的更多相关文章

  1. Android Fragment (二) 实例1

    千呼万唤始出来,今天就也写一篇Frament 的简单实例.先看效果: 一看这效果,首先我们的配置资源文件:new -->android xml -->selector --> 四个图 ...

  2. Android Fragment (二) 实例2

    由于看客的要求,我就把读者所要的写出来. 由于上一篇是每一个Fragment 实例了同一个layout.xml ,造成了读者的困惑,这篇我就让每一个Fragment 加载一个不同的layout.xml ...

  3. 【转】Android Https服务器端和客户端简单实例

    转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...

  4. Android Fragment 实例

    Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...

  5. android Jni NDK开发环境搭建及其简单实例的编写

    android  Jni  NDK开发环境搭建及其简单实例的编写 由于工作需要,需要采用开发想要的JNI,由于之前没有接触过安卓的开发,所以更加网上的帖子,学习了下.遇到了些问题,然后总结下学习过程中 ...

  6. android viewpager 拿到当前显示的 fragment 的实例

    一个 ViewPager 通过 FragmentPagerAdapter 绑定了 3 个 fragment 可以通过 Fragment fragment = getSupportFragmentMan ...

  7. 【Android自学日记】【转】Android Fragment 真正的完全解析(下)

    上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...

  8. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  9. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

随机推荐

  1. 2013ACM省赛题目

    地址就贴这一个吧 都在附近 当时回来也没做做 一伤心了 二是当时实在太弱了 先补两道DP E题的区间DP dp[i][j]  截止到i位置以字母j为结束的上升序列 正序 逆序各来一遍 再循环一遍保存一 ...

  2. 【spring-boot】快速构建spring-boot微框架

    spring-boot是一个快速构建环境的一套框架,其设计理念是尽可能的减少xml的配置,用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义 ...

  3. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  4. 如何配置Flash Media Live Encoder (FMLE)从而使用Azure直播服务

    Azure媒体服务中的直播服务已经在中国Azure开始公共预览.通过这篇英文博客,您可以了解到直播服务对RTMP协议的支持.以及多种客户端编码器的配置. http://blogs.msdn.com/b ...

  5. WCF配置文件详解(一)

    <?xml version="1.0" encoding="utf-8" ?><configuration>    <!-- &l ...

  6. c#中单元测试

    从走进.net后发现每天有写不完的代码,有做不完的测试...人感觉都已经机械,我们需要认清自己调整好心态,问下自己是否真的喜欢编程.我的答案当然也就是我爱编码,编码给我带来了许多欢乐,每天都给我体验小 ...

  7. Js获取Cookie值的方法

    function getCookie(name) { var prefix = name + "=" var start = document.cookie.indexOf(pre ...

  8. JS中图片的放大缩小没反应

    这段代码无反应: 代码如下: <script type="text/javascript"> onload = function () { document.getEl ...

  9. 一个可能是pip的一个BUG

    今天重新安装了Python,把Python的安装位置改为 D:\Program Files\Python\Python34\ 用pip 安装 Django 的时候出现一下错误 >pip inst ...

  10. QTP、LoadRunner、QC工具下载地址

    QTP10.0工具下载地址:http://h30302.www3.hp.com/prdownloads/T6510-15063.zip?ordernumber=380454070&itemid ...