lichao_normal

导航

android Fragment

  fragment做为android中activity的一个重要的组成部分,它理解起来相当简单。如果学习过html的知道,在html中也有一个标签叫fragment,它的作用是嵌入一个html文件,所以activity中的fragment也是一样的,它是activity的组成部分,每个fragment可以包含一个layout,而一个layout又可以包含多个fragment,就是这种循环的嵌套,使得android activity更加具有灵活性。因此,fragment可以被称为容器的容器。

  特别注意的是fragment必须嵌入到activity中才行。并且fragment的生命周期直接受宿主activity的影响。发生当宿主activity被暂停的时候,fragment也就被暂停了,当宿主activity被销毁时,fragment同样也要被销毁。当一个activity运行时,可以动态的添加和删除一个fragment。

  创建fragment

  要想创建fragment,必须创建Fragment的子类。Fragment类的代码和activity的代码非常相似。它包含于activity类似的的回调方法,如onCreate(),onStart(),onPause()和onStop()。

 我们写一个例子来了解Fragment的使用,首先创建两个layout布局文件,这两个布局文件是用来被两个fragment解析的,即这两个layout文件是被分别嵌入到fragment中的。

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:text="example"/>
    <Button android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="submit"/>
    
</LinearLayout>
my_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="23123123123123"
        />
</LinearLayout>
show_fragment

  两个布局文件创建好后,就可以创建两个继承Fragment类的子类,这也是创建片段的必要过程。这两个类的主要作用是动态的解析上述的两个布局文件。

 

public class myFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveBundle){
            return inflater.inflate(R.layout.my_fragment, container,false);
        }
}
myFragment
public class showFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveBundle){
        return inflater.inflate(R.layout.show_fragment, container,false);
    }
}
showFragment

  最后就是在主activity中添加Fragment组件就可以了,并且可以选择在Fragment中设置属性android:name=“”;

这样静态的将一个加载一个fragment。如:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment 
        android:name="com.example.day1119.myFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/list"
        />   
     <fragment android:id="@+id/list1"
         android:name="com.example.day1119.showFragment"
         android:layout_width="0dp"
         android:layout_weight="2"
         android:layout_height="match_parent"
         />
</LinearLayout>
activity_main

  这样就可以得到一个含有两个fragment的activity。用这样的方法,可以向activity中添加多个fragment。过程都是一个的,编写一个布局文件,编写一个解析布局文件的fragment,在主activity中添加fragment组件。

 

posted on 2016-11-22 16:08  lichao_normal  阅读(101)  评论(0)    收藏  举报