From 3ebb83a6af38a081e7c6e4318412354fbd4dd990 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 26 Sep 2019 22:30:31 +0800 Subject: [PATCH] up --- .../legado/app/ui/changesource/ChangeSourceDialog.kt | 12 ++++++------ .../io/legado/app/ui/main/bookshelf/BooksFragment.kt | 11 +++++++++++ .../legado/app/ui/main/bookshelf/BookshelfAdapter.kt | 2 +- .../app/ui/main/bookshelf/BookshelfFragment.kt | 9 +++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt index df03b920e..8f02698b9 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt @@ -26,12 +26,12 @@ class ChangeSourceDialog : DialogFragment(), const val tag = "changeSourceDialog" fun newInstance(name: String, author: String): ChangeSourceDialog { - val changeSourceDialog = ChangeSourceDialog() - val bundle = Bundle() - bundle.putString("name", name) - bundle.putString("author", author) - changeSourceDialog.arguments = bundle - return changeSourceDialog + return ChangeSourceDialog().apply { + val bundle = Bundle() + bundle.putString("name", name) + bundle.putString("author", author) + arguments = bundle + } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt index ee8236255..4b26fed38 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt @@ -25,6 +25,17 @@ import org.jetbrains.anko.startActivity class BooksFragment : VMBaseFragment(R.layout.fragment_books), BooksAdapter.CallBack { + + companion object { + fun newInstance(position: Int): BooksFragment { + return BooksFragment().apply { + val bundle = Bundle() + bundle.putInt("position", position) + arguments = bundle + } + } + } + override val viewModel: BooksViewModel get() = getViewModel(BooksViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt index 86238a663..c18c8ae13 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt @@ -11,7 +11,7 @@ class BookshelfAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { } override fun createFragment(position: Int): Fragment { - return BooksFragment() + return BooksFragment.newInstance(position) } } \ No newline at end of file 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 9b6d5f90b..d931f3a48 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 @@ -12,6 +12,7 @@ import androidx.lifecycle.Observer import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList import androidx.recyclerview.widget.LinearLayoutManager +import androidx.viewpager2.widget.ViewPager2 import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment @@ -36,6 +37,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private lateinit var booksAdapter: BooksAdapter private lateinit var bookGroupAdapter: BookGroupAdapter private var bookGroupLiveData: LiveData>? = null + private var position = -1 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -63,6 +65,13 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b rv_book_group.adapter = bookGroupAdapter bookGroupAdapter.callBack = this view_pager_bookshelf.adapter = BookshelfAdapter(this) + view_pager_bookshelf.registerOnPageChangeCallback(object : + ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + this@BookshelfFragment.position = position + } + }) observeEvent(Bus.UP_BOOK) { booksAdapter.notification(it) } }