学习笔记-切换界面时显示内容不重复创建

在同一个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;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值