`

android3.0之Fragment(碎片)基础

阅读更多
HoneyComb3.0组件运用可以看这里:
http://blog.csdn.net/mayingcai1987/article/category/786494

了解Fragment生命周期看这里:
http://www.open-open.com/lib/view/open1421734804296.html

http://www.android123.com.cn/androidkaifa/772.html
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面Android123介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.

  一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。

1,先定义2个Fragment,布局文件R.layout.first&R.layout.second根据自己需求随便写一个,我这里就不贴代码了。
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;

public class FirstFragment extends Fragment{

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                             Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.first, container, false);
		registerForContextMenu(root.findViewById(R.id.editText1));
		return root; 
    } 
	
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, 0, Menu.NONE, "菜单1");
        menu.add(Menu.NONE, 1, Menu.NONE, "菜单2");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item);
    }
	
}

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

public class SecondFragment extends Fragment{

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


2,在Activity中使用
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Honeycomb extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//        FirstFragment firstFragment=new FirstFragment();
//        //在Activity中通过这个与Fragment通讯
//        getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit();
        
        FragmentManager fm = getFragmentManager();
        addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment));
        addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment));
        
    }
    
    void addShowHideListener(int buttonId, final Fragment fragment) {
        final Button button = (Button)findViewById(buttonId);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                //为Fragment设置淡入淡出效果
                ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
                        
                if (fragment.isHidden()) {
                    ft.show(fragment);
                    button.setText("隐藏");
                } else {
                    ft.hide(fragment);
                    button.setText("显示");
                }
                ft.commit();
            }
        });
    }
    
}

3,布局R.layout.main中引用碎片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    >
   
     <fragment android:name="com.ql.app.FirstFragment" 
            android:id="@+id/firstFragment" 
            android:layout_weight="1" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            /> 
    <fragment android:name="com.ql.app.SecondFragment" 
            android:id="@+id/secondFragment" 
            android:layout_weight="2" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            />
            
     <Button android:id="@+id/btn_1"
      	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content"
    	android:text="隐藏"
     /> 
     <Button android:id="@+id/btn_2"
      	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content"
    	android:text="隐藏"
     /> 
</LinearLayout>

4,上图



http://blog.csdn.net/nkmnkm/article/details/7256605

  • 大小: 24.1 KB
分享到:
评论

相关推荐

    Android使用Fragment实现标签页

    Fragment的概念是从Android3.0开始引入的,直译为碎片、片段,目的是为不同屏幕大小的设备(手机、平板等)创建灵活动态的UI。诚如其名,你可以把Fragment当作是Activity的模块化组件,它拥有自己的生命周期和UI,接受...

    Fragment碎片技术

    android3.0以上常用的碎片技术fragment,分享大家学习中

    Android Fragment 完全解析

    为了让界面可以在平板上更好地展示,Android 在3.0版本引入了Fragment(碎片)功能,它非常类似于 Activity,可以像Activity 一样包含布局。Fragment 通常是嵌套在Activity 中使用的,现在想象这种场景: 有两个...

    Fragment的传值问题

    Fragment,碎片,是Android 3.0之后加入的一个非常重要的概念。每个Fragment都有相应的Activity对它进行托管。一个Activity中可以有多个Fragment,这很自然的给大屏幕的适配提供了很便捷的方案。现在大家在开发中都必...

    Android Fragment(动态,静态)碎片详解及总结

     Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户界面.使用多个Fragment可以在一个单独的Activity中建 立多个UI面板,也可以在多个Activity中...

    Fragment+ActionBar

    Fragment和ActionBar都是Android3.0之后出现的,Fragment,碎片,主要是为了支持更多的UI设计在大屏幕设备上,如平板。因为现在设备的屏幕越来越大,使用Fragment可以更灵活的管理视图层次的变化。像Activity一样,...

    Fragment演示

    Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像

    Android程序技术:Fragment的介绍.pptx

    Fragment(碎片)是一种可以嵌入在Activity中的UI片段,与Activity非常相似,不仅包含布局,同时也具有自己的生命周期 Fragment不能独立存在必须嵌入到Activity中使用,所以Fragment生命周期直接受所在的Activity...

    Fragment实例

    android 3.0版本提供的 新技术 碎片 ,本程序提供 fragment 一般的使用方法

    Android Fragment的用法实例详解

    Fragment是Android 3.0的时候被引入的,主要目的是为了给大屏幕(如平板电脑)添加动态和灵活的UI支持。利用Fragment实现更好的用户体验。 Fragment加载 1.静态加载:添加Fragment到Activity布局中,以xml的形式。 2....

    深入浅析 Android Fragment(上篇)

    为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,...

    Fragment与Activity简单使用,包括二者之间的关联与生命周期

    在Android3.0之后,谷歌推出了Fragment,Fragment在Android中被称为碎片。 我们可以把Fragment看作是Activity的一个界面或者组成部分,而且Fragment具有与Activity很相似的生命周期,我们可以在Activity中动态的...

    Android 使用Fragment模仿微信界面的实例代码

     自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其目的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件...

    Android应用开发中使用Fragment的入门学习教程

    Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多...

    Android中你可能不知道的Fragment妙用

    自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。下面这篇文章主要给大家分享了关于Android中你可能不知道的Fragment妙用,对大家具有一定的参考学习价值,需要的朋友可以参考下。

    【推荐】超全的移动安全自学资料精编合集(43份).zip

    Android安全开发基础: 图形界面(UI)和碎片(Fragment)(上) Android安全开发基础: 图形界面(UI)和碎片(Fragment)(下) Android安全开发基础: 持久化技术 Android安全开发基础: 多媒体 Android安全开发基础:...

    移动安全系列教学下载共43份.zip

    Android安全开发基础--10--图形界面(UI)和碎片(Fragment)(上).pdf Android安全开发基础--11--图形界面(UI)和碎片(Fragment)(下).pdf Android安全开发基础--12--持久化技术.pdf Android安全开发基础--13-...

    【安卓移动应用】主页面中嵌入Fragment

    Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,...

    精通ANDROID 3(中文版)1/2

    1.5.3 Android基础组件  1.5.4 高级UI概念  1.5.5 Android Service组件  1.5.6 Android媒体和电话组件  1.5.7 Android Java包  1.6 利用Android源代码  1.7 本书的示例项目  1.8 小结  第2章 设置...

Global site tag (gtag.js) - Google Analytics