From 4f81827e156cef51df780a18ea7d8481ddadac53 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 20 Jun 2020 23:32:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=8C=E5=87=BB=E4=B9=A6=E6=9E=B6?= =?UTF-8?q?=E5=9B=BE=E6=A0=87=E8=BF=94=E5=9B=9E=E9=A1=B6=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/main/MainActivity.kt | 15 +++++++++++++++ .../app/ui/main/bookshelf/BookshelfFragment.kt | 4 ++++ .../app/ui/main/bookshelf/books/BooksFragment.kt | 9 +++++++++ 3 files changed, 28 insertions(+) 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 d604465a1..1eb3250e0 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 @@ -30,10 +30,12 @@ import org.jetbrains.anko.toast class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, + BottomNavigationView.OnNavigationItemReselectedListener, ViewPager.OnPageChangeListener by ViewPager.SimpleOnPageChangeListener() { override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) private var exitTime: Long = 0 + private var bookshelfReselected: Long = 0 private var pagePosition = 0 private val fragmentId = arrayOf(0, 1, 2, 3) private val fragmentMap = mapOf( @@ -50,6 +52,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) + bottom_navigation_view.setOnNavigationItemReselectedListener(this) bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS } @@ -74,6 +77,18 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), return false } + override fun onNavigationItemReselected(item: MenuItem) { + when (item.itemId) { + R.id.menu_bookshelf -> { + if (System.currentTimeMillis() - bookshelfReselected > 300) { + bookshelfReselected = System.currentTimeMillis() + } else { + (fragmentMap[0] as? BookshelfFragment)?.gotoTop() + } + } + } + } + private fun upVersion() { if (getPrefInt(PreferKey.versionCode) != App.INSTANCE.versionCode) { putPrefInt(PreferKey.versionCode, App.INSTANCE.versionCode) 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 341a45eb5..cfd4f7d6d 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 @@ -266,6 +266,10 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } } + fun gotoTop() { + fragmentMap[selectedGroup.groupId]?.gotoTop() + } + private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index 4d3d51663..f0731f1ed 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -16,6 +16,7 @@ import io.legado.app.constant.BookType import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book +import io.legado.app.help.AppConfig import io.legado.app.help.IntentDataHelp import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor @@ -122,6 +123,14 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), return booksAdapter.getItems() } + fun gotoTop() { + if (AppConfig.isEInkMode) { + rv_bookshelf.scrollToPosition(0) + } else { + rv_bookshelf.smoothScrollToPosition(0) + } + } + override fun open(book: Book) { when (book.type) { BookType.audio ->