android Fragment的重点:

  1. 3.0版本后引入,即minSdk要大于11
  2. Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fragment也是需要嵌套在Activity中的,间接地说,Fragment还是需要嵌套在Activity中!!受寄主Activity的生命周期影响,当然他也有自己的生命周期!另外不建议在Fragment里面嵌套Fragment因为嵌套在里面的Fragment生命周期不可控!!!

    官方文档说创建Fragment时至少需要实现三个方法:onCreate( ),onCreateView( ),OnPause( );不过貌似只写一个onCreateView也是可以的...
  3. 3.Fragment的生命周期和Activity有点类似:三种状态:

    Resumed:在允许中的Fragment可见

    Paused:所在Activity可见,但是得不到焦点

    Stoped:①调用addToBackStack(),Fragment被添加到Bcak栈②该Activity转向后台,或者该Fragment被替换/删除

    ps:停止状态的fragment仍然活着(所有状态和成员信息被系统保持着),然而,它对用户不再可见,并且如果activity被干掉,他也会被干掉.

Fragment静态加载

主要步骤:

  1. 定义Fragment的布局文件Fragment1.axml,Fragment里面的所有控件都可以写在里面.
  2. 定义Fragment类,继承Fragment类或其子类,必须重写OnCreateView方法
  3. 在加载Fragment的Activity的布局文件里<fragment> 元素里面 name属性写上完全限定名.Fragement类名
  4. 必须得注意在加载Fragment的Activity的布局文件中fragment中Id必须写,不写会报错

新建一个Fragment的布局文件fragment1.axml:

<?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"
android:background="#ff0000">
<TextView
android:id="@+id/tv_test"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="静态的Fangment1"
android:textColor="#ffffff"
android:textSize="30dp"
android:gravity="center_vertical|center" />
</LinearLayout>

自定义继承Fragment的Class,重写OnCreateView方法

using System;
using Android.App;
using Android.OS;
using Android.Views; namespace Static_Fragment_Demo.Fragments
{
public class FragmentStatic1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.Inflate(Resource.Layout.Fragment_Static_layout1, container, false);
return v;
}
}
}

加载Fragment的Activity的布局文件Main.axml,记住name的值不要写错了,Id必须写

<?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">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fragment1" />
<fragment
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/fragment_static"
android:name="Static_Fragment_Demo.Fragments.FragmentStatic" />
<fragment
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/fragment_static1"
android:name="Static_Fragment_Demo.Fragments.FragmentStatic1" />
</LinearLayout>

代码非常简单,但是也要细心,尤其是name属性值不要写错了。fragment静态加载的简单用法,代码示例下载:xamarin android fragment静态加载例子

Fragment动态加载

实现流程:

  1. 获得FragmentMangger对象,直接通过FragmentManager属性获取
  2. 获取FragmentTransaction对象fm.BeginTransaction();
  3. 调用Add方法或者replace方法加载Fragment

示例代码:演示的是横竖屏切换Fragment的效果

Fragment的布局和Class代码就用上面的吧,稍微改一改就可以了

直接贴出MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android;
using Static_Fragment_Demo.Fragments;
namespace Static_Fragment_Demo
{
[Activity(Label = "Static_Fragment_Demo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Display dis = WindowManager.DefaultDisplay;
if (dis.Width > dis.Height)
{
Fragment1 f1 = new Fragment1();
FragmentManager.BeginTransaction().Replace(Resource.Id.linearLayout1, f1).Commit();
}
else
{
Fragment2 f2 = new Fragment2();
FragmentManager.BeginTransaction().Replace(Resource.Id.linearLayout1, f2).Commit();
}
}
}
}

Activity和Fragment组件的获取:

Fragment获取Activity中的组件Activity.FindViewById(Resource.Id.tv_test)

Activity获取Fragment中的组件:FragmentManager.FindFragmentById(Resource.Id.fragment);

感觉还有好多重要的没有说完,下次吧,Fragment的用处非常常见也很重要! 关于xamarin android中使用Fragment实现底部导航栏可以看看这个 xamarin android Fragment实现底部导航栏

作者:张林

标题:Xamarin  Android Fragment的两种加载方式 原文地址:http://blog.csdn.net/kebi007/article/details/52740285

转载随意注明出处

