Android-Fragment(基本知识,静态加载,动态加载)

1.回顾

   上篇了解了和学习了 Andorid 的webView 的使用

2.重点

   (1)Fragment基本知识

   (2)Fragment 静态加载

   (3)Fragment 动态加载

   (4)Fragment的生命周期

   (5)Fragment与Activity 之间相互通信(传值)


3.Fragment基本知识

  (1)可以作为Activity一部分
  (2)可以在Activity 出现多个Fragment
  (3)可以在多个Activity 中出现一个Fragment
 
 (4)Fragment在Activity 运行过程中,可以添加,移除和替换
 (5)Fragment可以响应自己的输入事件
 (6)Fragment的独立的生命周期,受宿主Acitivity的生命周期影响

 (7)独立的组件控制

 (8)独立的用户输入响应


4.Fragment静态 加载

   4.1 新建 layout 布局文件 fragmen.xml

        包括一个按钮和一个 textview

<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/btn_frag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变" />

</LinearLayout>

    4.2 新建 MyFragment

          继承自 Fragment  ,实现 onCreateView() 方法,其中的 View对象 中需要的布局文件为 ,上面新建的fragment.xml 布局文件

package com.example.fragment;

import com.example.studydemo.R;

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



public class MyFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// layout布局文件 转成view 对象
		/**
		 * resource 布局文件
		 * root 加载layout 的父 ViewGroup
		 * attachToRoot :false 不反悔
		 */
		View view=inflater.inflate(R.layout.fragment, container, false);
		TextView textView1=(TextView) view.findViewById(R.id.textView1);
		textView1.setText("静态加载Fragment");
		return view;
	}
}


 

   4.3 新建Activity 

          布局文件中新添加 fragment 标签,引入刚刚新建的 MyFragment 实现类 ; 一定要给 fragment 添加id

  <fragment
       android:id="@+id/fragment_2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:name="com.example.fragment.MyFragment"
       >
   </fragment>

   4.4 测试 

        在 新建的Activity 类中 ,实例话 fragment中的 控件 按钮和 textview ;

         btn_frag=(Button) findViewById(R.id.btn_frag);
		tv1=(TextView) findViewById(R.id.textView1);
		btn_frag.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击事件
				tv1.setText("我是Fragment2Activity");
			}
		});


    4.5 运行测试



5. Fragment 动态加载

   5.1 新建 Activity 

    在布局文件中 添加一个 按钮和 一个 布局文件

 <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/button3"
        android:text="动态加载Fragment" />

    <LinearLayout
        android:id="@+id/fragme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="42dp"
        android:orientation="vertical" >
    </LinearLayout>


   5.2  事件

    初始化按钮,和在按钮点击事件里添加,动态加载代码:

  例如:

     (1)MyFragment 和静态加载一样 实现 Fragment ,

     (2)transaction.add()方法中 的 R.id.fragme 是 上面 lineatLayout 的 id

     (3)最后一定 commit

                  MyFragment2 fragment2=new MyFragment2();
                  FragmentManager fragmentManager= getFragmentManager();
		  FragmentTransaction transaction=fragmentManager.beginTransaction();
		  transaction.add(R.id.fragme, fragment2);
		  
		  //回退
		  transaction.addToBackStack(null);
		  
		  transaction.commit();

6.总结

    动态加载的时候,FragmentTransaction 有 多个操作 Fragment操作方法,比如 add() , replace() ,remove()等

 下面将学习 Fragment的 生命周期和 与 Activity 相互通信(传参)

   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值