首页 > 作文

基础框架之BaseActivity

更新时间:2023-04-04 02:58:51 阅读: 评论:0

import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.MenuItem;import android.view.WindowManager;import com.trello.rxlifecycle2.LifecycleTransformer;import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;import javax.inject.Inject;import androidx.annotation.LayoutRes;import androidx.annotation.Nullable;import androidx.appcompat.widget.Toolbar;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentTransaction;import butterknife.BindView;import butterknife.ButterKnife;import iptv.com.quick.LocalApplication;import iptv.com.quick.R;import iptv.com.quick.injector.components.ApplicationComponent;import iptv.com.quick.utils.Logger;import iptv.com.quick.widget.EmptyLayout;public abstract class BaActivity<T extends IBaPrenter> extends RxAppCompatActivity implements IBaView, EmptyLayout.OnRetryListener, Handler.Callback {/** * 把 EmptyLayout 放在基类统一处理,@Nullable 表明 View 可以为 null,详细可看 ButterKnife * 注意,资源的ID一定要一样 */@Nullable@BindView(R.id.empty_layout)protected EmptyLayout mEmptyLayout;/** * 把 Prenter提取到基类需要配合基类的initInjector() 进行注入,如果基层这个基类则必定要提供一个Prenter注入方法, * 该APP把所有Prenter 都是在Module 提供注入实现的, 也可以选择提供不带Prenter 的基类 */@Injectprotected T mPrenter;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);getSupportActionBar().hide();//设置为隐藏ActionBar和全屏显示// getWindow().tSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);getWindow().tStatusBarColor(getResources().getColor(R.color.color_FF282C33));}// getWindow().tFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//getWindow().tFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);tContentView(attachLayoutRes());ButterKnife.bind(this);initInjector();initViews(savedInstanceState);updateViews(fal);}/** * 绑定布局文件 * * @return */@LayoutResprotected abstract int attachLayoutRes();/** * Dagger 注入 */protected abstract void initInjector();/** * 初始化视图 * * @param savedInstanceState */protected abstract void initViews(Bundle savedInstanceState);/** * 接下来开始为更新视图进行从操作,如开始请求网络数据 * * @param isRefresh */protected abstract void updateViews(boolean isRefresh);@Overridepublic void showLoading() {if (mEmptyLayout != null) {mEmptyLayout.tEmptyStatus(EmptyLayout.STATUS_LOADING);}}@Overridepublic void showLoading(int loadingIv) {if (mEmptyLayout != null) {mEmptyLayout.tEmptyStatus(loadingIv,EmptyLayout.STATUS_LOADING_IV);}}@Overridepublic void hideLoading() {if (mEmptyLayout != null) {mEmptyLayout.hide();}}@Overridepublic void showNetError(String msg) {if (mEmptyLayout != null) {mEmptyLayout.tEmptyStatus(EmptyLayout.STATUS_NO_NET);mEmptyLayout.tRetryListener(this);}}@Overridepublic <T> LifecycleTransformer<T> bindToLife() {return this.<T>bindToLifecycle();}/** * 显示网络错误时,点击重试监听 */@Overridepublic void onRetry() {updateViews(fal);}@Overrideprotected void onResume() {super.onResume();Logger.i(Logger._TJ, "onResume " + getClass().getSimpleName() +" "+  getClass().hashCode());}@Overrideprotected void onDestroy() {su耿耿星河per.onDestroy();if (mEmptyLayout != null) mEmptyLayout.onDestroy();Logger.i(Logger._TJ, "onDestroy " + getClass().getSimpleName() +" "+  getClass().hashCode());}@Overrideprotected void onPau() {super.onPau();Logger.i(Logger._TJ, "onPau " + getClass().getSimpleName() +" "+  getClass().hashCode());}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);Logger.i(Logger._TJ, "onNewIntent " + getClass().getSimpleName() +" "+  getClass().hashCode());}@Overrideprotected void onStart() {super.onStart();Logger.i(Logger._TJ, "onStart " + getClass().getSimple哪些植物吸甲醛Name() +" "+  getClass().hashCode());}/** * 获取 ApplicationComponent * * @return ApplicationComponent */protected ApplicationComponent getAppComponent() {return LocalApplication.getAppComponent();}/** * 初始化 toolbar * homeAsUpEnables为true时,如果toolbar没有设置navigationIcon属性,则默认显示系统默认返回按钮 * * @param toolbar * @param homeAsUpEnable * @param title */protected void initToolbar(Toolbar toolbar, boolean homeAsUpEnable, String title) {toolbar.tTitle(title);tSupportActionBar(toolbar);getSupportActionBar().tDisplayHomeAsUpEnabled(homeAsUpEnable);}protected void initToolbar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle) {initToolbar(toolbar, homeAsUpEnabled, getString(resTitle));}/** * android.R.id.home 就是 navigation·home * 参考getSupportActionBar().tDisplayHomeAsUpEnabled(homeAsUpEnable); * * @param item * @return */@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == android.R.id.home) {finish();return true;}return super.onOptionsItemSelected(item);}/** * 添加 Fragment 没有Tag值,不压入回退栈 * * @param containerViewId * @param fragment */protected void addFragment(int containerViewId, Fragment fragment) {FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();fragmentTransaction.add(containerViewId, fragment);fragmentTransaction.commit();}/** * 添加 Fragment 压入回退栈 * * @param containerViewId * @param fragment * @param tag */public void addFragment(int containerViewId, Fragment f英语分类ragment, String tag) {FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();// 设置Tag, 不然下面 findFragmentByTag(tag) 找不到fragmentTransaction.add(containerViewId, fragment, tag);fragmentTransaction.addToBackStack(tag);fragmentTransaction.commit();}/** * 替换 Fragment * * @param containerViewId * @param fragment */public void replaceFragment(int containerViewId, Fragment fragment) {FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();fragmentTransaction.replace(containerViewId, fragment);fragmentTransaction.tTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);fragmentTransaction.commit();}学年总结大二/** * 替换 Fragment * * @param containerViewId * @param fragment * @param tag */public void replaceFragment(int containerViewId, Fragment fragment, String tag) {if (getSupportFragmentManager().findFragmentByTag(tag) == null) {FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();//设置TagfragmentTransaction.replace(containerViewId, fragment, tag);fragmentTransaction.tTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);// 这里也要设置tag,通过这里保存的tag找到对应的fragmentfragmentTransaction.addToBackStack(tag);fragmentTransaction.commit();Logger.d(Logger._JN, "replaceFragment :%s ", fragment.getClass().getSimpleName()+"保留上方fragment");} el {//存在则弹出在它上面的所有fragment,并显示对应的fragment,类似activity启动模式SingleTackLogger.d(Logger._JN, "replaceFragment :%s ", fragment.getClass().getSimpleName()+"弹出在它上面的所有fragment");getSupportFragmentManage天津师范大学分数线r().popBackStackImmediate(tag, 0);}}/************************************* - Handler-Helper - ****************************************/private Handler mHandler;protected final boolean ndEmptyMessage(int what) {if (mHandler == null) {mHandler = new Handler(this);}return mHandler.ndEmptyMessage(what);}protected final boolean ndEmptyMessageDelayed(int what, long delayMillis) {if (mHandler == null) {mHandler = new Handler(this);}return mHandler.ndEmptyMessageDelayed(what, delayMillis);}protected final boolean ndEmptyMessageAtTime(int what, long uptimeMillis) {if (mHandler == null) {mHandler = new Handler(this);}return mHandler.ndEmptyMessageAtTime(what, uptimeMillis);}protected final void removeMessages(int what) {if (mHandler != null) {mHandler.removeMessages(what);}}@Overridepublic boolean handleMessage(Message msg) {return fal;}}

本文地址:https://blog.csdn.net/weixin_37716758/article/details/107985863

本文发布于:2023-04-04 02:58:50,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/0781807489a59c7bf989dc4dee63a025.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:基础框架之BaseActivity.doc

本文 PDF 下载地址:基础框架之BaseActivity.pdf

标签:弹出   视图   初始化   是在
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图