在同一个Activity中切换界面(UI)。
MiddleManager.getInstance().changeUI(new SecondUI(MiddleManager.getInstance().getContext()));
<span style="white-space: pre;">MiddleManager.getInstance().changeUI(new SecondUI(MiddleManager.getInstance().getContext()));</span>
某UI代码:
public class FirstUI extends BaseUI{
<span style="white-space:pre"> </span><span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public FirstUI(Context context) {
<span style="white-space:pre"> </span>super(context);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 获取需要加载的View
<span style="white-space:pre"> </span> * @return
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public View getView() {
<span style="white-space:pre"> </span>//简单界面
<span style="white-space:pre"> </span>TextView tv = new TextView(context);
<span style="white-space:pre"> </span>LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
<span style="white-space:pre"> </span>tv.setLayoutParams(lp);
<span style="white-space:pre"> </span>tv.setBackgroundColor(Color.BLUE);
<span style="white-space:pre"> </span>tv.setText("第一个界面");
<span style="white-space:pre"> </span>return tv;
<span style="white-space:pre"> </span>}
}
/**
* 切换界面
* @param ui
*/
public void changeUI1(BaseUI ui) {
// FadeUtil.FadeOut(child1, 1000);
middle.removeAllViews();
View child = ui.getView();
middle.addView(child);
// FadeUtil.FadeIn(child, 1000, 1000);
child.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.ia_view_change));
}
public Context getContext() {
return middle.getContext();
}
每次改变UI都会new一个UI,UI里的View也是new出来的
优化后的代码:
/**
* 切换界面:解决每次点击重复创建(new)的问题
* @param ui
*/
//储存打开过的UI
private Map<String, BaseUI> VIEWCACHE = new HashMap<String, BaseUI>();
//当前UI
private BaseUI currentUI;
public void changeUI(Class<? extends BaseUI> targetClazz) {
//如果要打开的是当前UI则不作处理
if(currentUI != null && currentUI.getClass() == targetClazz) {
return;
}
BaseUI targetUI = null;
//判断是否创建过
String key = targetClazz.getSimpleName();
if(VIEWCACHE.containsKey(key)) {
//创建了,重用
targetUI = VIEWCACHE.get(key);
} else {
//否则,创建之
try {
Constructor<? extends BaseUI> constructor = targetClazz.getConstructor(Context.class); //获取构造器
targetUI = constructor.newInstance(getContext()); //获取构造含参数实例
VIEWCACHE.put(key, targetUI);
} catch (Exception e) {
throw new RuntimeException("构造出问题");
}
}
Log.i(TAG, targetUI.toString());
// FadeUtil.FadeOut(child1, 1000);
middle.removeAllViews();
View child = targetUI.getView();
middle.addView(child);
FadeUtil.FadeIn(child, 1000, 1000);
child.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.ia_view_change));
currentUI = targetUI;
}
优化后的某UI:
public class SecondUI extends BaseUI {
TextView tv;
public SecondUI(Context context) {
super(context);
init();
}
/**
* 放在构造器中,这样view只会new一次,因为UI只创建一次
*/
private void init() {
tv = new TextView(context);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tv.setLayoutParams(lp);
tv.setBackgroundColor(Color.RED);
tv.setText("第2个界面");
}
/**
* 获取需要加载的UI
* @return
*/
public View getView() {
//简单界面
return tv;
}
}