Fragment的概念是从Android3.0开始引入的,直译为碎片、片段,目的是为不同屏幕大小的设备(手机、平板等)创建灵活动态的UI。诚如其名,你可以把Fragment当作是Activity的模块化组件,它拥有自己的生命周期和UI,接受自身的处理事件,可以在Activity运行中被动态的添加、移除、替换。

      Fragment必须被写成可重用的模块,你可以将多个Fragment组合到一个Activity中创建一个多模块界面,也可以在多个Activity中包含同一个Fragment的不同实例,这对于你的界面在不同屏幕尺寸下都能给用户完美的体验至关重要。

       Fragment不能独立存在,它必须嵌入到Activity中,因此 Fragment的生命周期也依赖于Activity的生命周期,当其依赖的Activity的某个生命周期方法被调用时,该Activity下包含的所有Fragment的相应生命周期方法也将被调用,如onPause(),onStop(),onDestroy()等。

  代码如下:

    一、layout

1、主窗口

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"        
        android:layout_height="wrap_content"
        android:layout_weight="1"
         >     

    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        >
         <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

2、第一个Fragement

<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
3、第二个Fragement

<?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" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

</LinearLayout>
4、第三个Fragement

<?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" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

二、源代码

1、fragmentActity1

package com.example.fragmentdemo;

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

public class fragmentActity1 extends Fragment {

    @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState){
           return inflater.inflate(R.layout.fragment1, container, false);
       }

}

2、fragmentActity2

package com.example.fragmentdemo;

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

public class fragmentActity2 extends Fragment {

    @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState){
           return inflater.inflate(R.layout.fragment2, container, false);
       }

}
 

3、fragmentActity3

package com.example.fragmentdemo;

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

public class fragmentActity3 extends Fragment {
    
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState){
       return inflater.inflate(R.layout.fragment3, container, false);
   }


}
 

4、主窗口

package com.example.fragmentdemo;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private Button button1 ,button2,button3;
    private FragmentTransaction fragmentTransaction;
    private fragmentActity1 frame1;
    private fragmentActity2 frame2;
    private fragmentActity3 frame3;
    
    private int currentTabIndex = 0;
    private final int TAB_INDEX1 = 0;
    private final int TAB_INDEX2 = 1;
    private final int TAB_INDEX3 = 2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initViews();
    }

    private void initViews() {
        
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
                
        setTabSelected(TAB_INDEX1); 
        
    }
    
    private void hideFragment(FragmentTransaction fragment) {
        if (frame1 != null)
            fragment.hide(frame1);
        if (frame2 != null)
            fragment.hide(frame2);
        if (frame3 != null)
            fragment.hide(frame3);        
    }

    private void setTabSelected(int i) {
        fragmentTransaction = getFragmentManager().beginTransaction();
        
        hideFragment(fragmentTransaction);
        clearSelectedState();
        
        switch (i) {
        case TAB_INDEX1:
            button1.setSelected(true);
            
            if (frame1 == null) {
                frame1 = new fragmentActity1();
                fragmentTransaction.add(R.id.framelayout, frame1);
            } else {
                fragmentTransaction.show(frame1);
            }
            
            break;
        case TAB_INDEX2:
            button1.setSelected(true);
            
            if (frame2 == null) {
                frame2 = new fragmentActity2();
                fragmentTransaction.add(R.id.framelayout, frame2);
            } else {
                fragmentTransaction.show(frame2);
            }
            
            break;
        case TAB_INDEX3:
            button1.setSelected(true);
            
            if (frame3 == null) {
                frame3 = new fragmentActity3();
                fragmentTransaction.add(R.id.framelayout, frame3);
            } else {
                fragmentTransaction.show(frame3);
            }            
            break;            
        }    
        
        fragmentTransaction.commit();
                
    }
    
    private void clearSelectedState() {
        button1.setSelected(false);
        button2.setSelected(false);
        button3.setSelected(false);
    }

    @Override
    public void onClick(View v) {        
        hideFragment(fragmentTransaction);
        
        switch (v.getId()) {
        case R.id.button1:
            setTabSelected(TAB_INDEX1);
            break;
        case R.id.button2:
            setTabSelected(TAB_INDEX2);
            break;
        case R.id.button3:
            setTabSelected(TAB_INDEX3);
            break;    
        }    
        
         
    }
    
    

   
}
 

运行效果如下:

Android使用Fragment实现标签页

 

相关文章: