public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
private RadioButtonrb;
private RadioButtonrb1;
private BFragmentbFragment;
private FragmentManagermanager;
private AFragmentaFragment;
private final StringSHOUYETAG ="f1";
private FrameLayouttv_fragment;
private FragmentTransactiontransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv_fragment = (FrameLayout) findViewById(R.id.tv_fragment);
tv_fragment.setOnClickListener(this);
rb = (RadioButton) findViewById(R.id.rb);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb.setOnClickListener(this);
rb1.setOnClickListener(this);
manager = getSupportFragmentManager();
aFragment = (AFragment)manager.findFragmentByTag(SHOUYETAG);
FragmentTransaction fragmentTransaction =manager.beginTransaction();
if (aFragment ==null) {
AFragment aFragment =new AFragment();
fragmentTransaction.add(R.id.tv_fragment, aFragment,SHOUYETAG);
fragmentTransaction.commit();
}
}
private void initFragment(Fragment fragment) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
transaction = supportFragmentManager.beginTransaction();
transaction.replace(R.id.tv_fragment, fragment);
transaction.commit();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.rb:
aFragment =new AFragment();
initFragment(aFragment);
break;
case R.id.rb1:
bFragment =new BFragment();
initFragment(bFragment);
break;
}
}
}
奇迹布局
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/fg" />
android:id="@+id/fg"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="@id/rg"
android:background="#E3E3E3" />
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="@dimen/dp_5"
android:orientation="horizontal">
android:id="@+id/rb1"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white"
android:button="@null"
android:drawableTop="@drawable/button_shouye"
android:gravity="center"
android:text="首页" />
android:id="@+id/rb2"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:button="@null"
android:drawableTop="@drawable/button_souke"
android:gravity="center"
android:text="搜课" />
android:id="@+id/rb3"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:button="@null"
android:gravity="center"
android:text="今日直播" />
android:id="@+id/rb4"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:button="@null"
android:drawableTop="@drawable/button_cache"
android:gravity="center"
android:text="缓存" />
android:id="@+id/rb5"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:button="@null"
android:drawableTop="@drawable/button_my"
android:gravity="center"
android:text="我的" />
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:button="@null"
android:drawableTop="@drawable/button_live" />