关于fragment的学习总结(1)
2016-01-20 09:23
363 查看
一:什么是fragment:
fragment :Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。(以上来自鸿洋前辈的介绍,这篇文章也参考了很多鸿洋前辈的内容,相关的博客请移步 点击打开链接 /article/1336252.html,还是蛮详细的)
二: fragment的生命周期
下面一幅图是来自官网的图:onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
用的最多的是onCreateView()方法,该方法返回的View将有fragment显示出来
从这幅图也是可以看出的:fragment和activity的生命周期是有联系的,Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期
三:使用fragment的例子详解
下面先上效果图:(定义了两个fragment,一个是用来显示title即左边的按钮区域,右边是内容区域,点击按钮会触发事件)
使用fragment也是很简单,把Fragment当成普通的控件,直接写在Activity的布局文件中
下面是步骤:(来自网络)
1:在主配置文件中Activity中声明此Fragment,就当和普通的View一样,自定name
2:继承Fragment,重写onCreateView决定Fragemnt的布局
贴上测试的代码:
首先是titlt部分的fragment布局文件,(很简单,只有两个按钮,设置背景颜色而已):
<RelativeLayout 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:background="#ff00" tools:context="com.example.administrator.fragmenttext2.titleFragment"> <!-- TODO: Update blank fragment layout --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:id="@+id/login" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:id="@+id/regic" android:layout_below="@+id/login" android:layout_alignEnd="@+id/login" android:layout_marginTop="102dp" /> </RelativeLayout>
titileFragment
package com.example.administrator.fragmenttext2; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; /** * A simple {@link Fragment} subclass. */ public class titleFragment extends Fragment implements View.OnClickListener { View view; public titleFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment //关联相关的布局 view= inflater.inflate(R.layout.fragment_title, container, false); view.findViewById(R.id.login).setOnClickListener(this); view.findViewById(R.id.regic).setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.regic : Toast.makeText(getActivity(), "这是注册的fragment的测试 ! ", Toast.LENGTH_SHORT).show(); case R.id.login : Toast.makeText(getActivity(), "这是登录的fragment的测试 ! ", Toast.LENGTH_SHORT).show(); } } }
还有ContentFragment的其布局文件
<FrameLayout 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:background="#3225c7" tools:context="com.example.administrator.fragmenttext2.ContentFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/hello_blank_fragment" /> </FrameLayout>
package com.example.administrator.fragmenttext2; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. */ public class ContentFragment extends Fragment { public ContentFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_content, container, false); } }
MainActivity和其布局文件,fragment中要自定name
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:name="com.example.administrator.fragmenttext2.titleFragment" android:id="@+id/titleFragment"> </fragment> <fragment android:layout_width="0dp" android:layout_weight="3" android:name="com.example.administrator.fragmenttext2.ContentFragment" android:id="@+id/contentFragment" android:layout_height="match_parent"> </fragment> </LinearLayout>
package com.example.administrator.fragmenttext2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
好了,就是这么的简单,这也是最基本的用法,再来看效果图
相关文章推荐
- Java中的继承,多态,抽象类
- 高逼格UI-ASD(Android Support Design)
- Android Sqlite 之事务
- 关于JNA调用32位和64位动态链接库,即*.dll
- 什么情况使用存储过程
- centos防火墙管理
- C#-如何让TABLE中的TEXTBOX充满单元格
- Android开发:Google网络框架Volley的使用
- 去除导航栏下方的横线
- 2016{太原网络营销师}分享如何做404页面?
- 推荐提升 Android 性能的建议
- HTML 5结构
- 在其他地方你学不到的jQuery小贴士和技巧(欢迎收藏)
- Python调用MATLAB Install MATLAB Engine for Python
- CodeForces 584E Anton and Ira
- From Disk partition to PostgreSQL installation
- SPSS Modeler数据挖掘:数据挖掘概述
- Android02_Android API Guide学习和常用布局及基本UI控件
- java.lang.OutOfMemoryError处理错误
- Django+xadmin 显示问题,无法添加inline