From 2dd0d21447ed2328baf87a78afd7a423ebc1a9d9 Mon Sep 17 00:00:00 2001 From: MirkoWu <709651717@qq.com> Date: Thu, 20 Apr 2023 00:12:52 +0800 Subject: [PATCH] =?UTF-8?q?#perf=20=E4=BC=98=E5=8C=96base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mirkowu/mvm/base/BaseActivity.java | 34 +++++++++---------- .../com/mirkowu/mvm/ui/mvvm/MVVMActivity.kt | 11 +++--- .../com/mirkowu/mvm/ui/webview/WebActivity.kt | 4 +-- .../lib_base/activity/BaseMVMActivity.java | 31 ++++++++--------- .../lib_webview/CommonWebActivity.java | 8 ++--- 5 files changed, 41 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/mirkowu/mvm/base/BaseActivity.java b/app/src/main/java/com/mirkowu/mvm/base/BaseActivity.java index 9066ea9..50d4335 100644 --- a/app/src/main/java/com/mirkowu/mvm/base/BaseActivity.java +++ b/app/src/main/java/com/mirkowu/mvm/base/BaseActivity.java @@ -3,6 +3,7 @@ import android.content.res.Resources; +import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; import com.mirkowu.lib_base.activity.BaseMVMActivity; @@ -27,33 +28,30 @@ protected M initMediator() { protected void initStatusBar() { BarUtils.setStatusBarLightMode(this, true); } -// @Override -// public void showStateView(StateBean bean) { -// -// } -// -// @Override -// public void hideStateView() { -// -// } -// -// @Override -// public void onFailure() { -// -// } + /** * 也可以放在Base中 */ - //protected VB mBinding; -// + @LayoutRes + protected int getLayoutId() { + return 0; + } + + @Override + protected void bindContentView() { + setContentView(getLayoutId()); + } + // protected VB mBinding; + // @Override // protected void bindContentView() { -//// super.bindContentView(); +// super.bindContentView(); // this.mBinding = (VB) DataBindingUtil.setContentView(this, getLayoutId()); // setContentView(mBinding.getRoot()); -// // } + + @Override public Resources getResources() { //这个地方慎用,可能会导致在不恰当的时间 调用 diff --git a/app/src/main/java/com/mirkowu/mvm/ui/mvvm/MVVMActivity.kt b/app/src/main/java/com/mirkowu/mvm/ui/mvvm/MVVMActivity.kt index 70b0309..2d9b35b 100644 --- a/app/src/main/java/com/mirkowu/mvm/ui/mvvm/MVVMActivity.kt +++ b/app/src/main/java/com/mirkowu/mvm/ui/mvvm/MVVMActivity.kt @@ -27,15 +27,12 @@ class MVVMActivity : BaseActivity(), RefreshHelper.OnRefreshListe val binding by bindingView(ActivityMVVMBinding::inflate) private lateinit var refreshHelper: RefreshHelper private lateinit var imageAdapter: ImageAdapter - override fun getLayoutId(): Int { - return R.layout.activity_m_v_v_m - } override fun bindContentView() { setContentView(binding.root) } - var isGrayMode=false + var isGrayMode = false override fun initialize() { refreshHelper = RefreshHelper(binding.mRefresh, binding.mRecyclerView, this) imageAdapter = ImageAdapter() @@ -53,10 +50,10 @@ class MVVMActivity : BaseActivity(), RefreshHelper.OnRefreshListe "TAG", "onItemChildClick: $position" ) - isGrayMode=!isGrayMode - if (isGrayMode){ + isGrayMode = !isGrayMode + if (isGrayMode) { ColorFilterUtils.setGrayFilter(activity) - }else{ + } else { ColorFilterUtils.removeFilter(activity) } } diff --git a/app/src/main/java/com/mirkowu/mvm/ui/webview/WebActivity.kt b/app/src/main/java/com/mirkowu/mvm/ui/webview/WebActivity.kt index 58dc02c..1831043 100644 --- a/app/src/main/java/com/mirkowu/mvm/ui/webview/WebActivity.kt +++ b/app/src/main/java/com/mirkowu/mvm/ui/webview/WebActivity.kt @@ -34,8 +34,8 @@ class WebActivity : CommonWebActivity() { } - override fun getLayoutId(): Int { - return R.layout.activity_web + override fun bindContentView() { + setContentView(R.layout.activity_web) } override fun getWebConfig(): WebConfig { diff --git a/lib_base/src/main/java/com/mirkowu/lib_base/activity/BaseMVMActivity.java b/lib_base/src/main/java/com/mirkowu/lib_base/activity/BaseMVMActivity.java index 03acee9..b893902 100644 --- a/lib_base/src/main/java/com/mirkowu/lib_base/activity/BaseMVMActivity.java +++ b/lib_base/src/main/java/com/mirkowu/lib_base/activity/BaseMVMActivity.java @@ -25,7 +25,8 @@ import java.util.concurrent.atomic.AtomicInteger; /** - * 更多可配置的 + * MVM base 示例 + * 可自行实现接口,重写 */ public abstract class BaseMVMActivity extends AppCompatActivity implements IBaseView { @@ -51,13 +52,9 @@ protected void onCreateDelegate(@Nullable Bundle savedInstanceState) { initialize(); } - protected void initialize(@Nullable Bundle savedInstanceState) { - } - /*** 绑定布局 */ - protected void bindContentView() { - setContentView(getLayoutId()); - } + protected abstract void bindContentView(); + /*** 初始化状态栏 setContentView 之前*/ protected void initStatusBar() { @@ -67,6 +64,9 @@ protected void initStatusBar() { // BarUtils.transparentStatusBar(this); } + protected void initialize(@Nullable Bundle savedInstanceState) { + } + /*** 绑定中间件 */ protected void bindMediator() { //实现方式交给子类 @@ -87,6 +87,14 @@ private void detachMediator() { } } + + @Override + protected void onDestroy() { + detachMediator(); + super.onDestroy(); + } + + @Override public void showLoadingDialog() { showLoadingDialog(getString(R.string.widget_loading)); @@ -140,12 +148,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten PermissionsUtils.getInstance().onActivityResult(this, requestCode, resultCode, data); } - @Override - protected void onDestroy() { - detachMediator(); - super.onDestroy(); - } - @Override public void setRequestedOrientation(int requestedOrientation) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) { @@ -199,11 +201,8 @@ public Lifecycle.Event bindLifecycleUntil() { return Lifecycle.Event.ON_DESTROY; } - protected abstract int getLayoutId(); - protected abstract void initialize(); protected abstract M initMediator(); - } diff --git a/lib_webview/src/main/java/com/mirkowu/lib_webview/CommonWebActivity.java b/lib_webview/src/main/java/com/mirkowu/lib_webview/CommonWebActivity.java index 3152df1..f9532f0 100644 --- a/lib_webview/src/main/java/com/mirkowu/lib_webview/CommonWebActivity.java +++ b/lib_webview/src/main/java/com/mirkowu/lib_webview/CommonWebActivity.java @@ -64,13 +64,13 @@ public static void start(Context context, String title, String url, boolean newP protected WebConfig mWebConfig; @Override - protected BaseMediator initMediator() { - return null; + protected void bindContentView() { + setContentView(R.layout.webview_layout_common_web_view); } @Override - protected int getLayoutId() { - return R.layout.webview_layout_common_web_view; + protected BaseMediator initMediator() { + return null; } @Override