Android Fragment

Fragment顾名思义:片段

它是依附于Activity中的片段,就像iframe依附于HTML一样

类似于Actitity

但Fragment不需在andorid清单中列出,相当于轻量级Activity


其生命周期也很像Activity:


要写一个fragment就要继承Fragment,Fragment只能在SDK11 - Android 4.0 以上运行。不过为了向下兼容,也可以继承v4包的Fragment..getSupportedFragmentManager...

每当一个fragment创建时就调用onCreateView..而且它提供了inflater为我们的XML打气

Fragment1.java

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{
	
	/**
	 * 当fragment被创建的时候调用,返回当前fragment显示内容
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment1, null);
	}
	
}
Fragment1的XML布局文件

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:background="#0000FF"
    android:orientation="vertical" >
    

</LinearLayout>


在Activity中添加Fragment有两种方法 ,一种是静态添加,一种是动态添加

静态添加fragment到主Activity布局文件中:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fragment.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fg1"
        android:name="com.example.fragment.Fragment1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </fragment>

    <fragment
        android:id="@+id/fg2"
        android:name="com.example.fragment.Fragment2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </fragment>

</LinearLayout>


name项必须填写...填写fragment跟activity类似

动态添加Fragment

package com.example.fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {
	
	/**
	 * 动态的创建fragment;
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		int width = getWindowManager().getDefaultDisplay().getWidth();
		int height = getWindowManager().getDefaultDisplay().getHeight();
		
		//动态的添加fragment
		Fragment1 fragment1 = new Fragment1();	
		Fragment2 fragment2 = new Fragment2();
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		//垂直
		if(height > width)
		{
			//android.R.id.content返回当前Activity的id,也可以在xml中定义layout的id
			ft.replace(android.R.id.content, fragment1);
		}else
		{
			ft.replace(android.R.id.content, fragment2);
		}
		ft.commit();
	}

}


--------------------------------------------------------------Fragment实现选项卡功能-----------------------------------------------------------

下面的代码每次点击一个TAB都new 出了一个Fragment.其实应该将Fragment1,Fragment2,Fragment3,Fragment4提升成成员变量供下面访问节约内存

package com.example.tabfragment;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener {
	private FragmentManager fragmentManager;
	FragmentTransaction transaction;
	TextView tv1, tv2, tv3, tv4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fragmentManager = getFragmentManager();
		transaction = fragmentManager.beginTransaction();
		tv1 = (TextView) findViewById(R.id.tv_tab1);
		tv2 = (TextView) findViewById(R.id.tv_tab2);
		tv3 = (TextView) findViewById(R.id.tv_tab3);
		tv4 = (TextView) findViewById(R.id.tv_tab4);
		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		tv3.setOnClickListener(this);
		tv4.setOnClickListener(this);
		transaction.replace(R.id.ll_content, new Fragment1()).commit();
	}

	@Override
	public void onClick(View v) {
		transaction = fragmentManager.beginTransaction();
		switch (v.getId()) {
		case R.id.tv_tab1:
			transaction.replace(R.id.ll_content, new Fragment1());
			break;
		case R.id.tv_tab2:
			transaction.replace(R.id.ll_content, new Fragment2());
			break;
		case R.id.tv_tab3:
			transaction.replace(R.id.ll_content, new Fragment3());
			break;
		case R.id.tv_tab4:
			transaction.replace(R.id.ll_content, new Fragment4());
			break;
		default:
			break;
		}
		transaction.commit();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值