android View.OnClickListener条件スケジューラをCommandモードに再構築


今日は会社の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_offine_download) {
		down();
	    } else if (viewId == R.id.information_menu_child_custom_channel) {
		Intent cusIntent = new Intent();
		cusIntent.setClass(InformationBrowserActivity.this,
			CustomChannelActivity.class);
		Bundle b = new Bundle();
		int backFlag;
		if (moreNaviListLayout.getVisibility() == View.VISIBLE) {
		    backFlag = Config.NAVI_MORE;
		} else {
		    backFlag = Config.NAVI_FIRST;
		}
		b.putInt("backFlag", backFlag);
		cusIntent.putExtras(b);
		startActivity(cusIntent);
		layout.removeAllViews();
	    } else if (viewId == R.id.information_menu_child_favorite) {
		Intent favoriteIntent = new Intent(
			InformationBrowserActivity.this,
			MoreFavoriteActivity.class);
		startActivity(favoriteIntent);
	    } else if (viewId == R.id.information_menu_child_setting) {
		Intent settingIntent = new Intent(
			InformationBrowserActivity.this, PconlineSetting.class);
		startActivity(settingIntent);
	    }/*
	      * else if (viewId == R.id.information_menu_child_select_city) {
	      * changeCity(); }
	      */else if (viewId == R.id.information_menu_child_search) {
		Intent searchIntent = new Intent(
			InformationBrowserActivity.this,
			InformationCategorySearchResultActivity.class);
		startActivity(searchIntent);
	    } /*
	       * else if (viewId == R.id.information_menu_child_exit) {
	       * onBackPressed(); }
	       */else if (viewId == R.id.information_menu_top_layout) {
		mPopupWindow.dismiss();
	    }
	    mPopupWindow.dismiss();
	}
    };
再構築後のコード:
    private void pull_to_refresh() {
	boolean isHanlderForFooterViewInVisible = false;
	refresh(isHanlderForFooterViewInVisible);
    }

    private void setInInVisibleForFooterViews(boolean isVisible) {
	if (isVisible) {
	    if (footerViewIsVisible) {
		footLayout.setVisibility(View.GONE);
		footerViewIsVisible = false;
	    }
	    listView.showProgressBar();
	}
    }

    private void refresh(boolean isHanlderForFooterViewInVisible) {
	NavigationTextView textView;
	for (int i = 0; i < inforTitles.size(); i++) {
	    textView = inforTitles.get(i);
	    if (textView.isSelected()) {
		setViewBg(textView);
		setInInVisibleForFooterViews(isHanlderForFooterViewInVisible);
		inforAdapter.updateInforList(textView.getTitleUrl(), i, true);
		listView.setSelection(0);
		break;
	    }
	}
    }

    private Map<Integer, MenuChildListenerResponse> maps = new HashMap<Integer, MenuChildListenerResponse>();

    private void compositeCommandForMenuChildListener() {
	maps.put(R.id.information_menu_child_refresh, new MenuChildRefresh());
	maps.put(R.id.information_menu_child_offine_download,
		new MenuChildOfflineDownload());
	maps.put(R.id.information_menu_child_custom_channel,
		new MenuChildCustomChannel());
	maps.put(R.id.information_menu_child_favorite, new MenuChildFavorite());
	maps.put(R.id.information_menu_child_setting, new MenuChildSetting());
	maps.put(R.id.information_menu_child_search, new MenuChildSearch());
	maps.put(R.id.information_menu_top_layout, new MenuTopLayout());

    }

    private abstract class MenuChildListenerResponse {
	abstract void execute();

	void composeIntent(Class<?> c, Bundle b) {
	    Intent newIntent = new Intent(InformationBrowserActivity.this, c);
	    if (b != null)
		newIntent.putExtras(b);
	    startActivity(newIntent);
	}

    }

    private class MenuChildRefresh extends MenuChildListenerResponse {
	public void execute() {
	    boolean isHanlderForFooterViewInVisible = true;
	    refresh(isHanlderForFooterViewInVisible);
	}
    }

    private class MenuChildOfflineDownload extends MenuChildListenerResponse {

	@Override
	void execute() {
	    down();
	}
    }

    private class MenuChildCustomChannel extends MenuChildListenerResponse {
	void execute() {
	    Bundle b = new Bundle();
	    int backFlag;
	    if (moreNaviListLayout.getVisibility() == View.VISIBLE) {
		backFlag = Config.NAVI_MORE;
	    } else {
		backFlag = Config.NAVI_FIRST;
	    }
	    b.putInt("backFlag", backFlag);
	    composeIntent(CustomChannelActivity.class, b);
	    layout.removeAllViews();
	}

    }

    private class MenuChildFavorite extends MenuChildListenerResponse {
	void execute() {
	    composeIntent(MoreFavoriteActivity.class, null);
	}
    }

    private class MenuChildSetting extends MenuChildListenerResponse {
	void execute() {
	    composeIntent(PconlineSetting.class, null);
	}
    }

    private class MenuChildSearch extends MenuChildListenerResponse {
	void execute() {
	    composeIntent(InformationCategorySearchResultActivity.class, null);
	}
    }

    private class MenuTopLayout extends MenuChildListenerResponse {
	void execute() {
	    mPopupWindow.dismiss();
	}
    }

    private View.OnClickListener listener = new View.OnClickListener() {
	public void onClick(View v) {
	    int viewId = v.getId();
	    maps.get(viewId).execute();
	    new MenuTopLayout().execute();
	}
    };
 
 
10 , ? View View OnClickListener , MenuChildListenerResponse execute , Map View id , View , 。 , Pull Up , 。   Switch-case Command , if-else,switch-case, , 。

, !