fragment 显示隐藏
package com.bw.com.zhangsan;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import com.bw.com.zhangsan.frag.BlankFragment;
import com.bw.com.zhangsan.frag.BlankFragment2;
import com.bw.com.zhangsan.frag.BlankFragment3;
import com.bw.com.zhangsan.frag.BlankFragment4;
import com.bw.com.zhangsan.frag.BlankFragment5;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private FragmentManager manager;
private FrameLayout frag_layout;
private DrawerLayout draw;
private ListView leftdraw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
//获取事务管理者
manager = getSupportFragmentManager();
//开启事务
final FragmentTransaction transaction = manager.beginTransaction();
final BlankFragment frag01=new BlankFragment();
final BlankFragment2 frag02=new BlankFragment2();
final BlankFragment3 frag03=new BlankFragment3();
final BlankFragment4 frag04=new BlankFragment4();
final BlankFragment5 frag05=new BlankFragment5();
transaction.add(R.id.frag_layout,frag01);
transaction.add(R.id.frag_layout,frag02);
transaction.add(R.id.frag_layout,frag03);
transaction.add(R.id.frag_layout,frag04);
transaction.add(R.id.frag_layout,frag05);
transaction.show(frag01).hide(frag02).hide(frag03).hide(frag04).hide(frag05);
transaction.commit();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction beginTransaction = manager.beginTransaction();
switch (checkedId){
case R.id.buttn1:
beginTransaction.show(frag01).hide(frag02).hide(frag03).hide(frag04).hide(frag05);
break;
case R.id.buttn2:
beginTransaction.show(frag02).hide(frag01).hide(frag03).hide(frag04).hide(frag05);
break;
case R.id.buttn3:
beginTransaction.show(frag03).hide(frag01).hide(frag02).hide(frag04).hide(frag05);
break;
case R.id.buttn4:
beginTransaction.show(frag04).hide(frag01).hide(frag02).hide(frag03).hide(frag05);
break;
case R.id.buttn5:
beginTransaction.show(frag05).hide(frag01).hide(frag02).hide(frag03).hide(frag04);
break;
default:
draw.closeDrawer(checkedId);
}
beginTransaction.commit();
}
});
}
public void initview(){
radioGroup = findViewById(R.id.radio_group);
frag_layout = findViewById(R.id.frag_layout);
draw = findViewById(R.id.draw);
leftdraw = findViewById(R.id.left_draw);
}
}
fragment 隐藏 显示布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frag_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
>
<RadioButton
android:id="@+id/buttn1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/app_bu1"
android:gravity="center"
android:button="@null"
android:background="@drawable/sele_butn"
android:drawableTop="@drawable/ic_butn"/>
<RadioButton
android:id="@+id/buttn2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/app_bun2"
android:gravity="center"
android:button="@null"
android:background="@drawable/sele_butn"
android:drawableTop="@drawable/ic_butn"/>
<RadioButton
android:id="@+id/buttn3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/app_bun3"
android:button="@null"
android:gravity="center"
android:background="@drawable/sele_butn"
android:drawableTop="@drawable/ic_butn"/>
<RadioButton
android:id="@+id/buttn4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/app_bun4"
android:button="@null"
android:gravity="center"
android:background="@drawable/sele_butn"
android:drawableTop="@drawable/ic_butn"/>
<RadioButton
android:id="@+id/buttn5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/app_bun5"
android:button="@null"
android:gravity="center"
android:background="@drawable/sele_butn"
android:drawableTop="@drawable/ic_butn"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/left"
android:layout_width="140dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start|left">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_02"/>
<ListView
android:id="@+id/left_draw"
android:layout_width="140dp"
android:layout_height="match_parent"
android:background="#ff0"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>