Android Fragment用法实例说明
2014-11-21 21:54
344 查看
1、Fragment简介
Fragment是Android3.0引入一个类。因为当Android的世界成员越来越多,种类也变得更加丰富。其中最具特征的就是屏幕的尺寸千变万化。这就给Android开发者带来很多的痛苦。谷歌在2011年02月03日发布了专用于平板电脑的Android 3.0 Honeycomb系统,它带来了很多激动人心的新特性。这是首个基于Android的平板电脑专用操作系统。
为了使的在手机上开发的应用能够顺利移植到平板的大屏幕中,如果重新开发将会耗费大量人力物力,因此出了一个fragment。它使得Android程序布局在小屏幕手机或者大屏幕平板上都能游刃有余。
有很多代表性的应用都具有这样的特性。面试的时候,很多Android面试官也总喜欢询问有关Fragment的问题。因此有必要将学习的记录下来。
下图是精通Android4中介绍的:
1⃣️新建一个xml文件:
将Fragment嵌套进Activity中有两种加载方式:
1⃣️通过xml布局文件
2⃣️通过FragmentManager对Fragment进行实时管理
在我的demo中,我将两种类型融合在一起了:
新建一个xml布局文件
最后效果:
Fragment是Android3.0引入一个类。因为当Android的世界成员越来越多,种类也变得更加丰富。其中最具特征的就是屏幕的尺寸千变万化。这就给Android开发者带来很多的痛苦。谷歌在2011年02月03日发布了专用于平板电脑的Android 3.0 Honeycomb系统,它带来了很多激动人心的新特性。这是首个基于Android的平板电脑专用操作系统。
为了使的在手机上开发的应用能够顺利移植到平板的大屏幕中,如果重新开发将会耗费大量人力物力,因此出了一个fragment。它使得Android程序布局在小屏幕手机或者大屏幕平板上都能游刃有余。
有很多代表性的应用都具有这样的特性。面试的时候,很多Android面试官也总喜欢询问有关Fragment的问题。因此有必要将学习的记录下来。
2、Fragment的生命周期
由于Fragment是在Activity中嵌套的,因此他的生命周期与Activity的生命周期密切相关。下图是官网中Fragment的生命周期:下图是精通Android4中介绍的:
3、Fragment的开发使用
第一步:建立Fragment的UI界面1⃣️新建一个xml文件:
<?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" ]]> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/ic_launcher" /> </LinearLayout>2⃣️新建一个继承Fragment的子类
package com.scy.fragmenttest2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 定义Fragment视图 * @author songchunyu * */ public class MyFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 定义Fragment的View View myView = inflater.inflate(R.layout.fragment_view,container, false); return myView; } }第二步:将Fragment嵌套进Activity中
将Fragment嵌套进Activity中有两种加载方式:
1⃣️通过xml布局文件
2⃣️通过FragmentManager对Fragment进行实时管理
在我的demo中,我将两种类型融合在一起了:
新建一个xml布局文件
<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="com.scy.fragmenttest2.FragmentTest" android:orientation="horizontal" > <Button android:id="@+id/btn_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏Fragment" /> <!-- android:id="@+id/fragment" --> <!-- 第一种方式,使用xml布局 --> <fragment android:name="com.scy.fragmenttest2.MyFragment" android:tag="test_scy" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:visibility="invisible" /> <!-- 第二种方式 使用编程,将fragment实例add进一个ViewGroup,这里是LinearLayout--> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/test" android:layout_weight="1" ></LinearLayout]]> </LinearLayout>在Activity中,进行两种方式的调用:
package com.scy.fragmenttest2; import android.support.v7.app.ActionBarActivity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FragmentTest extends ActionBarActivity { private FragmentManager fManager = null; private Fragment fragment = null; private Fragment fragment2 = null; private Button button; private FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_test); // 1⃣️通过id获取fragment,同样也可以通过tag fManager = getFragmentManager(); // final Fragment fragment = fManager.findFragmentById(R.id.fragment); fragment = fManager.findFragmentByTag("test_scy"); // fragment.setMenuVisibility(false); // 2⃣️通过编程方式 fragment2 = new MyFragment(); // 注意这里需要每次事件重新建立 // ft = fManager.beginTransaction(); // ft.add(R.id.test, fragment2); // ft.hide(fragment); button = (Button)findViewById(R.id.btn_show); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 每次编辑事件都需要新建一个这样的事件 ft = fManager.beginTransaction(); //为Fragment设置淡入淡出效果 ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out); // 2⃣️通过另外一种方式添加Fragment ft.add(R.id.test, fragment2); // ft.hide(fragment); if (fragment.isHidden()) { ft.show(fragment); button.setText("隐藏Fragment"); } else { ft.hide(fragment); button.setText("显示Fragment"); } ft.commit(); } }); } }
最后效果:
相关文章推荐
- Android - TabHost 选项卡功能用法详解&&TabWidget切换卡的实现应用&&实例说明
- Android Fragment的用法实例详解
- Android - TabHost 选项卡功能用法详解&&TabWidget切换卡的实现应用&&实例说明
- Android开发之DialogFragment用法实例总结
- Android之Fragment静态实现实例
- Android之Fragment静态实现实例
- Android UI开发第十七篇——Android Fragment实例
- Android UI开发第十七篇——Android Fragment实例
- Android——ViewGroup的一个用法实例(转载)
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- Android权限之sharedUserId、签名(实例说明)
- Android中AsyncTask的用法实例
- Android:intent用法实例
- static简单实例说明其用法(转)
- 举例说明android中ImageView的用法
- velocity用法简单实例说明 .
- ROW_NUMBER、RANK、DENSE_RANK的用法(2) 实例说明与对比
- Android中AsyncTask的用法实例
- Android深入浅出系列之实例应用—开机就运行自定义的程序BroadcastReceiver和intent-filter的用法
- 关于android的几个组件用法实例