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 android.annotation.SuppressLint
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.data.entities.BookGroup
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import javax.script.ScriptEngine import javax.script.ScriptEngine
import javax.script.ScriptEngineManager import javax.script.ScriptEngineManager
@ -37,4 +38,12 @@ object AppConst {
"json", "css", "id", "class", "tag" "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 { fun newInstance(position: Int): BooksFragment {
return BooksFragment().apply { return BooksFragment().apply {
val bundle = Bundle() val bundle = Bundle()
bundle.putInt("position", position) bundle.putInt("groupId", position)
arguments = bundle arguments = bundle
} }
} }
@ -47,8 +47,7 @@ class BooksFragment : VMBaseFragment<BooksViewModel>(R.layout.fragment_books),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
activityViewModel = getViewModelOfActivity(MainViewModel::class.java) activityViewModel = getViewModelOfActivity(MainViewModel::class.java)
arguments?.let { arguments?.let {
val position = it.getInt("position", 0) groupId = it.getInt("groupId", -1)
} }
initRecyclerView() initRecyclerView()
upRecyclerData() upRecyclerData()

@ -2,16 +2,23 @@ package io.legado.app.ui.main.bookshelf
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter 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 { override fun getItemCount(): Int {
return 3 return callBack.groupSize
} }
override fun createFragment(position: Int): Fragment { 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.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.lib.dialogs.* import io.legado.app.lib.dialogs.*
@ -27,7 +28,8 @@ import org.jetbrains.anko.startActivity
class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_bookshelf), class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_bookshelf),
SearchView.OnQueryTextListener, SearchView.OnQueryTextListener,
BookGroupAdapter.CallBack { BookGroupAdapter.CallBack,
BookshelfAdapter.CallBack {
override val viewModel: BookshelfViewModel override val viewModel: BookshelfViewModel
get() = getViewModel(BookshelfViewModel::class.java) 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() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(view_pager_bookshelf) ATH.applyEdgeEffectColor(view_pager_bookshelf)
rv_book_group.layoutManager = rv_book_group.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
bookGroupAdapter = BookGroupAdapter(requireContext(), this) bookGroupAdapter = BookGroupAdapter(requireContext(), this)
rv_book_group.adapter = bookGroupAdapter 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 : view_pager_bookshelf.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() { ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) { override fun onPageSelected(position: Int) {
@ -76,11 +86,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
mutableListOf( AppConst.defaultBookGroups.apply {
BookGroup(-1, "全部"),
BookGroup(-2, "本地"),
BookGroup(-3, "音频")
).apply {
addAll(it) addAll(it)
}.let { }.let {
bookGroupAdapter.setItems(it) bookGroupAdapter.setItems(it)

Loading…
Cancel
Save