public class BaseFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("fragment:onCreateView", getView()); return null; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Log.d("fragment:onCreateView", getView()); findViewById(); } private ViewSwitcher[] titleSwitcher; /** * 初始化View */ protected void findViewById() { titleSwitcher = new ViewSwitcher[3]; titleSwitcher[0] = (ViewSwitcher) findViewById(R.id.app_title_left_switcher); titleSwitcher[1] = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher); titleSwitcher[2] = (ViewSwitcher) findViewById(R.id.app_title_right_switcher); } protected void setTitle(TitleRes leftTitle, TitleRes middleTitle, TitleRes rightTitle) { TitleRes[] reses = new TitleRes[] { leftTitle, middleTitle, rightTitle }; for (int i = 0; i < reses.length; i++) { TitleRes res = reses[i]; ViewSwitcher switcher = titleSwitcher[i]; if (res == null) { switcher.setVisibility(View.INVISIBLE); } else { switcher.setVisibility(View.VISIBLE); // 触发单击事件的View View clickView = null; // 文字 if (res.getType() == 0) { switcher.setDisplayedChild(0);// 显示TextView // Object resource = res.getResource(); String title = resource instanceof Integer ? getResources().getString((Integer) resource) : String.valueOf(resource); ((TextView) switcher.getChildAt(0)).setText(title); clickView = switcher.getChildAt(0); } else { switcher.setDisplayedChild(1);// 显示ImageView // int imageResource = (Integer) res.getResource(); ((ImageView) switcher.getChildAt(1)).setImageResource(imageResource); clickView = switcher.getChildAt(1); } // 设置title的单击事件 clickView.setOnClickListener(res.getClickListener()); } } } protected View findViewById(int id) { return getView().findViewById(id); } }
子类调用
public class HomeFragment extends BaseFragment implements View.OnClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.home, container, false); super.onCreateView(inflater, container, savedInstanceState); return layout; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); findViewById(R.id.home_title_sample).setOnClickListener(this); TitleRes left = null; TitleRes middle = new TitleRes("首页标题"); TitleRes right = null; setTitle(left, middle, right); }
但是BaseActivity和BaseFragment又没有共同的父类
这个时候需要采用 组合的方式进行代码重构
/** * 因为Fragment和Activity没有共同的父类 ,但是他们各自的父类里又有一些TitleBar相同的业务逻辑处理<br> * 所以我创建了该类 * * @author coffee <br> * 2015-12-24 下午10:26:10 */ public class TitleMgr { /** * TitleBar的上下文 */ private Context context; /** * TitleBar所在的顶层View容易 */ private View contentView; /** * 三个Title */ private ViewSwitcher[] titleSwitcher; public TitleMgr(Context context, View contenView) { this.context = context; this.contentView = contenView; } private Resources getResources() { return context.getResources(); } private View findViewById(int id) { return contentView.findViewById(id); } /** * 初始化title bar */ public void findViewById() { titleSwitcher = new ViewSwitcher[3]; titleSwitcher[0] = (ViewSwitcher) findViewById(R.id.app_title_left_switcher); titleSwitcher[1] = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher); titleSwitcher[2] = (ViewSwitcher) findViewById(R.id.app_title_right_switcher); } public void setTitle(TitleRes leftTitle, TitleRes middleTitle, TitleRes rightTitle) { TitleRes[] reses = new TitleRes[] { leftTitle, middleTitle, rightTitle }; for (int i = 0; i < reses.length; i++) { TitleRes res = reses[i]; ViewSwitcher switcher = titleSwitcher[i]; if (res == null) { switcher.setVisibility(View.INVISIBLE); } else { switcher.setVisibility(View.VISIBLE); // 触发单击事件的View View clickView = null; // 文字 if (res.getType() == 0) { switcher.setDisplayedChild(0);// 显示TextView // Object resource = res.getResource(); String title = resource instanceof Integer ? getResources().getString((Integer) resource) : String.valueOf(resource); ((TextView) switcher.getChildAt(0)).setText(title); clickView = switcher.getChildAt(0); } else { switcher.setDisplayedChild(1);// 显示ImageView // int imageResource = (Integer) res.getResource(); ((ImageView) switcher.getChildAt(1)).setImageResource(imageResource); clickView = switcher.getChildAt(1); } // 设置title的单击事件 clickView.setOnClickListener(res.getClickListener()); } } } }
public class BaseFragment extends Fragment { private TitleMgr titleMgr; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("fragment:onCreateView", getView()); return null; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Log.d("fragment:onCreateView", getView()); // titleMgr = new TitleMgr(getActivity(), getView()); titleMgr.findViewById(); } protected void setTitle(TitleRes leftTitle, TitleRes middleTitle, TitleRes rightTitle) { titleMgr.setTitle(leftTitle, middleTitle, rightTitle); }
public abstract class BaseActivity extends Activity { private TitleMgr titleMgr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMgr.push(this); findViewById(); } /** * 初始化View */ protected void findViewById() { titleMgr = new TitleMgr(getContext(), findViewById(android.R.id.content)); titleMgr.findViewById(); } protected void setTitle(TitleRes leftTitle, TitleRes middleTitle, TitleRes rightTitle) { titleMgr.setTitle(leftTitle, middleTitle, rightTitle); }
我把业务逻辑抽出来放在单独写好的TitleMgr类中,然后BaseActivity和BaseFragment采用组合?也算是代理模式实现吧