diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index c3be3ac14..87ec5121e 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -1,12 +1,14 @@ package io.legado.app.base +import android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.View +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl -import androidx.core.content.ContextCompat import androidx.core.view.forEach import androidx.lifecycle.ViewModel import io.legado.app.R @@ -14,6 +16,8 @@ import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.getPrimaryTextColor +import io.legado.app.utils.getCompatColor + abstract class BaseActivity : AppCompatActivity() { @@ -23,6 +27,7 @@ abstract class BaseActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { initTheme() + setupSystemBar() super.onCreate(savedInstanceState) setContentView(layoutID) onViewModelCreated(viewModel, savedInstanceState) @@ -37,8 +42,8 @@ abstract class BaseActivity : AppCompatActivity() { } val bool = onCompatCreateOptionsMenu(it) val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this))) - val defaultTextColor = ContextCompat.getColor(this, R.color.tv_text_default) - menu.forEach {item -> + val defaultTextColor = getCompatColor(R.color.tv_text_default) + menu.forEach { item -> (item as MenuItemImpl).let { impl -> //overflow:展开的item DrawableUtils.setTint( @@ -77,4 +82,11 @@ abstract class BaseActivity : AppCompatActivity() { setTheme(R.style.AppTheme_Dark) } } + + protected fun setupSystemBar() { + window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) + window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) + window.statusBarColor = Color.TRANSPARENT + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 4b1a01ef4..c63b53d05 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -22,7 +22,6 @@ import kotlinx.android.synthetic.main.activity_main.* class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener { - private val mFragmentList: ArrayList = ArrayList() override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) @@ -31,12 +30,8 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig get() = R.layout.activity_main override fun onViewModelCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) { - mFragmentList.add(BookshelfFragment(R.layout.fragment_bookshelf)) - mFragmentList.add(FindBookFragment(R.layout.fragment_find_book)) - mFragmentList.add(BookSourceFragment(R.layout.fragment_book_source)) - mFragmentList.add(MyConfigFragment(R.layout.fragment_my_config)) - view_pager_main.adapter = - TabFragmentPageAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) + view_pager_main.offscreenPageLimit = 3 + view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) } @@ -81,17 +76,21 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig bottom_navigation_view.menu.getItem(position).isChecked = true } - inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager, behavior: Int) : - FragmentPagerAdapter(fm, behavior) { + private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : + FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { override fun getItem(position: Int): Fragment { - return mFragmentList[position] + return when (position) { + 0 -> BookshelfFragment() + 1 -> FindBookFragment() + 2 -> BookSourceFragment() + else -> MyConfigFragment() + } } override fun getCount(): Int { - return mFragmentList.size + return 4 } - } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index f47c26989..88c59221b 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -1,13 +1,17 @@ package io.legado.app.ui.main.bookshelf import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.Fragment +import io.legado.app.R -class BookshelfFragment(contentLayoutId: Int) : Fragment(contentLayoutId) { +class BookshelfFragment : Fragment(R.layout.fragment_bookshelf) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + Log.e("TAG", "BookshelfFragment") + } diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index c7700907b..7f0195ab9 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -1,13 +1,16 @@ package io.legado.app.ui.main.booksource import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.Fragment +import io.legado.app.R -class BookSourceFragment(contentLayoutId: Int) : Fragment(contentLayoutId) { +class BookSourceFragment : Fragment(R.layout.fragment_book_source) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + Log.e("TAG", "BookSourceFragment") } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt index 1f82bde48..ce2140978 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt @@ -1,13 +1,16 @@ package io.legado.app.ui.main.findbook import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.Fragment +import io.legado.app.R -class FindBookFragment(contentLayoutId: Int) : Fragment(contentLayoutId) { +class FindBookFragment : Fragment(R.layout.fragment_find_book) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + Log.e("TAG", "FindBookFragment") } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt index af07504b3..052f1324a 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt @@ -1,13 +1,16 @@ package io.legado.app.ui.main.myconfig import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.Fragment +import io.legado.app.R -class MyConfigFragment(contentLayoutId: Int) : Fragment(contentLayoutId) { +class MyConfigFragment : Fragment(R.layout.fragment_my_config) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + Log.e("TAG", "MyConfigFragment") } } \ 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 index 428500b6e..01597a940 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -14,6 +14,9 @@ import androidx.appcompat.widget.Toolbar import androidx.core.graphics.drawable.DrawableCompat import com.google.android.material.appbar.AppBarLayout import io.legado.app.R +import io.legado.app.utils.getStatusBarHeight +import org.jetbrains.anko.bottomPadding +import org.jetbrains.anko.topPadding class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, attrs) { @@ -84,6 +87,14 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a } } + if (a.getBoolean(R.styleable.TitleBar_fitStatusBar, true)) { + topPadding = context.getStatusBarHeight() + } + + if (a.getBoolean(R.styleable.TitleBar_fitNavigationBar, false)) { + bottomPadding = context.getStatusBarHeight() + } + a.recycle() if (attachToActivity) { @@ -122,8 +133,8 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a private fun attachToActivity(context: Context) { val activity = getCompatActivity(context) activity?.let { - activity.setSupportActionBar(toolbar) - activity.supportActionBar?.setDisplayHomeAsUpEnabled(mDisplayHomeAsUp) + it.setSupportActionBar(toolbar) + it.supportActionBar?.setDisplayHomeAsUpEnabled(mDisplayHomeAsUp) } } diff --git a/app/src/main/java/io/legado/app/utils/AnkoExtensions.kt b/app/src/main/java/io/legado/app/utils/AnkoExtensions.kt index 4fdace873..591bd4ade 100644 --- a/app/src/main/java/io/legado/app/utils/AnkoExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/AnkoExtensions.kt @@ -1,6 +1,12 @@ package io.legado.app.utils +import android.app.Activity +import android.content.res.ColorStateList +import android.graphics.drawable.Drawable import android.view.View +import android.view.WindowManager +import androidx.annotation.ColorRes +import androidx.annotation.DrawableRes import androidx.annotation.StringRes import androidx.fragment.app.Fragment import com.google.android.material.snackbar.Snackbar @@ -51,3 +57,9 @@ fun Fragment.longToast(text: CharSequence) = requireActivity().longToast(text) fun Fragment.dip(value: Int): Int = requireActivity().dip(value) fun Fragment.dip(value: Float): Int = requireActivity().dip(value) + +fun Fragment.getCompatColor(@ColorRes id: Int): Int = requireContext().getCompatColor(id) + +fun Fragment.getCompatDrawable(@DrawableRes id: Int): Drawable? = requireContext().getCompatDrawable(id) + +fun Fragment.getCompatColorStateList(@ColorRes id: Int): ColorStateList? = requireContext().getCompatColorStateList(id) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index a94267fda..6a80cd841 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -1,6 +1,11 @@ package io.legado.app.utils import android.content.Context +import android.content.res.ColorStateList +import android.graphics.drawable.Drawable +import androidx.annotation.ColorRes +import androidx.annotation.DrawableRes +import androidx.core.content.ContextCompat import androidx.core.content.edit import org.jetbrains.anko.connectivityManager import org.jetbrains.anko.defaultSharedPreferences @@ -40,3 +45,15 @@ fun Context.putPrefStringSet(key: String, value: MutableSet) = fun Context.removePref(key: String) = defaultSharedPreferences.edit { remove(key) } + + +fun Context.getCompatColor(@ColorRes id: Int): Int = ContextCompat.getColor(this, id) + +fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? = ContextCompat.getDrawable(this, id) + +fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? = ContextCompat.getColorStateList(this, id) + +fun Context.getStatusBarHeight(): Int { + val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android") + return resources.getDimensionPixelSize(resourceId) +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a0730b697..411c77428 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,13 +3,13 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" - android:layout_height="match_parent" - android:fitsSystemWindows="true"> + android:layout_height="match_parent"> diff --git a/app/src/main/res/layout/fragment_bookshelf.xml b/app/src/main/res/layout/fragment_bookshelf.xml index 2360d6d70..4469491be 100644 --- a/app/src/main/res/layout/fragment_bookshelf.xml +++ b/app/src/main/res/layout/fragment_bookshelf.xml @@ -1,15 +1,17 @@ - + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_find_book.xml b/app/src/main/res/layout/fragment_find_book.xml index 4c8e1609e..c7eeea4da 100644 --- a/app/src/main/res/layout/fragment_find_book.xml +++ b/app/src/main/res/layout/fragment_find_book.xml @@ -9,6 +9,7 @@ android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" + app:attachToActivity="false" app:layout_constraintTop_toTopOf="parent" app:title="@string/find"/> diff --git a/app/src/main/res/layout/fragment_my_config.xml b/app/src/main/res/layout/fragment_my_config.xml index a4b260fa9..9fe7b70f7 100644 --- a/app/src/main/res/layout/fragment_my_config.xml +++ b/app/src/main/res/layout/fragment_my_config.xml @@ -1,7 +1,6 @@ - @@ -9,7 +8,9 @@ android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" + app:attachToActivity="false" app:layout_constraintTop_toTopOf="parent" app:title="我的"/> - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index b60fcb652..75b4d171f 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -11,6 +11,8 @@ + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 15c10c068..8dbf324cb 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -31,7 +31,8 @@ @style/Style.PopupMenu @style/Style.MenuItemText @style/Style.PopupMenu - true + @color/transparent + true