diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index 2a32912ee..51068b149 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt @@ -1,6 +1,6 @@ package io.legado.app.data.dao -import androidx.paging.DataSource +import androidx.lifecycle.LiveData import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy @@ -11,7 +11,7 @@ import io.legado.app.data.entities.BookGroup interface BookGroupDao { @Query("SELECT * FROM book_groups ORDER BY `order`") - fun observeAll(): DataSource.Factory + fun liveDataAll(): LiveData> @get:Query("SELECT MAX(groupId) FROM book_groups") val maxId: Int diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt index bb3adbf8f..4e0a9e4d8 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt @@ -1,65 +1,19 @@ package io.legado.app.ui.main.bookshelf -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.paging.PagedListAdapter -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.RecyclerView +import android.content.Context import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.BookGroup -import io.legado.app.help.AdapterDataObserverProxy import kotlinx.android.synthetic.main.item_book_group.view.* -class BookGroupAdapter : PagedListAdapter(DIFF_CALLBACK) { +class BookGroupAdapter(context: Context, val callBack: CallBack) : + SimpleRecyclerAdapter(context, R.layout.item_book_group) { - companion object { - @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean = - oldItem.groupId == newItem.groupId - - override fun areContentsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean = - oldItem.groupId == newItem.groupId - && oldItem.groupName == newItem.groupName - && oldItem.order == newItem.order - } - } - - var callBack: CallBack? = null - private val defaultGroups = arrayOf(BookGroup(-1, "全部"), - BookGroup(-2, "本地"), - BookGroup(-3, "音频")) - - private val addBookGroup = BookGroup(-10, "+") - - override fun getItemCount(): Int { - return super.getItemCount() + defaultGroups.size + 1 - } - - override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) { - super.registerAdapterDataObserver(AdapterDataObserverProxy(observer, defaultGroups.size)) - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { - return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_book_group, parent, false)) - } - - override fun onBindViewHolder(holder: MyViewHolder, position: Int) { - when { - position < defaultGroups.size -> holder.bind(defaultGroups[position], callBack) - position == itemCount - 1 -> holder.bind(addBookGroup, callBack) - else -> currentList?.get(position - defaultGroups.size)?.let { - holder.bind(it, callBack) - } - } - } - - class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { - - fun bind(bookGroup: BookGroup, callBack: CallBack?) = with(itemView) { - tv_group.text = bookGroup.groupName - tv_group.setOnClickListener { callBack?.open(bookGroup) } + override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { + with(holder.itemView) { + tv_group.text = item.groupName + tv_group.setOnClickListener { callBack.open(item) } } } 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 4b26fed38..ac1142f7a 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 @@ -42,10 +42,14 @@ class BooksFragment : VMBaseFragment(R.layout.fragment_books), private lateinit var activityViewModel: MainViewModel private lateinit var booksAdapter: BooksAdapter private var bookshelfLiveData: LiveData>? = null - private val groupId = -1 + private var groupId = -1 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { activityViewModel = getViewModelOfActivity(MainViewModel::class.java) + arguments?.let { + val position = it.getInt("position", 0) + + } initRecyclerView() upRecyclerData() } 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 d931f3a48..e4d212e4e 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,8 +9,6 @@ import android.widget.EditText import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer -import androidx.paging.LivePagedListBuilder -import androidx.paging.PagedList import androidx.recyclerview.widget.LinearLayoutManager import androidx.viewpager2.widget.ViewPager2 import io.legado.app.App @@ -36,7 +34,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private lateinit var booksAdapter: BooksAdapter private lateinit var bookGroupAdapter: BookGroupAdapter - private var bookGroupLiveData: LiveData>? = null + private var bookGroupLiveData: LiveData>? = null private var position = -1 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -61,9 +59,8 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b ATH.applyEdgeEffectColor(view_pager_bookshelf) rv_book_group.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) - bookGroupAdapter = BookGroupAdapter() + bookGroupAdapter = BookGroupAdapter(requireContext(), this) rv_book_group.adapter = bookGroupAdapter - bookGroupAdapter.callBack = this view_pager_bookshelf.adapter = BookshelfAdapter(this) view_pager_bookshelf.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { @@ -77,8 +74,18 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private fun initBookGroupData() { bookGroupLiveData?.removeObservers(viewLifecycleOwner) - bookGroupLiveData = LivePagedListBuilder(App.db.bookGroupDao().observeAll(), 10).build() - bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupAdapter.submitList(it) }) + bookGroupLiveData = App.db.bookGroupDao().liveDataAll() + bookGroupLiveData?.observe(viewLifecycleOwner, Observer { + mutableListOf( + BookGroup(-1, "全部"), + BookGroup(-2, "本地"), + BookGroup(-3, "音频") + ).apply { + addAll(it) + }.let { + bookGroupAdapter.setItems(it) + } + }) } override fun open(bookGroup: BookGroup) {