Android開發 使用Fragment做界面切換
1、準備需要切換的界面,建立fragment01、02、03,建立三個類進行界面的跳轉
2、activity_main.xml
<FrameLayout
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/weixin"
android:gravity="center"
android:text="A" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/tongxunlu"
android:gravity="center"
android:text="B" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/weixin"
android:gravity="center"
android:text="C" />
</RadioGroup>
3、MainActivity
private FrameLayout frameLayout;
private RadioGroup group;
private ArrayList list;
private Fragment01 fragment01;
private Fragment fragment02, fragment03;
private FragmentManager manager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.interface_switch);
//初始化控件
frameLayout = findViewById(R.id.pager);
group = findViewById(R.id.radiogroup);
//擷取事務
manager = getSupportFragmentManager();
//開啟事務
FragmentTransaction transaction = manager.beginTransaction();
fragment01 = new Fragment01();
fragment02 = new Fragment02();
fragment03 = new Fragment03();
//事務添加
transaction.add(R.id.pager, fragment01);
transaction.add(R.id.pager, fragment02);
transaction.add(R.id.pager, fragment03);
//展示一個界面隐藏另外兩個
transaction.show(fragment01).hide(fragment02).hide(fragment03);
//送出事務
transaction.commit();
hideBottomUIMenu();
group.check(group.getChildAt(0).getId());
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
//開啟事務
FragmentTransaction transaction1 = manager.beginTransaction();
//選擇
switch (i) {
case R.id.radioButton:
//展示一個界面,隐藏另外兩個界面
transaction1.show(fragment01).hide(fragment02).hide(fragment03);
break;
case R.id.radioButton2:
transaction1.show(fragment02).hide(fragment01).hide(fragment03);
break;
case R.id.radioButton3:
transaction1.show(fragment03).hide(fragment02).hide(fragment01);
break;
}
hideBottomUIMenu();
//送出事務
transaction1.commit();
}
});
}
//隐藏虛拟按鍵
protected void hideBottomUIMenu() {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}