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