Fragments的初识---不知道Fragments的不是合格的android开发
https://www.cnblogs.com/meetyourmakers/archive/2011/12/22/2297962.html
Fragments是Android 3.0 (API level 11)才引入的.但是它却又是向下兼容的.可以支持老的Android版本.
只不过需要导入jar包支持(在这个目录下:android-sdk-windows\extras\android\support\v4\android-support-v4.jar),
主要用于实现以下这种UI布局
想要实现这样一个activity里面有多个复杂的View布局, 按照以前的惯用写法可以使用viewgroup 或者自定义一些布局来实现
而Fragments就恰恰满足了我们这一需求,他相比我们原来实现的方法,功能更强大,
简单说来,我个人觉得可以把Fragments看成是一个view,他比view强大的地方是他是有生命周期的 并且这个生命周期会随着他附着的那个activity的改变而改变.
onCreate()
The system calls this when creating the
fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
onCreateView()
The
system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View
from
this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
onPause()
The system calls this method as the first indication that
the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
这是他的三个主要的方法,或者说生命周期.当他的父activity onPause();的时候,他也会调用onPause();同样他的父activityonResume();他调用onResume();
这样就很灵活,比如有大量bitmap的view,就可以在 onPause();释放掉无用的资源,再在onResume()的时候加载.
这些Fragments是由FragmentManager这样一个类似栈的容器管理的,可以通过 findFragmentById()
或者findFragmentByTag()
找到添加到页面里面的Fragments.
下面看个小小的DEMO:
首先我们要写一个继承Fragment的类,你可以把他看成你的UI界面里的某一个模块
public class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println(" onCreateView "); // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } @Override public void onCreate(Bundle savedInstanceState) { System.out.println(" onCreate "); super.onCreate(savedInstanceState); } @Override public void onPause() { System.out.println(" onPause "); super.onPause(); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println(" onResume "); } }
根据打印输出,你会发现只有第一次启动才会调用onCreateView;而这里面就是你这个UI模块的布局
接着是你要展示的activity:
public class MyFragmentActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // ExampleFragment exampleFragment = new ExampleFragment(); // FragmentManager fragmentManager = getSupportFragmentManager(); // FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // fragmentTransaction.add(R.id.viewer, exampleFragment); // fragmentTransaction.commit(); } }
然后activity的布局也非常简单
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/list" android:name="com.fragment.demo.ExampleFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/viewer" android:name="com.fragment.demo.ExampleFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>
这样就已经搞定,生成了2个fragment, 这算是静态的添加吧
如果是动态的添加那就需要用到上面注释的代码:
ExampleFragment exampleFragment = new ExampleFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.viewer, exampleFragment);
fragmentTransaction.commit();
差不多初识就是这样,希望有人能补充,下个项目有机会一定要尝试用用
最后更新:2017-04-02 17:28:36
上一篇:
android 图片点击一下就放大到全屏,再点一下就回到原界面
下一篇:
Android图片处理-----ScaleGestureDetector(缩放手势检测)
How Should I Backup MongoDB Databases?
后台开发:核心技术与应用实践1.6.2 结构体、共用体在内存单元占用字节数的计算
用UBOOT自带loadb命令加载应用程序到SDRAM中运行的方法
程序猿必看的 几部电影
你知道Unity IoC Container是如何创建对象的吗?
Linux文件权限
在windows和centos做mysql主从搭建方法
迷宫之广度优先搜索
利用 DirectShow 开发自己的 Filter
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors