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 b484993d5..f9fe5b942 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -39,7 +39,7 @@ object AppConst { } val defaultBookGroups by lazy { - mutableListOf( + listOf( BookGroup(-1, "全部"), BookGroup(-2, "本地"), BookGroup(-3, "音频") 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 40778ce6d..c4176f78d 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 @@ -13,12 +13,12 @@ class BookshelfAdapter(fragment: Fragment, val callBack: CallBack) : } override fun createFragment(position: Int): Fragment { - val groupId = callBack.getGroup(position)?.groupId ?: -1 + val groupId = callBack.getGroup(position).groupId return BooksFragment.newInstance(groupId) } interface CallBack { val groupSize: Int - fun getGroup(position: Int): BookGroup? + 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 0a546e57d..1102ea3de 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 @@ -9,13 +9,11 @@ import android.widget.EditText import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.viewpager2.widget.ViewPager2 +import com.google.android.material.tabs.TabLayoutMediator 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.* 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 kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.fragment_bookshelf.* +import kotlinx.android.synthetic.main.view_tab_layout.* import kotlinx.android.synthetic.main.view_title_bar.* import org.jetbrains.anko.startActivity @@ -34,10 +33,8 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) - private lateinit var booksAdapter: BooksAdapter - private lateinit var bookGroupAdapter: BookGroupAdapter private var bookGroupLiveData: LiveData>? = null - private var position = -1 + private val bookGroups = mutableListOf().apply { addAll(AppConst.defaultBookGroups) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -58,39 +55,29 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } override val groupSize: Int - get() = bookGroupAdapter.itemCount + get() = bookGroups.size - override fun getGroup(position: Int): BookGroup? { - return bookGroupAdapter.getItem(position) + override fun getGroup(position: Int): BookGroup { + return bookGroups[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 - bookGroupAdapter.setItems(AppConst.defaultBookGroups) view_pager_bookshelf.adapter = BookshelfAdapter(this, 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) } + TabLayoutMediator(tab_layout, view_pager_bookshelf) { tab, position -> + tab.text = bookGroups[position].groupName + }.attach() } private fun initBookGroupData() { bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData?.observe(viewLifecycleOwner, Observer { - AppConst.defaultBookGroups.apply { - addAll(it) - }.let { - bookGroupAdapter.setItems(it) + for (index in AppConst.defaultBookGroups.size until bookGroups.size) { + bookGroups.removeAt(AppConst.defaultBookGroups.size) } + bookGroups.addAll(it) + view_pager_bookshelf.adapter?.notifyDataSetChanged() }) } diff --git a/app/src/main/res/layout/fragment_bookshelf.xml b/app/src/main/res/layout/fragment_bookshelf.xml index 8bba3afae..aa76e7563 100644 --- a/app/src/main/res/layout/fragment_bookshelf.xml +++ b/app/src/main/res/layout/fragment_bookshelf.xml @@ -1,26 +1,24 @@ - + - - + android:id="@+id/title_bar" + android:layout_width="match_parent" + android:layout_height="wrap_content" + app:attachToActivity="false" + app:title="@string/bookshelf" + app:contentLayout="@layout/view_tab_layout" + app:layout_constraintTop_toTopOf="parent" /> + android:layout_height="0dp" + app:layout_constraintTop_toBottomOf="@+id/title_bar" + app:layout_constraintBottom_toBottomOf="parent" /> - \ No newline at end of file + \ No newline at end of file