您的位置:首页 > 移动开发 > Android开发

Android Fragment用法实例说明

2014-11-21 21:54 344 查看
1、Fragment简介

Fragment是Android3.0引入一个类。因为当Android的世界成员越来越多,种类也变得更加丰富。其中最具特征的就是屏幕的尺寸千变万化。这就给Android开发者带来很多的痛苦。谷歌在2011年02月03日发布了专用于平板电脑的Android 3.0 Honeycomb系统,它带来了很多激动人心的新特性。这是首个基于Android的平板电脑专用操作系统。

为了使的在手机上开发的应用能够顺利移植到平板的大屏幕中,如果重新开发将会耗费大量人力物力,因此出了一个fragment。它使得Android程序布局在小屏幕手机或者大屏幕平板上都能游刃有余。



有很多代表性的应用都具有这样的特性。面试的时候,很多Android面试官也总喜欢询问有关Fragment的问题。因此有必要将学习的记录下来。

2、Fragment的生命周期

由于Fragment是在Activity中嵌套的,因此他的生命周期与Activity的生命周期密切相关。下图是官网中Fragment的生命周期:



下图是精通Android4中介绍的:



3、Fragment的开发使用

第一步:建立Fragment的UI界面

1⃣️新建一个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" ]]>
    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher"
        />
</LinearLayout>
2⃣️新建一个继承Fragment的子类



package com.scy.fragmenttest2;

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

/**
 * 定义Fragment视图
 * @author songchunyu
 *
 */
public class MyFragment extends Fragment {
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
     }
     
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
          // 定义Fragment的View
          View myView = inflater.inflate(R.layout.fragment_view,container, false);
          return myView;
     }
}
第二步:将Fragment嵌套进Activity中

将Fragment嵌套进Activity中有两种加载方式:

1⃣️通过xml布局文件

2⃣️通过FragmentManager对Fragment进行实时管理

在我的demo中,我将两种类型融合在一起了:

新建一个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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.scy.fragmenttest2.FragmentTest"
    android:orientation="horizontal"
     >

    <Button 
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:text="隐藏Fragment"
        />
   <!-- android:id="@+id/fragment"   --> 
   <!-- 第一种方式,使用xml布局 -->
     <fragment android:name="com.scy.fragmenttest2.MyFragment"  
            android:tag="test_scy" 
            android:layout_weight="1"  
            android:layout_width="0dp"  
            android:layout_height="match_parent" 
            android:visibility="invisible"  
            /> 
     <!-- 第二种方式 使用编程,将fragment实例add进一个ViewGroup,这里是LinearLayout-->    
     <LinearLayout 
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/test"
    android:layout_weight="1" 
         ></LinearLayout]]>

</LinearLayout>
在Activity中,进行两种方式的调用:

package com.scy.fragmenttest2;

import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FragmentTest extends ActionBarActivity {
     
     private FragmentManager fManager = null;
    private Fragment fragment = null;
    private Fragment fragment2 = null;
     private Button button;
     private FragmentTransaction ft;
     
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);
        
        // 1⃣️通过id获取fragment,同样也可以通过tag
        fManager = getFragmentManager();
//        final Fragment fragment = fManager.findFragmentById(R.id.fragment);
        fragment = fManager.findFragmentByTag("test_scy");
//        fragment.setMenuVisibility(false);
        
        // 2⃣️通过编程方式
        fragment2 = new MyFragment();
        // 注意这里需要每次事件重新建立
//        ft = fManager.beginTransaction();
//        ft.add(R.id.test, fragment2);
//        ft.hide(fragment);
        button = (Button)findViewById(R.id.btn_show);
        button.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                    // 每次编辑事件都需要新建一个这样的事件
                    ft = fManager.beginTransaction();   
                //为Fragment设置淡入淡出效果    
                ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);    
                // 2⃣️通过另外一种方式添加Fragment
                ft.add(R.id.test, fragment2);
//                ft.hide(fragment);
                if (fragment.isHidden()) {    
                    ft.show(fragment);    
                     
                    button.setText("隐藏Fragment");    
                } else {    
                    ft.hide(fragment);    
                    button.setText("显示Fragment");    
                }    
                ft.commit();    
               }
          });
    }
}


最后效果:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: