zoukankan      html  css  js  c++  java
  • Xamarin Android Fragment的两种加载方式

    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实现底部导航栏

  • 相关阅读:
    【拆点费用流】【HDU1853】【 Cyclic Tour】
    【最小费用最大流】【HDU1533】【Going Home】
    【最大流,二分图匹配】【hdu2063】【过山车】
    【最小费用最大流模板】【Uva10806+Spring Team PK】Dijkstra, Dijkstra,
    【最大流之sap】【HDU1532】模板题
    HDU 6130 Kolakoski 思维个屁 水题
    Codeforces 837 D Round Subset DP 思维
    Educational Codeforces Round 26
    Codeforces Round 427 B The name on the board 水题
    Codeforces Round 428 B Game of the rows 贪心 思维
  • 原文地址:https://www.cnblogs.com/zhangmumu/p/7374804.html
Copyright © 2011-2022 走看看