pull/35/head
kunfei 5 years ago
parent cf88899eb0
commit 65211b19d5
  1. 2
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 4
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt
  3. 39
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  4. 34
      app/src/main/res/layout/fragment_bookshelf.xml

@ -39,7 +39,7 @@ object AppConst {
} }
val defaultBookGroups by lazy { val defaultBookGroups by lazy {
mutableListOf( listOf(
BookGroup(-1, "全部"), BookGroup(-1, "全部"),
BookGroup(-2, "本地"), BookGroup(-2, "本地"),
BookGroup(-3, "音频") BookGroup(-3, "音频")

@ -13,12 +13,12 @@ class BookshelfAdapter(fragment: Fragment, val callBack: CallBack) :
} }
override fun createFragment(position: Int): Fragment { override fun createFragment(position: Int): Fragment {
val groupId = callBack.getGroup(position)?.groupId ?: -1 val groupId = callBack.getGroup(position).groupId
return BooksFragment.newInstance(groupId) return BooksFragment.newInstance(groupId)
} }
interface CallBack { interface CallBack {
val groupSize: Int val groupSize: Int
fun getGroup(position: Int): BookGroup? fun getGroup(position: Int): BookGroup
} }
} }

@ -9,13 +9,11 @@ import android.widget.EditText
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.tabs.TabLayoutMediator
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.AppConst
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.*
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
@ -23,6 +21,7 @@ import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.fragment_bookshelf.*
import kotlinx.android.synthetic.main.view_tab_layout.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -34,10 +33,8 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
override val viewModel: BookshelfViewModel override val viewModel: BookshelfViewModel
get() = getViewModel(BookshelfViewModel::class.java) get() = getViewModel(BookshelfViewModel::class.java)
private lateinit var booksAdapter: BooksAdapter
private lateinit var bookGroupAdapter: BookGroupAdapter
private var bookGroupLiveData: LiveData<List<BookGroup>>? = null private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private var position = -1 private val bookGroups = mutableListOf<BookGroup>().apply { addAll(AppConst.defaultBookGroups) }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
@ -58,39 +55,29 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
} }
override val groupSize: Int override val groupSize: Int
get() = bookGroupAdapter.itemCount get() = bookGroups.size
override fun getGroup(position: Int): BookGroup? { override fun getGroup(position: Int): BookGroup {
return bookGroupAdapter.getItem(position) return bookGroups[position]
} }
private fun initRecyclerView() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(view_pager_bookshelf) ATH.applyEdgeEffectColor(view_pager_bookshelf)
rv_book_group.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
bookGroupAdapter = BookGroupAdapter(requireContext(), this)
rv_book_group.adapter = bookGroupAdapter
bookGroupAdapter.setItems(AppConst.defaultBookGroups)
view_pager_bookshelf.adapter = BookshelfAdapter(this, this) view_pager_bookshelf.adapter = BookshelfAdapter(this, this)
view_pager_bookshelf.registerOnPageChangeCallback(object : TabLayoutMediator(tab_layout, view_pager_bookshelf) { tab, position ->
ViewPager2.OnPageChangeCallback() { tab.text = bookGroups[position].groupName
override fun onPageSelected(position: Int) { }.attach()
super.onPageSelected(position)
this@BookshelfFragment.position = position
}
})
observeEvent<String>(Bus.UP_BOOK) { booksAdapter.notification(it) }
} }
private fun initBookGroupData() { private fun initBookGroupData() {
bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
AppConst.defaultBookGroups.apply { for (index in AppConst.defaultBookGroups.size until bookGroups.size) {
addAll(it) bookGroups.removeAt(AppConst.defaultBookGroups.size)
}.let {
bookGroupAdapter.setItems(it)
} }
bookGroups.addAll(it)
view_pager_bookshelf.adapter?.notifyDataSetChanged()
}) })
} }

@ -1,26 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<io.legado.app.ui.widget.TitleBar <io.legado.app.ui.widget.TitleBar
android:id="@+id/title_bar" android:id="@+id/title_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:attachToActivity="false" app:attachToActivity="false"
app:title="@string/bookshelf"/> app:title="@string/bookshelf"
app:contentLayout="@layout/view_tab_layout"
<androidx.recyclerview.widget.RecyclerView app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/rv_book_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
<androidx.viewpager2.widget.ViewPager2 <androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager_bookshelf" android:id="@+id/view_pager_bookshelf"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/title_bar"
app:layout_constraintBottom_toBottomOf="parent" />
</LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save