Android学习-Fragment(上)

Fragment基础详解
Android在3.0引入了Fragment的概念,主要目的是用在大屏幕设备上——例如平板电脑上,支持更加动态和灵活的ui设计。平板电脑的屏幕要比手机大得多,有更多的空间来访更多的UI组件,并且这些组件之间会产生更多的交互。

Fragment的设计哲学
Fragment在你的应用中应当是一个模块化和可重用的组件,因为Fragment定义了它自己的布局,以及通过使用它自己的生命周期回调方法定义了它自己的行为,你可以将Fragment包含到多个Activity中。

Fragment知识概要
1>Fragment可以作为Activity界面的一部分组成出现
2>可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用。
3>在Activity运行过程中,可以添加、移除或替换Fragment
4>Fragment可以响应自己的输入事件,并且有自己的生命周期,它们的生命周期会受宿主Activity的生命周期的影响。

onCreatView()方法
Fragment第一次绘制它的用户界面的时候,系统会调用此方法,为了绘制Fragment的UI,此方法必须返回一个View,如果不显示UI,返回null即可。

Fragment加载方式
1>静态加载
2>动态加载

静态加载
在Activity的layout文件中声明Fragment,需要特别注意的是中的amdroid:name属性制定了在layout中实例化的Fragment类
标识Fragment的方法
android:id属性提供一个唯一的id
android:tag属性提供一个唯一字符串

动态加载
撰写代码将Fragment添加到一个Activity layout中
add():添加一个Fragment(指定要添加的fragment和插入的View)
与此类似的还有remove()、替换();

处理Fragment事务
根据用户的交互情况,对Fragment进行添加、移除、替换,以及执行其他动作,提交给Activity的每一套变化简称一个事务
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beinTransaction();
每一个事务都是同时执行一套变化,可以在一个事务中设置你所有想执行的变化,包括add()|remove()、replace(),然后提交给Activity,必须调用commit()方法。
如果允许用户通过按下back键返回到前一个Fragment状态,调用commit()之前可以加入addToBackStack()方法

MainActivity.java
package com.example.angel.listviewpro;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;

public class MainAcitivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);
    }
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.first:
                Intent intent = new Intent(this,MainActivity2.class);
                startActivity(intent);
                Log.i("tag", "onCheckedChanged: 11111");
                break;
            case R.id.second:
                MyFragment2 fragment2 = new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //ragmentTransaction add(int var1, Fragment var2);
                //FragmentTransaction replace(int var1, Fragment var2);
                //FragmentTransaction remove(Fragment var1);
                beginTransaction.add(R.id.frame,fragment2);
                beginTransaction.addToBackStack(null);//可以回退
                beginTransaction.commit();//提交事务
                break;
            case R.id.third:

                break;
            case R.id.fourth:

                break;
        }
    }
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="bottom">

        <RadioButton
            android:id="@+id/first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:background="@drawable/orange_pressed"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="静态加载" />

        <RadioButton
            android:id="@+id/second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:background="@drawable/orange_pressed"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="动态加载" />

        <RadioButton
            android:id="@+id/third"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:background="@drawable/orange_pressed"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="生命周期" />

        <RadioButton
            android:id="@+id/fourth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:background="@drawable/orange_pressed"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="传值通信" />
    </RadioGroup>
</LinearLayout>
MainActivity2.java
package com.example.angel.listviewpro;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity{
    TextView tv;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Button button = (Button) findViewById(R.id.button);
        tv= (TextView) findViewById(R.id.text);
        button.setText("改变");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                tv.setText("TextView改变了");
            }
        });
    }
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.angel.listviewpro.MyFragment"/>
</LinearLayout>
MyFragment.java
package com.example.angel.listviewpro;
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{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       //layout文件转化为View对象
        /*
        * public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
        * 第一个参数:Fragment需要加载的布局文件
        * 第二个参数:加载layout的父ViewGroup
        * 第三`这里写代码片`个参数:false不返回
        * */
        View view = inflater.inflate(R.layout.fragment,container,false);
        TextView text = (TextView) view.findViewById(R.id.text);
        text.setText("静态加载");
        return view;
    }
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
MyFragment2.java
package com.example.angel.listviewpro;

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 MyFragment2 extends Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       //layout文件转化为View对象
        /*
        * public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
        * 第一个参数:Fragment需要加载的布局文件
        * 第二个参数:加载layout的父ViewGroup
        * 第三个参数:false不返回
        * */
        View view = inflater.inflate(R.layout.fragment,container,false);
        TextView text = (TextView) view.findViewById(R.id.text);
        text.setText("动态加载");
        return view;
    }
}

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值