Android攻城狮的第二门课(第1季)第8章 Fragment基础概述

Fragment主要用于大屏幕设备上(如:平板)。支持更加动态和灵活的UI设计。
一个Activity可以包含多个Fragment
Fragment在你的应用中应当是一个模块化和可重用的组件,应为Fragment的定义了它自己的布局,以及通过使用它自己的生命周期回调方法定义了它自己的行为,你可以将Fragment包含多个Activity中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

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

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

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

        <RadioButton
            android:id="@+id/fourth"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/radio_pressed"
            android:button="@null"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center_horizontal"
            android:text="传值通信" />
    </RadioGroup>

</RelativeLayout>
android的选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/gray" android:state_checked="true"></item>
    <item android:drawable="@color/white" android:state_pressed="true"></item>
    <item android:drawable="@color/white"></item>

</selector>

这里写图片描述

package com.example.android_fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener
{

    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.radiogroup);
        group.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub

        switch (checkedId) {
            case R.id.first: {  //实现Fragment的静态加载,跳转Activity
                Intent intent=new Intent(this,MainActivity2.class);
                startActivity(intent);
                break;

            }
            case R.id.second: {
                MyFragment2 fragment2=new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                beginTransaction.add(R.id.frame, fragment2);
                beginTransaction.addToBackStack(null);
                beginTransaction.commit();
                break;
            }
            case R.id.thrid: {
                Intent intent=new Intent(MainActivity.this,MainActivity3.class);
                startActivity(intent);

                break;
            }
            case R.id.fourth: {
                Intent intent=new Intent(MainActivity.this,MainActivity4.class);
                startActivity(intent);

                break;
            }
        }
    }
}

一、静态加载Fragment。
Activity 房子、宿主 Fragment 房间、寄生

Fragment中的android:name 属性指定了在layout中实例化的Fragment类。

MainActivity 包含多个Activity 每个Activity绑定一个XML文件, 每一个Activity的XML文件 包含多个Fragment

MainActivity-》MainActivity2-》MyFragment
activity_main->
Fragment对于Activity共享UI ID

            case R.id.first: {  //实现Fragment的静态加载,跳转Activity
                Intent intent=new Intent(this,MainActivity2.class);
                startActivity(intent);
                break;

            }
MainActivity2.java
package com.example.android_fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * Created by li806 on 2017/8/26.
 */

public class MainActivity2 extends Activity{
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        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 OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.setText("TextView改变了");
            }
        });

    }
}
main2.xml mainactivity的布局文件
静态绑定
<?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" >

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

</LinearLayout>
MyFragment
package com.example.android_fragment;

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

public class MyFragment extends Fragment{

    private String aaa;


    public String getAaa() {
        return aaa;
    }


    public void setAaa(String aaa) {
        this.aaa = aaa;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //layout布局文件转换成View对象
        /**
         * resource:Fragment需要加载的布局文件
         * root:加载layout的父ViewGroup
         * attactToRoot:false,不返回父ViewGroup
         */
        View view = inflater.inflate(R.layout.fragment, container, false);
        TextView text=(TextView) view.findViewById(R.id.text);
        Button button=(Button) view.findViewById(R.id.button);
        text.setText("静态加载Fragment"); //帮助寄生的Activity设置内容
        button.setText("获取内容");//帮助寄生的Activity设置内容
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String value = getAaa();
                Toast.makeText(getActivity(), "value="+value, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
fragment.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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

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

</LinearLayout>

这里写图片描述
这里写图片描述
二、动态加载Fragment

                MyFragment2 fragment2=new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();// fragment管理者
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                // 开启一个事务
                beginTransaction.add(R.id.frame, fragment2);//添加一个
                //参数一R.id.frame  要把fragment加载到哪   参数二:fragment对象
                beginTransaction.addToBackStack(null);//点击返回回到初始界面 (即前一个Fragment的状态)
                beginTransaction.commit();//事务提交
                break;
main.xml
    <LinearLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

三、Fragment的生命周期

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值