Fragment

Fragment

>片段,碎片  


1. 定義某一個片段的界面 繼承Fragment類
public class BlueToothFragment extends Fragment {}
2. 重寫Fragment裏面的方法
顯示Fragment的ui,把佈局文件轉化成view對象
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.blue_tooth_layout, null);
}
3. 獲取Fragment管理器
fm = getFragmentManager();
4. 動態的修改界面
f1 = new BlueToothFragment();//先把要顯示的f1new出來
FragmentTransaction ft = fm.beginTransaction();//開啓事務
ft.replace(R.id.fl_container, f1);

ft.commit();


activity_main佈局

<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"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="0dip" >
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#22000000"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting01"
            android:text="藍牙設置" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting02"
            android:text="聲音設置" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setting03"
            android:text="顯示設置" />
    </LinearLayout>

</LinearLayout>
public class MainActivity extends Activity {
	private FragmentManager fm;
	BlueToothFragment f1;
	SoundFragment f2;
	ShowFragment f3;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fm = getFragmentManager();
		initFragment();
		//默認加載 BlueToothFragment 
		FragmentTransaction ft = fm.beginTransaction();//開啓事務
		ft.replace(R.id.fl_container, f1);
		ft.commit();
	}

	private void initFragment() {
		f1 = new BlueToothFragment();
		f2 = new SoundFragment();
		f3 = new ShowFragment();
	}

	//--------------- 3個點擊事件-------------------------
	//點擊藍牙
	public void setting01(View view) {
		FragmentTransaction ft = fm.beginTransaction();//這裏要重新開啓,不能在上面定義成成員
		ft.replace(R.id.fl_container, f1);
		ft.commit();
	}
	//點擊聲音
	public void setting02(View view) {
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f2);
		ft.commit();
	}
	//點擊顯示
	public void setting03(View view) {
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f3);
		ft.commit();
	}

}
public class BlueToothFragment extends Fragment {
	//顯示Fragment的ui的
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.blue_tooth_layout, null);
	}
}

Fragment的向下兼容
>使用support-v4的jar包
>1. MainActivity extends FragmentActivity
>2. 所有的fragment的導包, android.support.v4.app.Fragment
>3. getSupportFragmentManager();
>4. 注意兩個導包
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {
	private FragmentManager fm;
	BlueToothFragment f1;
	ShowFragment f3;
	SoundFragment f2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//爲了向下兼容採用v4包裏面的FragmentManager
		fm = getSupportFragmentManager();
		initFragment();
		//事務的概念
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f1);
		ft.commit();//保證了 要麼同時成功,要麼同時失敗
	}

	private void initFragment() {
		f1 = new BlueToothFragment();
		f2 = new SoundFragment();
		f3 = new ShowFragment();
	}

	
	public void setting01(View view) {
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f1);
		ft.commit();
	}
	
	public void setting02(View view) {
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f2);
		ft.commit();
	}
	
	public void setting03(View view) {
		
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.fl_container, f3);
		ft.commit();
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章