- 浏览: 8509 次
- 来自: 北京
最新评论
Fragments 诞生初衷
自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragments 。fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将Activity 的布局分散到frament 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化。
Fragments 设计理念
在设计应用时特别是Android 应用 ,有众多的分辨率要去适应,而fragments 可以让你在屏幕不同的屏幕上动态管理UI。例如:通讯应用程序(QQ),用户列表可以在左边,消息窗口在右边的设计。而在手机屏幕用户列表填充屏幕当点击某一用户时,则弹出对话窗口的设计,如下图:
Fragments的生命周期
每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:
其中大多数程序必须使用Fragments 必须实现的三个回调方法分别为:
onCreate
系统创建Fragments 时调用,可做执行初始化工作或者当程序被暂停或停止时用来恢复状态,跟Activity 中的onCreate相当。
onCreateView
用于首次绘制用户界面的回调方法,必须返回要创建的Fragments 视图UI。假如你不希望提供Fragments 用户界面则可以返回NULL。
onPause
当用户离开这个Fragments 的时候调用,这时你要提交任何应该持久的变化,因为用户可能不会回来。更多的事件可以参考上图的生命周期关系图。
Fragments 的类别
系统内置了三种Fragments ,这三种Fragments 分别有不同的应用场景分别为:
DialogFragment
对话框式的Fragments,可以将一个fragments 对话框并到activity 管理的fragments back stack 中,允许用户回到一个前曾摒弃fragments.
ListFragments
类似于ListActivity 的效果,并且还提供了ListActivity 类似的onListItemCLick和setListAdapter等功能。
PreferenceFragments
类似于PreferenceActivity .可以创建类似IPAD的设置界面。
Fragments 的详细使用
首先先来看一张DEMO 效果图:
左边点击时,右边的字符会与左边选中的项的字符相同。与IPAD上的设置界面很相似,这一点是否借鉴了ipad 上的UI呢?
相就的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="horizontal" > <fragment class="com.xuzhi.fragment.FragmentDemoActivity$TitlesFragment" android:id="@+id/titles" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" android:background="?android:attr/detailsElementBackground" ></FrameLayout> </LinearLayout>
主界面代码(己做注释):
package com.xuzhi.fragment; import android.app.Activity; import android.app.AlertDialog; import android.app.Fragment; import android.app.FragmentTransaction; import android.app.ListFragment; import android.os.Bundle; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; public class FragmentDemoActivity extends Activity { public static String[] array = { "text1,", "text2", "text3", "text4", "text5,", "text6", "text7", "text8" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public static class TitlesFragment extends ListFragment { boolean mDualPane; int mCurCheckPosition = 0; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); System.out.println("Fragment-->onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub System.out.println("Fragment-->onCreateView"); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("Fragment-->onPause"); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("Fragment-->onStop"); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); System.out.println("Fragment-->onAttach"); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("Fragment-->onStart"); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("Fragment-->onResume"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("Fragment-->onDestroy"); } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); System.out.println("Fragment-->onActivityCreted"); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array)); View detailsFrame = getActivity().findViewById(R.id.details); mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; if (savedInstanceState != null) { mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); //从保存的状态中取出数据 } if (mDualPane) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); showDetails(mCurCheckPosition); } } @Override public void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); outState.putInt("curChoice", mCurCheckPosition);//保存当前的下标 } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); showDetails(position); } void showDetails(int index) { mCurCheckPosition = index; if (mDualPane) { getListView().setItemChecked(index, true); DetailsFragment details = (DetailsFragment) getFragmentManager() .findFragmentById(R.id.details); if (details == null || details.getShownIndex() != index) { details = DetailsFragment.newInstance(mCurCheckPosition); //得到一个fragment 事务(类似sqlite的操作) FragmentTransaction ft = getFragmentManager() .beginTransaction(); ft.replace(R.id.details, details);//将得到的fragment 替换当前的viewGroup内容,add则不替换会依次累加 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果 ft.commit();//提交 } } else { new AlertDialog.Builder(getActivity()).setTitle( android.R.string.dialog_alert_title).setMessage( array[index]).setPositiveButton(android.R.string.ok, null).show(); } } } /** * 作为界面的一部分,为fragment 提供一个layout * @author terry * */ public static class DetailsFragment extends Fragment { public static DetailsFragment newInstance(int index) { DetailsFragment details = new DetailsFragment(); Bundle args = new Bundle(); args.putInt("index", index); details.setArguments(args); return details; } public int getShownIndex() { return getArguments().getInt("index", 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub if (container == null) return null; ScrollView scroller = new ScrollView(getActivity()); TextView text = new TextView(getActivity()); int padding = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 4, getActivity() .getResources().getDisplayMetrics()); text.setPadding(padding, padding, padding, padding); scroller.addView(text); text.setText(array[getShownIndex()]); return scroller; } } }
相关推荐
本篇文章主要介绍了Android Fragments 详细使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Android Fragments, 2014, pdf
[Packt Publishing] 动态 UI 开发教程 Android Fragments 实现 英文版 [Packt Publishing] Creating Dynamic UI with Android Fragments E Book ☆ 图书概要:☆ Leverage the power of Android fragments to ...
代码来自博客实例:http://blog.csdn.net/lmj623565791/article/details/42628537 有问题请博客留言~~
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult at best. As developers, we need more control than that afforded by activities. We need a new ...
This book looks at the impact fragments have on Android UI design and their role in both simplifying many common UI challenges and in providing best practices for incorporating rich UI behaviors....
Creating Dynamic UI with Android Fragments Leverage the power of Android Fragments to develop dynamic user interfaces for your apps
基于Android框架的动态UI构建(Creating Dynamic UI with Android Fragments, 2nd Edition)-2016英文原版,0积分——该书是2016年最新的第2版,全书154页。
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult. As developers, we need more control than that afforded by activities. We need a new approach: ...
Trying to meet these demands using Android's traditional activity-centric UI design model is dif cult at best. As developers, we need more control than that afforded by activities. We need a new ...
使用Fragment如何创建高性能UI;使用Fragment如何创建高性能UI
android-tutorial, 在android中,特性动画和 Fragments 教程 Android教程这里项目包含两个部分。 属性动画与 fragment 在Android中的应用。属性动画一个简单而完整的演示,可以在安卓中实现。 获取更多信息在 ...
Android: ViewPager and Fragments The FragmentPagerActivity class creates the ViewPager and the associated FragmentPagerAdapter.
它拥有自己的生命周期,接受它自己的输入事件,你也可以在运行activity的时候添加或者移除它(有点像“子activity”你可以在不同的activity中重用) 这节课演示怎么样使用Support Library继承Fragment类,如此你的app...
AndroidAutowire, Android视图activity 和 Fragments的Java注释方法 Android自动装配使用Java注释和反射,这个库将允许你用基于注释的方法替换来自 activity 。Fragments 和视图的一些烦人的样板设置。这里知识库在...
adp-worker-fragments, 使用 Fragments 处理配置更改 工作 Fragment这里示例代码随处理配置更改的博客文章一起随 Fragments 。这个存储库中的代码是开发人员要共享的Android的产品,并且它的所有内容都在,中。
fragements例子 博文链接:https://dasheng.iteye.com/blog/986809
一个实用程序库,用于使用Android Fragments促进React Native开发。 入门 $ npm install react-native-android-fragment --save 或者 $ yarn add react-native-android-fragment 大多是自动安装 $ react-native ...