接着上一篇文章使用Vitamio库打造万能播放器(二)
细心的可以发现,上一篇文章中VideoPlayerActivity是继承至BaseActivity的,BaseActivity相信大家都不陌生了,每个项目都会有一个定制的基类Activity,不同的开发者所写的BaseActivity也都不也一样,但是目的都是一样的,就是:
1.规范所有Activity的代码编写;
2.将公共的方法都写在BaseActivity上,节省编码量;
3.方便阅读和维护;
这里本不是介绍BaseActivity该如何自定制的,但是为了方便阅读上一篇文章,这里还是贴出代码介绍下.
BaseActivity布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".module.main.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--自定义的titleBar--> <mchenys.net.csdn.blog.myplayer.common.widget.TitleBar android:id="@id/titleBar" android:layout_width="match_parent" android:layout_height="50dp"/> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--装载子View的根容器--> <FrameLayout android:id="@id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent"/> <!--自定义的加载圈--> <mchenys.net.csdn.blog.myplayer.common.widget.LoadView android:id="@id/loadView" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/> </FrameLayout> </LinearLayout> </RelativeLayout>
BaseActivity代码如下:
package mchenys.net.csdn.blog.myplayer.common.base; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageButton; import mchenys.net.csdn.blog.myplayer.R; import mchenys.net.csdn.blog.myplayer.common.interfaces.UIOperation; import mchenys.net.csdn.blog.myplayer.common.utils.UIUtils; import mchenys.net.csdn.blog.myplayer.common.widget.LoadView; import mchenys.net.csdn.blog.myplayer.common.widget.TitleBar; /** * Created by mChenys on 2016/1/14. */ public abstract class BaseActivity extends FragmentActivity implements UIOperation { protected LoadView mLoadView;//加载视图 protected View mContentView;//子View的布局 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initBase(); initView(); initListener(); initData(); } /** * 初始化基类 */ private void initBase() { setContentView(R.layout.activity_base); setTitleBar((TitleBar) findView(R.id.titleBar));//设置标题 mLoadView = findView(R.id.loadView);//加载视图 FrameLayout flContent = findView(R.id.fl_content);//子View的父布局 if (null != getLayoutResId()) { mContentView = View.inflate(this,getLayoutResId(),null); if (null != mContentView) { flContent.removeAllViews(); flContent.addView(mContentView);//添加子视图 //给所有的Button/ImageButton添加点击事件 findButtonSetOnClickListener(mContentView); } } mLoadView.setClickReLoadListener(new LoadView.LoadViewReloadListener() { @Override public void reLoad() { reLoadData();//重新加载数据 } }); } /** * 设置顶部标题栏 * * @param titleBar */ protected abstract void setTitleBar(TitleBar titleBar); /** * 给当前布局的所有button,imageButton设置点击事件 * * @param view */ private void findButtonSetOnClickListener(View view) { if (view instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) view; int count = viewGroup.getChildCount(); for (int i = 0; i < count; i++) { View child = viewGroup.getChildAt(i); if (child instanceof ViewGroup) { findButtonSetOnClickListener(child); } else if (child instanceof Button || child instanceof ImageButton) { child.setOnClickListener(this); } } } } /** * 自动类型转换查找控件 * * @param id * @param <T> * @return */ public <T> T findView(int id) { return (T) super.findViewById(id); } /** * 是否是主页面 * * @return */ protected abstract boolean isHomePage(); private long exitTime; @Override public void onBackPressed() { if (isHomePage()) { if (System.currentTimeMillis() - exitTime > 2000) { exitTime = System.currentTimeMillis(); showToast("再按一次退出应用"); } else { android.os.Process.killProcess(android.os.Process.myPid()); } } else { finish(); this.overridePendingTransition(R.anim.left_in, R.anim.right_out); } } /** * 显示在屏幕中间的土司 * * @param text */ public void showToast(String text) { UIUtils.showToast(text); } /** * 添加Fragment到R.id.fl_content位置 * * @param baseFragment * @param tag * @param bundle */ protected void addFragment(BaseFragment baseFragment, String tag, Bundle bundle) { if (null != bundle) { baseFragment.setArguments(bundle); } getSupportFragmentManager().beginTransaction() .replace(R.id.fl_content, baseFragment, tag) .commitAllowingStateLoss(); } }
UIOperation 接口代码如下:
package mchenys.net.csdn.blog.myplayer.common.interfaces; import android.view.View; /** * Created by mChenys on 2016/1/16. */ public interface UIOperation extends View.OnClickListener { /** * 返回布局的资源文件id * * @return */ Integer getLayoutResId(); /** * 初始化View */ void initView(); /** * 初始化监听 */ void initListener(); /** * 初始化数据 */ void initData(); /** * 重新加载数据 */ void reLoadData(); }