Fragment_簡單框架實現QQ滑動
阿新 • • 發佈:2019-01-10
FragmentLayout與Fragment簡單框架
package com.zzf.qq;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private FragmentManager manager;
private Frag_01 frag_01;
private Frag_02 frag_02;
private Frag_03 frag_03;
private Frag_04 frag_04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取Fragment管理者
manager = getFragmentManager();
// 開啟事務
FragmentTransaction transaction = manager.beginTransaction();
// 把fragment物件新增到事務中
frag_01 = new Frag_01();
frag_02 = new Frag_02();
frag_03 = new Frag_03();
frag_04 = new Frag_04();
// (幀佈局id,fragment物件)
transaction.add(R.id.main_frag, frag_01);
transaction. add(R.id.main_frag, frag_02);
transaction.add(R.id.main_frag, frag_03);
transaction.add(R.id.main_frag, frag_04);
// 顯示和隱藏
transaction.show(frag_01).hide(frag_02).hide(frag_03).hide(frag_04);
// 提交
transaction.commit();
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
//預設選擇第一個radiobutton
radioGroup.getChildAt(0).getId();
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//開啟一個新的事物
FragmentTransaction qq = manager.beginTransaction();
//根據下標顯示與隱藏
switch (checkedId) {
case R.id.radio0:
qq.show(frag_01).hide(frag_02).hide(frag_03).hide(frag_04);
break;
case R.id.radio1:
qq.show(frag_02).hide(frag_01).hide(frag_03).hide(frag_04);
break;
case R.id.radio2:
qq.show(frag_03).hide(frag_02).hide(frag_01).hide(frag_04);
break;
case R.id.radio3:
qq.show(frag_04).hide(frag_02).hide(frag_03).hide(frag_01);
break;
}
//提交新的事物
qq.commit();
}
});
}
}
<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/main_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<RadioGroup
android:gravity="center"
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/select_01"
android:gravity="center"
android:text="訊息" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/select_01"
android:gravity="center"
android:text="聯絡人" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/select_01"
android:gravity="center"
android:text="看點" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/select_01"
android:gravity="center"
android:text="動態" />
</RadioGroup>
</LinearLayout>