fragment


--Android在3.0中引入的主要是用在大屏幕设备上
--平板比手机大可以放更多的 Ui,组件之间会产生更多的交互
1、fragment可以作为Activity的一部分出现
2、一个Activity中可以包含多个fragment且一个Fragment也可以在多个Activity中出现
3、在Activity运行中可以添加、移除、替换Fragment
4、Fragment可以响应自己的输入事件,有自己的生命周期(并且受宿主影响)
--先创建一个子类

加载方式:静态加载、动态加载
-静态加载:在layout文件中声明Fragment
<fragment
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.android_fragment.MyFragment"
        />
在《fragment》name属性指定layout中实例化的Fragment类
标示fragment的方法:提供一个唯一字符串(id或者tag);

动态加载:
---要动态加载就要完成一个事务:
---getFragmentManager().beginTransaction().
---add(这个参数是标识符(id), fragment2).commit();
注:根据包不同得到Mannager对象方法不一样
事务:3种add()、reovme()、rep...()
---如果允许用户通过按下BACK按键返回到前一个Fragment,
---在调用commit()前加入addToBackStack();


生命周期:Fragment被添加到Activity时调用方法:onAttach()
生命周期:
1.onAttach();当Fragment被添加到Activity时候会回调这个方法,并且只调用一次
2.onCreat();创建Fragment时被回调,并且只调用一次
3.onCreatView();每次创建都会绘制Fragment的View组件时调用该方法
4.onActivityCreated();当Fragment所在的Activity启动完成后调用这个方法
5.onStart();恢复Fragment时会被回调,
--调用onStart()方法后面一定会调用onResume()方法
6.onPause();暂停Fragment
7.onStop();停止Fragment
8.onDestroyView();销毁Fragment所包含的View组件
9.onDestroy();销毁Fragment回调
10.onDetach();Fragment从Activity删除时回调

这里我写了一个Activity两个Fragment一个静态添加Activity一个动态添加Activity:

package zxx.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.textView1);
		MyFragment mfragment=(MyFragment)getSupportFragmentManager().findFragmentById(R.layout.fragment);

		Button but=(Button) findViewById(R.id.add_fragment);
		
	}
	public void listenner(View  v ){
		getSupportFragmentManager().beginTransaction().
		add(R.id.LinearLayout1, new Fragmnet2()).commit();
		Log.i("******", "dianji ");
	}
}

这里Activity所需要的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="zxx.fragment.MainActivity" >

    <Button
        android:id="@+id/add_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="listenner"
        android:text="添加一个Fragment" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment的动/静加载" />

    <fragment
        android:id="@+id/fragment"
        android:name="zxx.fragment.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这里Fragment我只实现了view方法:

public class MyFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment, null, false);
		return v;
	}
}
 <pre class="html" name="code"><?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一个静态fragment" />

</LinearLayout>

 

 

public class Fragmnet2 extends Fragment {
	public Fragmnet2() {
		super();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View v=inflater.inflate(R.layout.fragment2, null);
		Log.i("******", "fragmrn执行了 ");
		return v;
	}
}

 

 

 

 

 

 

 

 


<?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="wrap_content"
    android:orientation="vertical" >
    
<TextView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="这是一个动态fragment"/>
</LinearLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值