日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android的Fragment介绍

發布時間:2023/12/20 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android的Fragment介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

??? fragment是從android3.0開始提出來的,用來支持大屏幕設備的ui設計。通過將activity劃分為多個fragment,不僅提高了設計的靈活性,而且可以在程序運行時改變它們的特征,比如動態的修改,替換已有的fragment等等。

??? fragment的角色是相當于activity中ui的一個子集或者說是activity中的一個模塊,可以通過組合多個fragment來構造一個多個面板的界面。由于fragment嵌入在activity中,因此它的生命周期受到activity的影響,一旦activity停掉,它里面所有的fragment也都會被摧毀。

??? 可以在activity的布局文件中通過聲明<fragment>標簽來填充它的布局,同時也可以通過代碼將它加到已經存在的ViewGroup中,因為fragment布局實際上就是ViewGroup的子樹。

新建一個Fragment

??? 首先需要了解的是fragment的生命周期,如下圖所示:

可以看出調用的先后順序是:

  • onCreate? 這里可以用來初始化一些當fragment停掉時候你仍想保存的組件
  • onCreateView? 這里可以用來繪制fragment的用戶界面,因此必須返回一個view
  • onPause? 當fragment被移走或者替換時調用,可以在這里commit changes

一般可以繼承fragment類來創建自己的fragment,同時亦有幾個有用的fragment基類供我們繼承:

  • DialogFragment? 也就是浮動在Activity上面的fragment,這種對話框可以供用戶返回,便于管理,是google推薦的做法。
  • ListFragment? 就是包含ListView的Fragment,用來顯示列表等。
  • PreferenceFragment? 可以用來顯示用戶選項的設置。
  • 一般來說,要創建Fragment,第一步就是創建一個Fragment的子類,并在onCrateView方法中填充布局:

    public static class ExampleFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragment//resourceID,ViewGroup, whether the inflated layout should be //attached to the ViewGroup during inflationreturn inflater.inflate(R.layout.example_fragment, container, false);} }

    第二步就是將fragment添加到activity中,可以在activity的布局文件中聲明,如:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragment android:name="com.example.news.ArticleListFragment"android:id="@+id/list"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent" /><fragment android:name="com.example.news.ArticleReaderFragment"android:id="@+id/viewer"android:layout_weight="2"android:layout_width="0dp"android:layout_height="match_parent" /> </LinearLayout>

    這樣一來,當創建activity的布局時,就會初始化每一個fragment,并且調用它們的onCreateView方法,獲取fragment的布局,然后插入到ViewGroup下面。

    除了使用布局文件之外,還可以用程序進行添加:

    FragmentManager fragmentManager = getFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();

    前兩行用來獲取碎片事務FragmentTransaction,后三行用來添加一個fragment,add方法的第一個參數是要放置的ViewGroup,第二個參數是要添加的fragment。

    Fragment的管理

    通過fragmentManager可以做一下事情:

    • 獲取fragment,通過findFragmentById或者findFragmentByTag
    • 將Fragment從棧中移除,通過popBackStack方法
    • 為back stack注冊監聽器

    相信這里的細節還有很多,暫時先不介紹。

    Fragment的事務

    ??? fragment的一大亮點就是可以動態增加,刪除,替換fragment,而這是通過fragmentTransaction來實現的。一旦獲取了事務的實例,就可以通過調用add,remove,replace方法完成上述功能,接下來通過commit方法提交事務就可以了。

    // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction();// Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null);// Commit the transaction transaction.commit();

    ??? 除此之外還可以調用addToBackStack方法將fragment添加到back stack中,用戶按下返回鍵時,就會跳到back stack頂部的fragment。調不調用它的區別在于,如果沒有它,fragment被remove后就徹底銷毀了,再也不能恢復,如果調用了它,fragment就不會銷毀而是stop,這樣就可以恢復,因為fragment的狀態都被保存在了back stack上面。

    ??? commit方法的作用是調度到activity的主線程中執行,并且事務的提交必須要在onSaveInstanceState之前。

    和Activity的通信

    ??? fragment也是可以和它所在的activity通信的,比如可以通過getActivity()方法得到所在的activity,相應的,activity也可以調用FragmentManager的findFragmentById方法獲取某一個fragment的引用。

    ??? 有時候需要在fragment和activity之間共享事件,如activity有一個fragment,是用來顯示文章的列表的,當用戶點擊了某一項,activity需要檢測到這個事件,同時更改另一個fragment從而顯示出相應的文章內容。這時可以這么做,在fragment中定義事件監聽的接口,然后讓activity實現這個接口。然后,這個fragment就可以調用接口中的某些方法來通知activity了。fragment中可以在onAttach里面將activity強制轉化為接口的實現類,從而確保能成功調用接口中的方法。

    public static class FragmentA extends ListFragment {OnArticleSelectedListener mListener;...// Container Activity must implement this interfacepublic interface OnArticleSelectedListener {public void onArticleSelected(Uri articleUri);}...@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);try {mListener = (OnArticleSelectedListener) activity;} catch (ClassCastException e) {throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");}}...@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {// Append the clicked item's row ID with the content provider UriUri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);// Send the event and Uri to the host activitymListener.onArticleSelected(noteUri);}... }

    例子

    ?? 該例子來源于api demo,用到了很典型的兩個fragment,一個是TitlesFragment,用來顯示一個列表,另一個是DetailsFragment,用來顯示選中item的詳細信息。此外,該程序還考慮到了屏幕的橫豎屏問題,如果是豎屏的話,沒有足夠的地方顯示第二個Fragment,就單獨啟動一個Activity。源碼如下:

    /*** Demonstration of using fragments to implement different activity layouts.* This sample provides a different layout (and activity flow) when run in* landscape.*/ public class FragmentLayout extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_layout);}/*** This is a secondary activity, to show what the user has selected* when the screen is not large enough to show it all in one activity.*/public static class DetailsActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE) {// If the screen is now in landscape mode, we can show the// dialog in-line with the list so we don't need this activity. finish();return;}if (savedInstanceState == null) {// During initial setup, plug in the details fragment.DetailsFragment details = new DetailsFragment();details.setArguments(getIntent().getExtras());getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();}}}/*** This is the "top-level" fragment, showing a list of items that the* user can pick. Upon picking an item, it takes care of displaying the* data to the user as appropriate based on the currrent UI layout.*/public static class TitlesFragment extends ListFragment {boolean mDualPane;int mCurCheckPosition = 0;@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);// Populate list with our static array of titles.setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));// Check to see if we have a frame in which to embed the details// fragment directly in the containing UI.View detailsFrame = getActivity().findViewById(R.id.details);mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;if (savedInstanceState != null) {// Restore last state for checked position.mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);}if (mDualPane) {// In dual-pane mode, the list view highlights the selected item. getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);// Make sure our UI is in the correct state. showDetails(mCurCheckPosition);}}@Overridepublic void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putInt("curChoice", mCurCheckPosition);}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {showDetails(position);}/*** Helper function to show the details of a selected item, either by* displaying a fragment in-place in the current UI, or starting a* whole new activity in which it is displayed.*/void showDetails(int index) {mCurCheckPosition = index;if (mDualPane) {// We can display everything in-place with fragments, so update// the list to highlight the selected item and show the data.getListView().setItemChecked(index, true);// Check what fragment is currently shown, replace if needed.DetailsFragment details = (DetailsFragment)getFragmentManager().findFragmentById(R.id.details);if (details == null || details.getShownIndex() != index) {// Make new fragment to show this selection.details = DetailsFragment.newInstance(index);// Execute a transaction, replacing any existing fragment// with this one inside the frame.FragmentTransaction ft = getFragmentManager().beginTransaction();if (index == 0) {ft.replace(R.id.details, details);} else {ft.replace(R.id.a_item, details);}ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);ft.commit();}} else {// Otherwise we need to launch a new activity to display// the dialog fragment with selected text.Intent intent = new Intent();intent.setClass(getActivity(), DetailsActivity.class);intent.putExtra("index", index);startActivity(intent);}}}/*** This is the secondary fragment, displaying the details of a particular* item.*/public static class DetailsFragment extends Fragment {/*** Create a new instance of DetailsFragment, initialized to* show the text at 'index'.*/public static DetailsFragment newInstance(int index) {DetailsFragment f = new DetailsFragment();// Supply index input as an argument.Bundle args = new Bundle();args.putInt("index", index);f.setArguments(args);return f;}public int getShownIndex() {return getArguments().getInt("index", 0);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {if (container == null) {// We have different layouts, and in one of them this// fragment's containing frame doesn't exist. The fragment// may still be created from its saved state, but there is// no reason to try to create its view hierarchy because it// won't be displayed. Note this is not needed -- we could// just run the code below, where we would create and return// the view hierarchy; it would just never be used.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(Shakespeare.DIALOGUE[getShownIndex()]);return scroller;}}} View Code

    ?

    轉載于:https://www.cnblogs.com/cubika/p/3177782.html

    總結

    以上是生活随笔為你收集整理的Android的Fragment介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。