Xamarin Android Fragment的两种加载方式的更多相关文章

  1. Linux共享库两种加载方式简述

      Linux共享库两种加载方式简述  动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...

  2. ios 图片的两种加载方式

    控件加载图片,plist,懒加载,序列帧动画,添加动画效果. IOS中有2种加载图片的方式. 方式一:有缓存(图片所占用的内存会一直停留在程序中) + (UIImage *)imageNamed:(N ...

  3. 渐进式jpeg(progressive jpeg)图片及其相关 --图片的两种加载方式

    渐进式jpeg(progressive jpeg)图片及其相关   一.基本JPEG(baseline jpeg)和渐进JPEG 网络上那些色色的照片都是.jpg格式的("色色"指 ...

  4. Linux驱动的两种加载方式过程分析

    一.概念简述 在Linux下可以通过两种方式加载驱动程序:静态加载和动态加载. 静态加载就是把驱动程序直接编译进内核,系统启动后可以直接调用.静态加载的缺点是调试起来比较麻烦,每次修改一个地方都要重新 ...

  5. dll的两种加载方式(pend)+ delayload

    看过关于动态库的调用例子,于是决定动手做一做:dll的对外接口声明头文件,Mydll.h: //Mydll.h #include <stdio.h> #include <stdlib ...

  6. Qml文件的两种加载方式

    一种是QQmlApplicationEngine搭配Window,例如: #include <QGuiApplication> #include <QQmlApplicationEn ...

  7. Android Activity四种加载方式

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

  8. Android学习笔记_50_(转 四种加载方式详解(standard singleTop singleTask singleInstance)

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

  9. Android 四种加载方式详解(standard singleTop singleTask singleInstance) .

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

随机推荐

  1. 开源项目 log4android 使用方式详解

    话不多说, 直接上主题. log4android 是一个类似于log4j的开源android 日志记录项目. 项目基于 microlog 改编而来, 新加入了对文件输出的各种定义方式. 项目地址: 点 ...

  2. zend Framework的MVC模式的搭建

    1.首先搭建Apache和MySQL,搭建的Apache中必须有PDO_MYSQL模块,如果没有,可以到官方下载. 1.配置HTTP.CONF (1)进入Apache的conf目录下,打开httpd. ...

  3. MFC中小笔记(二)

    6.有三个API函数可以运行可执行文件WinExec.ShellExecute和CreateProcess.  关于这三者的概述总结,有好几篇,自己选择. 1.CreateProcess因为使用复杂, ...

  4. 谈谈微服务中的 API 网关(API Gateway)

    前言 又是很久没写博客了,最近一段时间换了新工作,比较忙,所以没有抽出来太多的时间写给关注我的粉丝写一些干货了,就有人问我怎么最近没有更新博客了,在这里给大家抱歉. 那么,在本篇文章中,我们就一起来探 ...

  5. Python函数篇(2)-递归函数、匿名函数及高阶函数

    1.全局变量和局部变量 一般定义在程序的最开始的变量称为函数变量,在子程序中定义的变量称为局部变量,可以简单的理解为,无缩进的为全局变量,有缩进的是局部变量,全局变量的作用域是整个程序,而局部变量的作 ...

  6. 使用superMap实现点标注和区域着色

    1.定义html文件,引入superMap的js和theme文件: <script src='${_ctxPath }/statics/js/superMap/SuperMap.Include. ...

  7. Codeforces 850C Arpa and a game with Mojtaba

    题意:给定一个正整数序列,两人轮流对这个数列进行如下修改:选取一个素数p和一个整数k将序列中能整除p^k的数除以p^k,问谁有必胜策略. 借此复习一下sg函数吧,sg(x) = mex ( sg(y) ...

  8. underscore源码解析(一)

    留存root // Establish the root object, `window` (`self`) in the browser, `global` // on the server, or ...

  9. 三十天学不会TCP,UDP/IP网络编程-IP头格式祥述

    我又来了,这篇文章还是来做(da)推(guang)介(gao)我自己的!俗话说事不过三,我觉得我下次得换个说法了,不然估计要被厌恶了,但是我是好心呐,一定要相信我纯洁的眼神.由于这两年接触到了比较多的 ...

  10. java学习笔记之集合家族1

    集合 集合介绍: 由于数组中存放对象,对对象操作起来不方便.java中有一类容器,专门用来存储对象. 集合与数组的区别: 1.数组的长度固定的,而集合长度时可变的 2.数组只能储存同一类型的元素,而且 ...