详解安卓Fragment(碎片化)

  Fragment从字面意思理解就是碎片的意思,当然是为了解决安卓各类设备碎片化严重的问题,比如同样一个App在手机上显示效果还不错,但是一旦上了16:9的平板立刻就变了味,使用安卓平板的同学可能体(bei)会(keng)更深,为此Google官方从android 3.0(对应API 11)引入Fragment,简单理解就是把界面分割成很多碎片,然后根据实际要求最后选择性的进行拼接,比如在手机竖屏模式下只能显示一本书的目录列表,但是如果是横屏模式(也可以立即为平板模式)下就可以在屏幕的左半边显示目录列表,然后在屏幕的右半边显示每个列表项目的简介,这样用户体验就会好很多。好了。下面开始实现一个简单的Fragment Demo

最后显示效果如下:(一个平板模式下左半边显示fragment1,右边显示fragment2)


1:分别创建fragment1和fragment2的布局

fragment1.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="vertical" >
    
    <TextView 
        android:text="这是fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#00CD00"/>
    

</LinearLayout>

fragment2.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="vertical" >
    
    <TextView 
        android:text="这是fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#EEEE00"/>
    

</LinearLayout>



2:这里仅仅是声明了静态界面,将来要引用的话还需要创建继承自Fragment的Activity(fragment1和fragment2都要创建)

Fragment1.class

package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment{
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	return inflater.inflate(R.layout.fragment, container,false) ;
}
	
}


<span style="font-size:18px;">
</span>

Fragment2.class

package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment2, container, false) ;
	}

}



3:到此基础的两个碎片都已经实现完毕,现在开始创建主界面局部

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="horizontal"
    android:baselineAligned="false"
    tools:context="com.example.fragment.MainActivity" >

   <fragment 
       android:id="@+id/fragment1"
       android:name="com.example.fragment.Fragment1"
       android:layout_height="wrap_content"
       android:layout_width="0dp"
       android:layout_weight="1"/>
   
    <fragment 
       android:id="@+id/fragment2"
       android:name="com.example.fragment.Fragment2"
       android:layout_height="wrap_content"
       android:layout_width="0dp"
       android:layout_weight="1"/>

</LinearLayout>

最后简单写一下主Activity启动项目
package com.example.fragment;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}


}


项目运行截图就不贴了。能正常运行。


看到这里,我们已经能够实现简单的静态界面了,但是Fragment最强大的地方却在动态加载上,我们在以上代码的基础上进行改造一下实现一个自动感应屏幕旋转,然后分别显示不同的Fragment的 Demo


同样先定义横屏竖屏的布局代码(因为在平板中调试的,为了演示效果,设置了300多的字体大小。。。。)

fragment1.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="vertical" >
    
    <TextView 
        android:text="这是竖屏"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="300sp"
        android:textColor="#00CD00"/>
    

</LinearLayout>


fragment2.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="vertical" >
    
    <TextView 
        android:text="这是横屏"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="300sp"
        android:textColor="#EEEE00"/>
    

</LinearLayout>

接下来当然是分别实现加载刚刚写好的fragment1和fragment2的布局

Fragment1.java

package com.example.dynamicfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment{
  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
	Bundle savedInstanceStat
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Fragment表现Activity中用户界面的一个行为或者是一部分。你可以在一个单独的activity上把多个fragment组合成为一个多区域的UI,并且可以在多个activity中再使用。你可以认为fragment是activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在activity运行时添加或者删除。 Fragment必须总是被嵌入到一个activity之中,并且fragment的生命周期直接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也被暂停,一旦activity被销毁,它里面所有的fragment也被销毁。然而,当activity正在运行时(处于resumed的生命周期状态),你可以单独的操控每个fragment,比如添加或者删除。当你执行这样一项事务时,可以将它添加到后台的一个栈中,这个栈由activity管理着——activity里面的每个后台栈内容实体是fragment发生过的一条事务记录。这个后台栈允许用户通过按BACK键回退一项fragment事务(往后导航)。 当你添加一个fragment作为某个activity布局的一部分时,它就存在于这个activity视图体系内部的ViewGroup之中,并且定义了它自己的视图布局。你可以通过在activity布局文件中声明fragment,用<fragment>元素把fragment插入到activity的布局中,或者是用应用程序源码将它添加到一个存在的ViewGroup中。然而,fragment并不是一个定要作为activity布局的一部分;fragment也可以为activity隐身工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值