写了简单的Setting:左边屏幕功能键,右边具体的功能实现界面,实现方式通过Fragment来实现。
具体的需要当前的两个fragment来实现数据共享 ,事件共享。在activity中实现其中一个fragment的接口回掉,在另外一个fragment用来展现当前回掉事件会触发的功能。
回掉接口:
private Callbacks mCallbacks;
// 定义一个回调接口,该Fragment所在Activity需要实现该接口
// 该Fragment将通过该接口与它所在的Activity交互
public interface Callbacks {
public void onItemSelected(Integer id);
}
fragment中的方法在对应的activity中回掉的同时,必须让该fragment附着在对应的activity中:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("BookListFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks) activity;
}
在activity中根据回掉的事件,来展现不同的fragment,用于显示:
@Override
public void onItemSelected(Integer id) {
if (nowFragmentID != 0 && nowFragmentID == id) { //用户直接单击fragment菜单多次,直接返回
return;
}
switch (id) {
case R.id.setting_wify_setting:
tDFragment = new WiFyFragment();
break;
case R.id.setting_twodenim:
tDFragment = new TwoDimensionFragment();
break;
// case R.id.setting_bluetooth_setting: //蓝牙设置
// tDFragment = new BlueToothFragment();
// break;
case R.id.rlabout: //关于模块
tDFragment = new AboutFragment();
break;
case R.id.rlappdown:
tDFragment = new DownDimensionFragment();
break;
case R.id.setting_usercount:
break;
case R.id.setting_bless:
break;
default:
break;
}
//替换内容界面 getFragmentManager().beginTransaction().replace(R.id.settingdimension, tDFragment).commit();
FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
beginTransaction.replace(R.id.settingdimension, tDFragment);
beginTransaction.commit();
nowFragmentID = id;
}