android 63 Fragment

简介:

#Fragment 是3.0平板才引入进来的,3.0之后就加入了Fragment。原来是一个屏幕就是一个Activity,
>片段,碎片

复制代码
1. 定义某一个片段的界面 继承Fragment类
        public class BlueToothFragment extends Fragment {}
2. 重写Fragment里面的方法
        显示Fragment的ui,把布局文件转化成view对象
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.blue_tooth_layout, null);
        }
3. 获取Fragment管理器
        fm = getFragmentManager();
4. 动态的修改界面
        f1 = new BlueToothFragment();//先把要显示的f1new出来
        FragmentTransaction ft = fm.beginTransaction();//开启事务
        ft.replace(R.id.fl_container, f1);
        ft.commit();//保证了 要么同时成功,要么同时失败
复制代码

主页面:

 

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"       垂直排列
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="0dip" >
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#22000000"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting01"
            android:text="蓝牙设置" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting02"
            android:text="声音设置" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting03"
            android:text="显示设置" />
    </LinearLayout>

</LinearLayout>
复制代码

3个fragment:

复制代码
蓝牙fragment:

<?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="我是蓝牙设置"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

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

</LinearLayout>

显示fragment:

<?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="我是显示设置"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

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

</LinearLayout>

声音fragment:

<?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="我是声音设置"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

</LinearLayout>
复制代码

主Activity:

复制代码
package com.itheima.fragementdemo;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    private FragmentManager fm;//管理所有Fragment,更新页面的时候要么同时更新成功要么同时更新失败所以有事物,
    BlueToothFragment f1;
    ShowFragment f3;
    SoundFragment f2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getFragmentManager();
        initFragment();
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f1);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }

    private void initFragment() {
        f1 = new BlueToothFragment();//Fragment是直接new出来的
        f2 = new SoundFragment();
        f3 = new ShowFragment();
    }

    //蓝牙点击事件
    public void setting01(View view) {
        //事物开始的时候实例化ft,事物结束的时候提交事物,提交后事物就过期了,所以每次都要初始化ft
        FragmentTransaction ft = fm.beginTransaction();//
        ft.replace(R.id.fl_container, f1);
        ft.commit();//保证了 要么同时成功,要么同时失败,保证了页面不花屏。
    }
    //声音点击事件
    public void setting02(View view) {
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f2);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }
    //显示点击事件
    public void setting03(View view) {
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f3);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }

}
复制代码

3个fragment java代码

复制代码
蓝牙:

package com.itheima.fragementdemo;

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

public class BlueToothFragment extends Fragment {
    //显示Fragment的ui的,不需要在清单文件配置任何内容
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.blue_tooth_layout, null);
    }
}


声音

package com.itheima.fragementdemo;

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

public class SoundFragment extends Fragment {
    //显示Fragment的ui的
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sound_layout, null);
    }
}


显示

package com.itheima.fragementdemo;

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

public class ShowFragment extends Fragment {
    //显示Fragment的ui的
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.show_layout, null);
    }
}
复制代码

 

##Fragment的向下兼容,兼容低版本
>使用support-v4的jar包
>1. MainActivity extends FragmentActivity
>2. 所有的fragment的导包, android.support.v4.app.Fragment
>3. getSupportFragmentManager();
>4. 注意两个导包
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

复制代码
package com.itheima.fragementdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class MainActivity extends FragmentActivity {
    private FragmentManager fm;
    BlueToothFragment f1;
    ShowFragment f3;
    SoundFragment f2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //为了向下版本兼容采用v4包里面的FragmentManager
        fm = getSupportFragmentManager();
        initFragment();
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f1);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }

    private void initFragment() {
        f1 = new BlueToothFragment();
        f2 = new SoundFragment();
        f3 = new ShowFragment();
    }

    //蓝牙
    public void setting01(View view) {
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f1);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }
    //声音
    public void setting02(View view) {
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f2);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }
    //显示
    public void setting03(View view) {
        //事务的概念
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_container, f3);
        ft.commit();//保证了 要么同时成功,要么同时失败
    }

}
复制代码

实现效果:



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4916082.html,如需转载请自行联系原作者


相关文章
|
6月前
|
XML Android开发 数据格式
Android -- Fragment动态注册
Android -- Fragment动态注册
31 0
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
292 54
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习Fragment
android开发,使用kotlin学习Fragment
48 0
|
4月前
|
XML Java Android开发
Android Studio App开发之碎片Fragment的讲解及实战(附源码 包括静态和动态注册)
Android Studio App开发之碎片Fragment的讲解及实战(附源码 包括静态和动态注册)
44 1
|
8月前
|
Android开发
Android ViewModel+LiveData实现Fragment间通信详解
Android ViewModel+LiveData实现Fragment间通信详解
101 0
|
8月前
|
Android开发
Android 中Activity和Fragment生命周期的具体变化详解
Android 中Activity和Fragment生命周期的具体变化详解
92 0
|
8月前
|
Android开发
Android 中Fragment和Activity之间的通信
Android 中Fragment和Activity之间的通信
42 0
|
8月前
|
Android开发
Android 中使用RadioGroup+Fragment实现底部导航栏的功能
Android 中使用RadioGroup+Fragment实现底部导航栏的功能
75 0
|
9月前
|
Android开发 容器
Android上机实验-6 Fragment的使用
Android上机实验-6 Fragment的使用
67 1
|
10月前
|
Java Android开发 容器
Android实战开发--小慕笔记UI设计(Fragment布局的使用)
Android实战开发--小慕笔记UI设计(Fragment布局的使用)
Android实战开发--小慕笔记UI设计(Fragment布局的使用)