package com.bwei.cartoon;
import com.bwei.cartoon.fragment.Fragment1;
import com.bwei.cartoon.fragment.Fragment2;
import com.bwei.cartoon.fragment.Fragment3;
import com.slidingmenu.lib.SlidingMenu;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private RadioButton r1;
private RadioGroup rg;
private FragmentManager manager;
private Fragment1 f1;
private Fragment2 f2;
private Fragment3 f3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件
r1 = (RadioButton) findViewById(R.id.r1);
rg = (RadioGroup) findViewById(R.id.rg);
r1.setChecked(true);
// 创建一个管理类
manager = getSupportFragmentManager();
// 创建一个事物
FragmentTransaction transaction = manager.beginTransaction();
// 实例化fragment类
f1 = new Fragment1();
f2 = new Fragment2();
f3 = new Fragment3();
// 将fragment对象添加到事物中
transaction.add(R.id.fl, f1, "f1");
transaction.add(R.id.fl, f2, "f2");
transaction.add(R.id.fl, f3, "f3");
// 提交
transaction.commit();
// 单选按钮的点击事件,点击哪个按钮就显示相应的fragment
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.r1:
switchFragment("f1");
break;
case R.id.r2:
switchFragment("f2");
break;
case R.id.r3:
switchFragment("f3");
break;
default:
break;
}
}
});
//默认显示第一个fragment
switchFragment("f1");
//创建侧滑对象
SlidingMenu menu = new SlidingMenu(this);
//从左边划
menu.setMode(SlidingMenu.LEFT);
// 设置触摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 设置滑动菜单视图的宽度
menu.setBehindOffsetRes(R.dimen.activity_cehua);
// 设置渐入渐出效果的值
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//侧滑视图
menu.setMenu(R.layout.cehua);
}
private void switchFragment(String tag) {
//事物
FragmentTransaction transaction = manager.beginTransaction();
if("f1".equals(tag)){
transaction.show(f1);
transaction.hide(f2);
transaction.hide(f3);
}else if("f2".equals(tag)){
transaction.show(f2);
transaction.hide(f1);
transaction.hide(f3);
}else if("f3".equals(tag)){
transaction.show(f3);
transaction.hide(f1);
transaction.hide(f2);
}
transaction.commit();
}
}
Fragment切换
最新推荐文章于 2024-06-26 15:22:32 发布