android 学习随笔二十三(动画:Fragment )

  • Fragment
  • * 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容

    * 在一个Activity中切换多个界面,每个界面就是一个Fragment
    * Fragmnent的内容也是一个View对象


    * 生命周期方法跟Activity一致,可以理解把其为就是一个Activity
    * fragment切换时会销毁旧的,再创建新的
    * 定义布局文件作为Fragment的显示内容

    //此方法返回的View就会被显示在Fragment上
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //用布局文件填充成一个View对象,返回出去,那么就显示在Fragment上了
    View v = inflater.inflate(R.layout.fragment01, null);
    return v; 
    }
    * 把Fragment显示至指定ViewGroup中

    //把fragment显示至界面
    //new出fragment对象
    Fragment01 fg = new Fragment01();
    FragmentManager fm = getFragmentManager();
    //开启事务
    FragmentTransaction ft = fm.beginTransaction();
    //把fragment对象显示到指定资源id的组件里面
    ft.replace(R.id.fl, fg);
    ft.commit();

    package com.itheima.fragment;
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            showfagment01();
        }
    
        private void showfagment01() {
            //1.创建fragment对象
            Fragment01 fragment1 = new Fragment01();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment1);
            //5.提交
            ft.commit();
        }
        
        public void click1(View v){
            showfagment01();
        }
        public void click2(View v){
            //1.创建fragment对象
            Fragment02 fragment2 = new Fragment02();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment2);
            //5.提交
            ft.commit();
        }
        public void click3(View v){
            //1.创建fragment对象
            Fragment03 fragment3 = new Fragment03();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment3);
            //5.提交
            ft.commit();
        }
        
        
    }
    MainActivity
    package com.itheima.fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class Fragment01 extends Fragment {
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment01, null);
            return v;
        }
    }
    Fragment01
    package com.itheima.fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class Fragment02 extends Fragment {
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment02, null);
            return v;
        }
    }
    Fragment02
    package com.itheima.fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class Fragment03 extends Fragment {
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment03, null);
            return v;
        }
    }
    Fragment03
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itheima.fragment"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.itheima.fragment.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    AndroidManifest
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" 
        android:orientation="horizontal"
        >
    
        
        <FrameLayout 
            android:id="@+id/fl"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            ></FrameLayout>
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面1"
                android:onClick="click1"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面2"
                android:onClick="click2"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面3"
                android:onClick="click3"
                />
            </LinearLayout>
    </LinearLayout>
    activity_main
    <?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" 
        android:background="#00ff00"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="这是plus的帽子的绿色"
            />
    
    </LinearLayout>
    fragment01
    <?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" 
        android:background="#ff0000"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="这是热情的红色"
            />
    
    </LinearLayout>
    fragment02
    <?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" 
        android:background="#0000ff"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="这是plus戴上帽子的心情的蓝色"
            />
    
    </LinearLayout>
    fragment03

     

    • 生命周期

    * fragment切换时旧fragment对象会销毁,新的fragment对象会被创建

    •  低版本兼容

    * 在support-v4.jar包中有相关api,也就是说fragment可以在低版本模拟器运行

    package com.itheima.fragment;
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            showfagment01();
        }
    
        private void showfagment01() {
            //1.创建fragment对象
            Fragment01 fragment1 = new Fragment01();
            //2.获取fragment管理器
            FragmentManager fm = getSupportFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment1);
            //5.提交
            ft.commit();
        }
        
        public void click1(View v){
            showfagment01();
        }
        public void click2(View v){
            //1.创建fragment对象
            Fragment02 fragment2 = new Fragment02();
            //2.获取fragment管理器
            FragmentManager fm = getSupportFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment2);
            //5.提交
            ft.commit();
        }
        public void click3(View v){
            //1.创建fragment对象
            Fragment03 fragment3 = new Fragment03();
            //2.获取fragment管理器
            FragmentManager fm = getSupportFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment3);
            //5.提交
            ft.commit();
        }
        
        
    }
    View Code
    package com.itheima.fragment;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class Fragment01 extends Fragment {
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment01, null);
            return v;
        }
    }
    Fragment01

     

    • Fragment和Activity传递数据
    package com.itheima.transmit;
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private Fragment01 fragment1;
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            showfagment01();
        }
    
        private void showfagment01() {
            //1.创建fragment对象
            fragment1 = new Fragment01();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment1);
            //5.提交
            ft.commit();
        }
        
        public void click1(View v){
            showfagment01();
        }
        public void click2(View v){
            //1.创建fragment对象
            Fragment02 fragment2 = new Fragment02();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment2);
            //5.提交
            ft.commit();
        }
        public void click3(View v){
            //1.创建fragment对象
            Fragment03 fragment3 = new Fragment03();
            //2.获取fragment管理器
            FragmentManager fm = getFragmentManager();
            //3.开启事务
            FragmentTransaction ft = fm.beginTransaction();
            //4.显示fragment
            //arg0:设置把fragment显示在哪个容器中
            ft.replace(R.id.fl, fragment3);
            //5.提交
            ft.commit();
        }
        
        public void click4(View v){
            EditText et_main = (EditText) findViewById(R.id.et_main);
            String text = et_main.getText().toString();
            fragment1.setText(text);
        }
        
        public void setText(String text){
            TextView tv_main = (TextView) findViewById(R.id.tv_main);
            tv_main.setText(text);
        }
        
    }
    MainActivity
    package com.itheima.transmit;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebView.FindListener;
    import android.widget.TextView;
    
    public class Fragment01 extends Fragment {
    
        private TextView tv;
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment01, null);
            tv = (TextView) v.findViewById(R.id.tv_fragment1);
            return v;
        }
        
        public void setText(String text){
            tv.setText(text);
        }
    }
    Fragment01
    package com.itheima.transmit;
    
    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.EditText;
    
    public class Fragment03 extends Fragment implements OnClickListener {
    
        private EditText et;
    
        //系统自动调用,返回的View对象作为Fragment的内容显示
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment03, null);
            Button bt_fragment03 = (Button) v.findViewById(R.id.bt_fragment03);
            et = (EditText) v.findViewById(R.id.et_fragment03);
            bt_fragment03.setOnClickListener(this);
            return v;
        }
    
        @Override
        public void onClick(View v) {
            String text = et.getText().toString();
            ((MainActivity)getActivity()).setText(text);
        }
        
        
    }
    Fragment03
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" 
        android:orientation="horizontal"
        >
    
        
        <FrameLayout 
            android:id="@+id/fl"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            ></FrameLayout>
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面1"
                android:onClick="click1"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面2"
                android:onClick="click2"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="界面3"
                android:onClick="click3"
                />
            <EditText 
                android:id="@+id/et_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="提交"
                android:onClick="click4"
                />
            <TextView 
                android:id="@+id/tv_main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            </LinearLayout>
    </LinearLayout>
    activity_main
    <?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" 
        android:background="#00ff00"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="这是plus的帽子的绿色"
            />
        <TextView 
            android:id="@+id/tv_fragment1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    fragment01
    <?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" 
        android:background="#0000ff"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="这是plus戴上帽子的心情的蓝色"
            />
        <EditText 
            android:id="@+id/et_fragment03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        <Button 
            android:id="@+id/bt_fragment03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    </LinearLayout>
    fragment03

     

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的海滨体育馆管理系统,源码+数据库+毕业论文+视频演示 本基于Spring Boot的海滨体育馆管理系统设计目标是实现海滨体育馆的信息化管理,提高管理效率,使得海滨体育馆管理工作规范化、高效化。 本文重点阐述了海滨体育馆管理系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MySQL作为系统数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了海滨体育馆相关信息管理的重要功能。 本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高海滨体育馆管理效率。 关键词:海滨体育馆管理,Java技术,MySQL数据库,Spring Boot框架 本基于Spring Boot的海滨体育馆管理系统主要实现了管理员功能模块和学生功能模块两大部分,这两大功能模块分别实现的功能如下: (1)管理员功能模块 管理员登录后可对系统进行全面管理操作,包括个人中心、学生管理、器材管理、器材借出管理、器材归还管理、器材分类管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理、灯光保修管理、体育论坛以及系统管理。 (2)学生功能模块 学生在系统前台可查看系统信息,包括首页、器材、体育论坛以及体育资讯等,没有账号的学生可进行注册操作,注册登录后主要功能模块包括个人中心、器材管理、器材借出管理、器材归还管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值