LookAround的主界面框架采用SlidingFragmentActivity,如上图,左边为侧滑菜单Fragment,右边为内容Fragment。
左边侧滑Fragment初始化如下:
/**
* 初始化侧滑菜单
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private voidinitSlideMenu(){
// log.e("MainLookAroundActivityinitSlideMenu");
SlidingMenu sm = getSlidingMenu();
sm.setMode(SlidingMenu.LEFT);
setBehindContentView(R.layout.left_menu_frame);
sm.setSlidingEnabled(true);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.left_menu_frame, newNavigationFragment())
.commit();
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setBehindScrollScale(0);
sm.setFadeDegree(0.25f);
}
右边的内容Fragment通过FragmentControlCenter进行控制。每个不同的主题,比如“星座”对应一个Fragment,FragmentControlCenter对主题和对应的Fragment进行键值进行Map存储,根据主题选择对应的Fragment切换显示。核心代码如下:
mContentFragment = mControlCenter.getCommonFragmentEx(mDataList.get(0));
switchContent(mContentFragment);
/**
* 切换内容
* @paramfragment
*/
public voidswitchContent(final CommonFragmentEx fragment) {
mContentFragment =fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContentFragment)
.commit();
Handler h = newHandler();
h.postDelayed(newRunnable() {
public void run() {
getSlidingMenu().showContent();
}
}, 50);
// 当前标题改为子Fragment标题
mTitleTextView.setText(mContentFragment.getData().mTitle);
}
单击内容Fragment中列表,跳转到内容界面。其他界面上无复杂。
还有个自定义的对话框工厂说明下。
public classDialogEntity {
public Context context;
public String title = "";
public String message = "";
public String btnSure = "";
public String btnCancel = "";
}
public interfaceIDialogInterface {
public void onSure();
public voidonCancel();
}
/**
* 对话框工厂
* @author Administrator
*
*/
public classDialogBuilder {
public DialogBuilder() {
}
/**
* 创建提示对话框
* @param context
* @param title
* @param message
* @paramlistener
* @return
*/
public static DialogbuildTipDialog(Context context, String title, String message, finalIDialogInterface listener){
DialogEntity entity = newDialogEntity();
entity.context = context;
entity.title = title;
entity.message =message;
entity.btnSure = "确定";
return buildTipDialog(entity,listener);
}
/**
* 创建一个正常对话框
* @param context
* @param title
* @param message
* @paramlistener
* @return
*/
public static DialogbuildNormalDialog(Context context, String title, String message, finalIDialogInterface listener){
DialogEntity entity = newDialogEntity();
entity.context = context;
entity.title = title;
entity.message =message;
entity.btnSure = "确定";
entity.btnCancel = "取消";
return buildNormalDialog(entity,listener);
}
/**
* 创建一个提示对话框
* @param entity
* @paramlistener
* @return
*/
private static DialogbuildTipDialog(DialogEntity entity, finalIDialogInterface listener){
AlertDialog.Builder builder = newAlertDialog.Builder(entity.context);
builder.setTitle(entity.title);
builder.setMessage(entity.message);
builder.setNegativeButton(entity.btnSure, newOnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
if (listener!= null){
listener.onSure();
}
}
});
returnbuilder.create();
}
/**
* 创建一个正常的对话框
* @param entity
* @paramlistener
* @return
*/
private static DialogbuildNormalDialog(DialogEntity entity, finalIDialogInterface listener){
AlertDialog.Builder builder = newAlertDialog.Builder(entity.context);
builder.setTitle(entity.title);
builder.setMessage(entity.message);
builder.setNegativeButton(entity.btnSure, newOnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
if (listener!= null){
listener.onSure();
}
}
});
builder.setPositiveButton(entity.btnCancel, newOnClickListener(){
@Override
public voidonClick(DialogInterface dialog, int which) {
if (listener!= null){
listener.onCancel();
}
}
});
returnbuilder.create();
}
}
界面中还使用到了自定义下拉刷新ListView;开源库satellite-menu,实现菜单的展开、聚合;使用了注册控件库roboguice.jar,简化了控件的申明,等等。