From a58bbfbfcea58a46aac542e2d41bb9ddb9c18485 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 27 Sep 2019 08:35:18 +0800 Subject: [PATCH] up --- .../java/io/legado/app/constant/AppConst.kt | 9 +++++++++ .../app/ui/main/bookshelf/BooksFragment.kt | 5 ++--- .../app/ui/main/bookshelf/BookshelfAdapter.kt | 13 +++++++++--- .../ui/main/bookshelf/BookshelfFragment.kt | 20 ++++++++++++------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 58c51fb25..10a2007c1 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -3,6 +3,7 @@ package io.legado.app.constant import android.annotation.SuppressLint import io.legado.app.App import io.legado.app.R +import io.legado.app.data.entities.BookGroup import java.text.SimpleDateFormat import javax.script.ScriptEngine import javax.script.ScriptEngineManager @@ -37,4 +38,12 @@ object AppConst { "json", "css", "id", "class", "tag" ) } + + val defaultBookGroups by lazy { + mutableListOf( + BookGroup(-1, "全部"), + BookGroup(-2, "本地"), + BookGroup(-3, "音频") + ) + } } \ No newline at end of file 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 ac1142f7a..bf556ffd5 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 @@ -30,7 +30,7 @@ class BooksFragment : VMBaseFragment(R.layout.fragment_books), fun newInstance(position: Int): BooksFragment { return BooksFragment().apply { val bundle = Bundle() - bundle.putInt("position", position) + bundle.putInt("groupId", position) arguments = bundle } } @@ -47,8 +47,7 @@ class BooksFragment : VMBaseFragment(R.layout.fragment_books), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { activityViewModel = getViewModelOfActivity(MainViewModel::class.java) arguments?.let { - val position = it.getInt("position", 0) - + groupId = it.getInt("groupId", -1) } initRecyclerView() upRecyclerData() 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 c18c8ae13..40778ce6d 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 @@ -2,16 +2,23 @@ package io.legado.app.ui.main.bookshelf import androidx.fragment.app.Fragment import androidx.viewpager2.adapter.FragmentStateAdapter +import io.legado.app.data.entities.BookGroup -class BookshelfAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { +class BookshelfAdapter(fragment: Fragment, val callBack: CallBack) : + FragmentStateAdapter(fragment) { override fun getItemCount(): Int { - return 3 + return callBack.groupSize } override fun createFragment(position: Int): Fragment { - return BooksFragment.newInstance(position) + val groupId = callBack.getGroup(position)?.groupId ?: -1 + return BooksFragment.newInstance(groupId) } + interface CallBack { + val groupSize: Int + fun getGroup(position: Int): BookGroup? + } } \ 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 e4d212e4e..e82bf72c2 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 @@ -14,6 +14,7 @@ import androidx.viewpager2.widget.ViewPager2 import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment +import io.legado.app.constant.AppConst import io.legado.app.constant.Bus import io.legado.app.data.entities.BookGroup import io.legado.app.lib.dialogs.* @@ -27,7 +28,8 @@ import org.jetbrains.anko.startActivity class BookshelfFragment : VMBaseFragment(R.layout.fragment_bookshelf), SearchView.OnQueryTextListener, - BookGroupAdapter.CallBack { + BookGroupAdapter.CallBack, + BookshelfAdapter.CallBack { override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) @@ -55,13 +57,21 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } } + override val groupSize: Int + get() = bookGroupAdapter.itemCount + + override fun getGroup(position: Int): BookGroup? { + return bookGroupAdapter.getItem(position) + } + private fun initRecyclerView() { ATH.applyEdgeEffectColor(view_pager_bookshelf) rv_book_group.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) bookGroupAdapter = BookGroupAdapter(requireContext(), this) rv_book_group.adapter = bookGroupAdapter - view_pager_bookshelf.adapter = BookshelfAdapter(this) + bookGroupAdapter.setItems(AppConst.defaultBookGroups) + view_pager_bookshelf.adapter = BookshelfAdapter(this, this) view_pager_bookshelf.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { @@ -76,11 +86,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData?.observe(viewLifecycleOwner, Observer { - mutableListOf( - BookGroup(-1, "全部"), - BookGroup(-2, "本地"), - BookGroup(-3, "音频") - ).apply { + AppConst.defaultBookGroups.apply { addAll(it) }.let { bookGroupAdapter.setItems(it)