最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android學習之Fragment

 更新時間:2016年01月14日 09:16:28   投稿:lijiao  
Fragment是什么?碎片(Fragment)是一種可以嵌入在活動(activity)當中的UI片段,想要了解碎片的簡單用法的朋友可以參考一下

Fragment 是什么

碎片(Fragment)是一種可以嵌入在活動(activity)當中的 UI 片段。

一、碎片的簡單用法

創(chuàng)建兩個布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:text="Button"
      />
  </LinearLayout>
//left_fragment.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:textSize="20sp"
      android:text="This is right fragment"
      />
  </LinearLayout>

//right_fragment.xml

所有的自定義 Fragment 都需要繼承 Fragment 類:

public class LeftFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.left_fragment, container, false); return view;
  }
}
public class RightFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.right_fragment, container, false); return view;
  }
}

最后定義 activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
      android:id="@+id/left_fragment"
      android:name="com.example.fragmenttest.LeftFragment"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
    <fragment
      android:id="@+id/right_fragment"
      android:name="com.example.fragmenttest.RightFragment"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
  </LinearLayout>

二、動態(tài)添加碎片

可以在代碼當中動態(tài)添加碎片:

@Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.button:
        AnotherRightFragment fragment = new AnotherRightFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.
  beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.commit();
        break;
      default:
        break;
  } }

動態(tài)添加碎片主要分為 5 步。

1、創(chuàng)建待添加的碎片實例。
2、獲取到 FragmentManager,在活動中可以直接調(diào)用 getFragmentManager()方法得到。
3、開啟一個事務,通過調(diào)用 beginTransaction()方法開啟。
4、向容器內(nèi)加入碎片,一般使用 replace()方法實現(xiàn),需要傳入容器的 id 和待添加的碎片實例。
5、提交事務,調(diào)用 commit()方法來完成。

三、在碎片中模擬返回棧

通過點擊按鈕添加了一個碎片之后,這時按下 Back 鍵程序就會直接退出。

 public class MainActivity extends Activity implements OnClickListener {
......
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.button:
        AnotherRightFragment fragment = new AnotherRightFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.
  beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
      default:
  break; }
  }
}

四、碎片和活動之間進行通信

為了方便碎片和活動之間進行通信,FragmentManager 提供了一個類似于 findViewById() 的方法,專門用于從布局文件中獲取碎片的實例,代碼如下所示:

  RightFragment rightFragment = (RightFragment) getFragmentManager()
      .findFragmentById(R.id.right_fragment);

在每個碎片中都可以通過調(diào)用getActivity() 方法來得到和當前碎片相關(guān)聯(lián) 的活動實例,代碼如下所示:

  MainActivity activity = (MainActivity) getActivity();

五、碎片的生命周期

運行狀態(tài):當一個碎片是可見的,并且它所關(guān)聯(lián)的活動正處于運行狀態(tài)時,該碎片也處于運行狀態(tài)。
暫停狀態(tài):當一個活動進入暫停狀態(tài)時(由于另一個未占滿屏幕的活動被添加到了棧頂),與它相關(guān)聯(lián)的可見碎片就會進入到暫停狀態(tài)。
停止狀態(tài):當一個活動進入停止狀態(tài)時,與它相關(guān)聯(lián)的碎片就會進入到停止狀態(tài)。
銷毀狀態(tài):碎片總是依附于活動而存在的,因此當活動被銷毀時,與它相關(guān)聯(lián)的碎片就會進入 到銷毀狀態(tài)。

下面是碎片的一些回調(diào)方法:

  • onAttach() 當碎片和活動建立關(guān)聯(lián)的時候調(diào)用。
  • onCreateView() 為碎片創(chuàng)建視圖(加載布局)時調(diào)用。
  • onActivityCreated() 確保與碎片相關(guān)聯(lián)的活動一定已經(jīng)創(chuàng)建完畢的時候調(diào)用。
  • onDestroyView() 當與碎片關(guān)聯(lián)的視圖被移除的時候調(diào)用。
  • onDetach() 當碎片和活動解除關(guān)聯(lián)的時候調(diào)用。

以上就是關(guān)于Android中碎片F(xiàn)ragment的全部內(nèi)容,希望對大家的學習有所幫助。

相關(guān)文章

  • Android中GridView插件的使用方法

    Android中GridView插件的使用方法

    今天小編就為大家分享一篇關(guān)于Android中GridView插件的使用方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android自定義實現(xiàn)可回彈的ScollView

    Android自定義實現(xiàn)可回彈的ScollView

    這篇文章主要為大家詳細介紹了Android自定義實現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android?Kotlin?中的groupBy方法詳解

    Android?Kotlin?中的groupBy方法詳解

    在Kotlin中,groupBy函數(shù)可以對集合進行分組,形成一個Map,其中key是分組標準,value是對應的元素列表,本文通過實例詳細解釋groupBy的使用方法和常見應用場景,如按員工年齡分組或產(chǎn)品類型統(tǒng)計數(shù)量等,展示了groupBy的靈活性和實用性
    2024-09-09
  • ViewPager實現(xiàn)輪播圖引導頁

    ViewPager實現(xiàn)輪播圖引導頁

    這篇文章主要為大家詳細介紹了ViewPager實現(xiàn)輪播圖引導頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 微信公眾平臺開發(fā)入門教程(SAE方倍工作室)

    微信公眾平臺開發(fā)入門教程(SAE方倍工作室)

    在這篇微信公眾平臺開發(fā)教程中,我們假定你已經(jīng)有了PHP語言程序、MySQL數(shù)據(jù)庫、計算機網(wǎng)絡通訊、及HTTP/XML/CSS/JS等基礎(chǔ)
    2014-05-05
  • Android UI使用HTML布局方法實例

    Android UI使用HTML布局方法實例

    這篇文章主要介紹了Android UI使用HTML布局方法實例,布局文件直接用一個WebView來代替,這樣就可以在WebView中使用HTML布局了,需要的朋友可以參考下
    2015-05-05
  • Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法

    Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法

    今天小編就為大家分享一篇關(guān)于Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android中的dumpsys命令詳解

    Android中的dumpsys命令詳解

    本文詳細講解了Android中的dumpsys命令,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Android開發(fā)筆記 Handler使用總結(jié)

    Android開發(fā)筆記 Handler使用總結(jié)

    當應用程序啟動時,Android首先會開啟一個主線程(也就是UI線程),主線程為管理界面中的UI控件,進行事件分發(fā)
    2012-11-11
  • android 獲取本機的IP地址和mac物理地址的實現(xiàn)方法

    android 獲取本機的IP地址和mac物理地址的實現(xiàn)方法

    本文主要介紹android 獲取本機的IP地址和mac物理地址的實現(xiàn)方法,這里提供示例代碼,實現(xiàn)功能,有需要的小伙伴可以參考下
    2016-09-09

最新評論

甘肃省| 肥乡县| 奉贤区| 和田县| 青神县| 桂平市| 云梦县| 额敏县| 文安县| 湘乡市| 儋州市| 中方县| 潍坊市| 通州市| 金溪县| 建湖县| 崇义县| 星子县| 石楼县| 平度市| 栾川县| 青冈县| 临沭县| 南投市| 宜良县| 湘乡市| 平安县| 安远县| 渭源县| 五台县| 汝南县| 海口市| 拜城县| 探索| 临湘市| 巧家县| 娱乐| 昭通市| 镇原县| 台东市| 丹凤县|