From 6b94113a49087733c89cde445c3cf3707ed6523f Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Wed, 22 May 2019 18:30:27 +0800 Subject: [PATCH] create BaseActivity --- app/build.gradle | 7 ++- .../java/io/legado/app/base/BaseActivity.kt | 55 +++++++++++++++++++ .../main/java/io/legado/app/base/README.md | 1 + .../java/io/legado/app/ui/widget/TitleBar.kt | 40 ++++++++++++++ app/src/main/res/layout/view_titlebar.xml | 16 ++++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/io/legado/app/base/BaseActivity.kt create mode 100644 app/src/main/java/io/legado/app/base/README.md create mode 100644 app/src/main/java/io/legado/app/ui/widget/TitleBar.kt create mode 100644 app/src/main/res/layout/view_titlebar.xml diff --git a/app/build.gradle b/app/build.gradle index 5a4c99f24..22574a10c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,13 +36,14 @@ dependencies { implementation 'androidx.paging:paging-runtime:2.1.0' + //anko def anko_version = '0.10.8' implementation "org.jetbrains.anko:anko-sdk27:$anko_version" implementation "org.jetbrains.anko:anko-sdk27-listeners:$anko_version" - - + //协程 + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' -} \ No newline at end of file +} diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt new file mode 100644 index 000000000..45a4679a6 --- /dev/null +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -0,0 +1,55 @@ +package io.legado.app.base + +import android.os.Bundle +import android.view.MenuItem +import androidx.annotation.LayoutRes +import androidx.appcompat.app.AppCompatActivity + +abstract class BaseActivity : AppCompatActivity() { + + @LayoutRes + abstract fun getLayoutID(): Int + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(getLayoutID()) + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + item?.let { + if (it.itemId == android.R.id.home) { + supportFinishAfterTransition() + return true + } + } + return if (item == null) true else onCompatOptionsItemSelected(item) + } + + open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { + return true + } + + override fun setTitle(title: CharSequence?) { + supportActionBar?.let { + it.title = title + } + } + + override fun setTitle(titleId: Int) { + supportActionBar?.let { + it.setTitle(titleId) + } + } + + fun setSubTitle(subtitle: CharSequence?){ + supportActionBar?.let { + it.subtitle = subtitle; + } + } + + fun setSubTitle(subtitleId: Int){ + supportActionBar?.let { + it.setSubtitle(subtitleId) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/base/README.md b/app/src/main/java/io/legado/app/base/README.md new file mode 100644 index 000000000..d28e27909 --- /dev/null +++ b/app/src/main/java/io/legado/app/base/README.md @@ -0,0 +1 @@ +## 基类 \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt new file mode 100644 index 000000000..da7d6ba71 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -0,0 +1,40 @@ +package io.legado.app.ui.widget + +import android.content.Context +import android.util.AttributeSet +import android.widget.FrameLayout +import androidx.appcompat.app.AppCompatActivity +import io.legado.app.R +import kotlinx.android.synthetic.main.view_titlebar.view.* + +class TitleBar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) { + + init { + inflate(context, R.layout.view_titlebar, this) + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + attachToActivity() + } + + private fun attachToActivity(){ + val activity = getCompatActivity(context) + activity?.let { + activity.setSupportActionBar(toolbar) + activity.supportActionBar?.let { + it.setDisplayHomeAsUpEnabled(true) + } + } + } + + private fun getCompatActivity(context: Context?): AppCompatActivity? { + if (context == null) return null + return when (context) { + is AppCompatActivity -> context + is androidx.appcompat.view.ContextThemeWrapper -> getCompatActivity(context.baseContext) + is android.view.ContextThemeWrapper -> getCompatActivity(context.baseContext) + else -> null + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/view_titlebar.xml b/app/src/main/res/layout/view_titlebar.xml new file mode 100644 index 000000000..6f86e663a --- /dev/null +++ b/app/src/main/res/layout/view_titlebar.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file