为什么要使用Fragment就不再多说了,网上有很多介绍,Fragment相对于Activity更灵活。为什么呢?Fragment与Activity有什么联系与区别呢?如果说Activity是一个大箱子的话,Fragment就是大箱子里面分装东西的小盒子,这样很容易理解东西分装在小盒子里更容易替换或更改,界面改变与设计也就更加灵活。这里就重点谈一下Fragment的入门使用动态加载与静态加载。
静态加载
特点:Fragment直接嵌入到Activity的xml文件中,这种方式使用fragment代码量少,但是灵活度低。
如下:直接在Activity的xml文件中嵌入MyFragment,然后对MyFragment进行设计,这里也可以嵌入多个,以一个为例。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmentContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/MyFragment" android:name="com.example.learningtest1_fragment.MyFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
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/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> </LinearLayout>
细心地同学可能发现了一个问题,这两个xml文件,Actvity的xml文件中嵌入fragment,fragment的xml文件控制界面控件排版,那么二者的联系点在哪?也就是说activtiy怎么知道加载哪个fragment呢?注意activity中这一行 android:name="com.example.learningtest1_fragment.MyFragment"。没错就是根据类名来唯一标识这种联系的。很明显,我们还需要一个MyFragment的类来作为纽带。
package com.example.learningtest1_fragment; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class MyFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * resource:需要加载的布局文件 * root:加载layout的父viewgroup的 * false:不返回父viewgroup */ View view= inflater.inflate(R.layout.myfragment, container, false); TextView tv=(TextView)view.findViewById(R.id.text1); tv.setText("静态加载fragment"); return view; } }
如此,在Activity显示的时候,fragment中的视图就会显示在界面上。
动态加载
特点:不在Activity的xml中嵌入,而是在代码中使用FragmentManger控制加载。这种方式灵活度高,可以通过代码替换、增加、移除fragment,但是代价就是大量代码用于加载控制。
同样,创建一个类MyFragmnet2
package com.example.learningtest1_fragment; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class MyFragment2 extends android.app.Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * resource:需要加载的布局文件 * root:加载layout的父viewgroup的 * false:不返回父viewgroup */ View view= inflater.inflate(R.layout.myfragment2, container, false); TextView tv=(TextView)view.findViewById(R.id.text1); tv.setText("动态加载fragment"); return view; } }
其对应的Fragmnet2.xml文件是:
相关推荐
Fragment动态加载和静态加载
静态加载fragment示例源码
Fragment静态加载
fragment设计哲理、静态、动态加载
Android 静态加载fragment,参考博客: http://blog.csdn.net/u010661782/article/details/51790987
主要介绍了Android应用开发中Fragment的静态加载与动态加载实例,例子中包括动态的添加更新以及删除Fragment等操作,很有借鉴意义,需要的朋友可以参考下
对Fragment静态使用的简单示例,配合博客说明:http://blog.csdn.net/swaytop
关于介绍xamarin android中fragment静态加载的用法,代码很简单,大家有意的可以参考参考。
实现静态、动态、添加、删除、隐藏、显示、替换Fragment事件
Android中在xml中静态添加Fragment 在activity_main.xml中添加LeftFragment和RightFragment
主要介绍了Android碎片fragment实现静态加载的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
使用静态fragment+动态figment+Glide三者结合加载任何网络照片,自带三级缓存
1.fragment动态切换 2.fragment静态加载 3.activity与fragment事件共享
加载网络图片并三级缓存+activity与fragment回调通讯
1. 静态加载 1.1 加载步骤 (1) 创建fragment:创建自定义Fragment类继承自Fragment类,同时将自定义Fragment类与Fragment视图绑定(将layout转换成View) View onCreateView(LayoutInflater inflater, ViewGroup ...
自己学习fragment时了解的东西,还有包括viewpager与fragment结合的tab实现、静态和动态加载fragment等
Android Fragment(动态,静态)碎片详解 一.Fragment的相关概念(一)Fragment的基础知识 Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户...