基础框架之BaseActivity
程序员文章站
2022-03-06 21:37:58
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 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 BaseActivity<T extends IBasePresenter> extends RxAppCompatActivity implements IBaseView, EmptyLayout.OnRetryListener, Handler.Callback {
/**
* 把 EmptyLayout 放在基类统一处理,@Nullable 表明 View 可以为 null,详细可看 ButterKnife
* 注意,资源的ID一定要一样
*/
@Nullable
@BindView(R.id.empty_layout)
protected EmptyLayout mEmptyLayout;
/**
* 把 Presenter提取到基类需要配合基类的initInjector() 进行注入,如果基层这个基类则必定要提供一个Presenter注入方法,
* 该APP把所有Presenter 都是在Module 提供注入实现的, 也可以选择提供不带Presenter 的基类
*/
@Inject
protected T mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();//设置为隐藏ActionBar和全屏显示
// getWindow().setSoftInputMode(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().setStatusBarColor(getResources().getColor(R.color.color_FF282C33));
}
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(attachLayoutRes());
ButterKnife.bind(this);
initInjector();
initViews(savedInstanceState);
updateViews(false);
}
/**
* 绑定布局文件
*
* @return
*/
@LayoutRes
protected abstract int attachLayoutRes();
/**
* Dagger 注入
*/
protected abstract void initInjector();
/**
* 初始化视图
*
* @param savedInstanceState
*/
protected abstract void initViews(Bundle savedInstanceState);
/**
* 接下来开始为更新视图进行从操作,如开始请求网络数据
*
* @param isRefresh
*/
protected abstract void updateViews(boolean isRefresh);
@Override
public void showLoading() {
if (mEmptyLayout != null) {
mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
}
}
@Override
public void showLoading(int loadingIv) {
if (mEmptyLayout != null) {
mEmptyLayout.setEmptyStatus(loadingIv,EmptyLayout.STATUS_LOADING_IV);
}
}
@Override
public void hideLoading() {
if (mEmptyLayout != null) {
mEmptyLayout.hide();
}
}
@Override
public void showNetError(String msg) {
if (mEmptyLayout != null) {
mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
mEmptyLayout.setRetryListener(this);
}
}
@Override
public <T> LifecycleTransformer<T> bindToLife() {
return this.<T>bindToLifecycle();
}
/**
* 显示网络错误时,点击重试监听
*/
@Override
public void onRetry() {
updateViews(false);
}
@Override
protected void onResume() {
super.onResume();
Logger.i(Logger._TJ, "onResume " + getClass().getSimpleName() +" "+ getClass().hashCode());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mEmptyLayout != null) mEmptyLayout.onDestroy();
Logger.i(Logger._TJ, "onDestroy " + getClass().getSimpleName() +" "+ getClass().hashCode());
}
@Override
protected void onPause() {
super.onPause();
Logger.i(Logger._TJ, "onPause " + getClass().getSimpleName() +" "+ getClass().hashCode());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Logger.i(Logger._TJ, "onNewIntent " + getClass().getSimpleName() +" "+ getClass().hashCode());
}
@Override
protected void onStart() {
super.onStart();
Logger.i(Logger._TJ, "onStart " + getClass().getSimpleName() +" "+ 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.setTitle(title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnable);
}
protected void initToolbar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle) {
initToolbar(toolbar, homeAsUpEnabled, getString(resTitle));
}
/**
* android.R.id.home 就是 navigation·home
* 参考getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnable);
*
* @param item
* @return
*/
@Override
public 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 fragment, 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.setTransition(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();
//设置Tag
fragmentTransaction.replace(containerViewId, fragment, tag);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// 这里也要设置tag,通过这里保存的tag找到对应的fragment
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
Logger.d(Logger._JN, "replaceFragment :%s ", fragment.getClass().getSimpleName()+"保留上方fragment");
} else {
//存在则弹出在它上面的所有fragment,并显示对应的fragment,类似activity启动模式SingleTack
Logger.d(Logger._JN, "replaceFragment :%s ", fragment.getClass().getSimpleName()+"弹出在它上面的所有fragment");
getSupportFragmentManager().popBackStackImmediate(tag, 0);
}
}
/************************************* - Handler-Helper - ****************************************/
private Handler mHandler;
protected final boolean sendEmptyMessage(int what) {
if (mHandler == null) {
mHandler = new Handler(this);
}
return mHandler.sendEmptyMessage(what);
}
protected final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
if (mHandler == null) {
mHandler = new Handler(this);
}
return mHandler.sendEmptyMessageDelayed(what, delayMillis);
}
protected final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
if (mHandler == null) {
mHandler = new Handler(this);
}
return mHandler.sendEmptyMessageAtTime(what, uptimeMillis);
}
protected final void removeMessages(int what) {
if (mHandler != null) {
mHandler.removeMessages(what);
}
}
@Override
public boolean handleMessage(Message msg) {
return false;
}
}
本文地址:https://blog.csdn.net/weixin_37716758/article/details/107985863
下一篇: 实现自定义View随手指移动的两种方法