pull/35/head
kunfei 5 years ago
parent a46886a833
commit a58bbfbfce
  1. 9
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 5
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt
  3. 13
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt
  4. 20
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.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, "音频")
)
}
}

@ -30,7 +30,7 @@ class BooksFragment : VMBaseFragment<BooksViewModel>(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<BooksViewModel>(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()

@ -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?
}
}

@ -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<BookshelfViewModel>(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<BookshelfViewModel>(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<BookshelfViewModel>(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)

Loading…
Cancel
Save