今天试着将公司的一大堆的android View.OnClickListener条件调度程序重构为Command模式,经测试,程序没有出错,代码的可读性高了,而且消除了不少重复代码,先将原始代码和重构后的代码贴至如下:
1.原先代码:
private void pull_to_refresh() {
NavigationTextView textView;
for (int i = 0; i < inforTitles.size(); i++) {
textView = inforTitles.get(i);
if (textView.isSelected()) {
setViewBg(textView);
/*
* if(footerViewIsVisible){ footLayout.setVisibility(View.GONE);
* footerViewIsVisible=false; }
*/
inforAdapter.updateInforList(textView.getTitleUrl(), i, true);
listView.setSelection(0);
break;
}
}
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.information_menu_child_refresh) {
NavigationTextView textView;
for (int i = 0; i < inforTitles.size(); i++) {
textView = inforTitles.get(i);
if (textView.isSelected()) {
setViewBg(textView);
if (footerViewIsVisible) {
footLayout.setVisibility(View.GONE);
footerViewIsVisible = false;
}
listView.showProgressBar();
inforAdapter.updateInforList(textView.getTitleUrl(), i,
true);
listView.setSelection(0);
break;
}
}
} else if (viewId == R.id.information_menu_child_of