大叔程序员的第二天 @Fragment学习

Fragment生命周期类似于Activity。

用于分屏显示功能。

使用可继承Fragment类或者其三个子类:DialogFragment,ListFragment,PreferenceFragment。

文库的学习资料http://wenku.baidu.com/view/13fa98c008a1284ac8504331.html介绍的比较全。

举个2个创建Fragment的例子,下面是一个由2个Fragment组成的Activity

例子是一个有2个fragment的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> 

而如果想通过无UI的方法调用Fragment,则可通过要添加一个无UI的fragment, 需要从activity使用 add(Fragment, String) 来添加 fragment

(为fragment提供一个唯一的字符串"tag", 而不是一个view ID).这么做添加了fragment, 但因为它没有关联到一个activity layout中的一个view,

所以不会接收到onCreateView()调用. 因此不必实现此方法.

管理Fragment


常用的方法如getFragmentManager(), findFragmentById(),findFragmentByTag()

原文地址:https://www.cnblogs.com/linxiaojiang/p/2944314.html