安卓专题

安卓Fragment开发教程与源码下载

  Fragment 代表运行在一个Activity中的用户界面或操作部分。一个单一Activity可以包含多个fragment片段,多fragment可以在许多不同的活动中重复使用。可以说一个fragment是一个Activity的的子活动,而且能够再次在多个其他Activity中使用。虽然每个fragment都有自己的生命周期,因为它属于当时调用的Activity活动,所以它的生命周期是直接由Activity活动的生命周期影响。使用fragment的主要优点是,在不同布局中重复使用,带来了便利性

如果按了No.1按钮,出现下面效果:

开发步骤:

1.创建一个Main Activity

2.创建这个活动的layout布局activity_main.xml:

<?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" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" />

   <fragment
        android:name="com.jdon.android.fragmentstest.FragmentOne"
        android:id="@+id/fragment_place"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
   
</LinearLayout>

布局配置中加入了fragment

3.下面创建这个 Fragment Class.,

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
     
       //Inflate the layout for this fragment
      
      return inflater.inflate(
                    R.layout.fragment_one, container, false);
   }
}

再创建第二个FragmentTwo.java:

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

 

public class FragmentTwo extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
    
      // Inflate the layout for this fragment
      
      return inflater.inflate(
              R.layout.fragment_two, container, false);
   }
}

正如你可以在代码中看到上面,我们使用了onCreateView重写方法。它被Android系统调用,这样的fragement创建用户界面。然后,它返回被放置在布局配置中<fragment>的视图组件。我们使用的inflate方法的原因,为了拾取xml布局文件,并返回它给视图。

4.创建fragement的布局

我们将为FragmentOne和FragmentTwo分别创建两个不同的xml布局文件。

fragment_one.xml:

package com.javacodegeeks.android.fragmentstest;

<?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"
   android:background="#00ffff">

       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="This is fragment No.1"
           android:textStyle="bold" />

</LinearLayout>

fragment_two.xml:

<?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"
   android:background="#ffff00">

       <TextView
           android:id="@+id/textView2"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="This is fragment No.2"
           android:textStyle="bold" />

</LinearLayout>

5.在主活动Activity中编码

MainActivity.java:

public class MainActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);
       }
        
       public void selectFrag(View view) {
               Fragment fr;
               
               if(view == findViewById(R.id.button2)) {
                      fr = new FragmentTwo();
               
               }else {
                      fr = new FragmentOne();
               }
               
               FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_place, fr);
            fragmentTransaction.commit();
               
       }
  
}

当我们的活动Activity运行时,我们可以添加或更换Fragment片段。因为我们使用FragmentTransaction ,做出改变后commit提交以激活,类似事务机制

源码下载