diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt index eaa8e8a9d..3bd6e01c4 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt @@ -9,12 +9,13 @@ import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup import io.legado.app.databinding.ItemBookshelfGridBinding +import io.legado.app.databinding.ItemBookshelfGridGroupBinding import io.legado.app.help.AppConfig import io.legado.app.utils.invisible import splitties.views.onLongClick class BooksAdapterGrid(context: Context, callBack: CallBack) : - BaseBooksAdapter(context, callBack) { + BaseBooksAdapter(context, callBack) { override fun getItemCount(): Int { return callBack.getItemCount() @@ -23,56 +24,82 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) : override fun onCreateViewHolder( parent: ViewGroup, viewType: Int - ): ItemViewHolder { - return ItemViewHolder( + ): RecyclerView.ViewHolder { + return BookViewHolder( ItemBookshelfGridBinding.inflate(LayoutInflater.from(context), parent, false) ) } override fun onBindViewHolder( - holder: ItemViewHolder, + holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList ) { - holder.binding.run { - val bundle = payloads.getOrNull(0) as? Bundle - if (bundle == null) { - super.onBindViewHolder(holder, position, payloads) - } else { - when (val item = callBack.getItem(position)) { - is Book -> { - bundle.keySet().forEach { - when (it) { - "name" -> tvName.text = item.name - "cover" -> ivCover.load( - item.getDisplayCover(), - item.name, - item.author - ) - "refresh" -> upRefresh(this, item) - } - } - } - is BookGroup -> { - tvName.text = item.groupName + val bundle = payloads.getOrNull(0) as? Bundle + when { + bundle == null -> super.onBindViewHolder(holder, position, payloads) + holder is BookViewHolder -> onBindBook(holder.binding, position, bundle) + holder is GroupViewHolder -> onBindGroup(holder.binding, position, bundle) + } + } + + private fun onBindGroup(binding: ItemBookshelfGridGroupBinding, position: Int, bundle: Bundle) { + binding.run { + val item = callBack.getItem(position) + if (item is BookGroup) { + tvName.text = item.groupName + } + } + } + + private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int, bundle: Bundle) { + binding.run { + val item = callBack.getItem(position) + if (item is Book) { + bundle.keySet().forEach { + when (it) { + "name" -> tvName.text = item.name + "cover" -> ivCover.load( + item.getDisplayCover(), + item.name, + item.author + ) + "refresh" -> upRefresh(this, item) } } + } + } + } + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + when (holder) { + is BookViewHolder -> onBindBook(holder.binding, position) + is GroupViewHolder -> onBindGroup(holder.binding, position) + } + } + + private fun onBindGroup(binding: ItemBookshelfGridGroupBinding, position: Int) { + binding.run { + val item = callBack.getItem(position) + if (item is BookGroup) { + tvName.text = item.groupName + } + root.setOnClickListener { + callBack.onItemClick(position) + } + root.onLongClick { + callBack.onItemLongClick(position) } } } - override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { - holder.binding.run { - when (val item = callBack.getItem(position)) { - is Book -> { - tvName.text = item.name - ivCover.load(item.getDisplayCover(), item.name, item.author) - upRefresh(this, item) - } - is BookGroup -> { - tvName.text = item.groupName - } + private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int) { + binding.run { + val item = callBack.getItem(position) + if (item is Book) { + tvName.text = item.name + ivCover.load(item.getDisplayCover(), item.name, item.author) + upRefresh(this, item) } root.setOnClickListener { callBack.onItemClick(position) @@ -98,7 +125,10 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) : } } - class ItemViewHolder(val binding: ItemBookshelfGridBinding) : + class BookViewHolder(val binding: ItemBookshelfGridBinding) : + RecyclerView.ViewHolder(binding.root) + + class GroupViewHolder(val binding: ItemBookshelfGridGroupBinding) : RecyclerView.ViewHolder(binding.root) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt index 755772eaa..0c85fd5fe 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt @@ -9,6 +9,7 @@ import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup import io.legado.app.databinding.ItemBookshelfListBinding +import io.legado.app.databinding.ItemBookshelfListGroupBinding import io.legado.app.help.AppConfig import io.legado.app.utils.gone import io.legado.app.utils.invisible @@ -16,78 +17,105 @@ import io.legado.app.utils.visible import splitties.views.onLongClick class BooksAdapterList(context: Context, callBack: CallBack) : - BaseBooksAdapter(context, callBack) { + BaseBooksAdapter(context, callBack) { override fun getItemCount(): Int { return callBack.getItemCount() } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { - return ItemViewHolder( + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + return BookViewHolder( ItemBookshelfListBinding.inflate(LayoutInflater.from(context), parent, false) ) } override fun onBindViewHolder( - holder: ItemViewHolder, + holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList ) { val bundle = payloads.getOrNull(0) as? Bundle - if (bundle == null) { - super.onBindViewHolder(holder, position, payloads) - } else { - holder.binding.run { - when (val item = callBack.getItem(position)) { - is Book -> { - tvRead.text = item.durChapterTitle - tvLast.text = item.latestChapterTitle - bundle.keySet().forEach { - when (it) { - "name" -> tvName.text = item.name - "author" -> tvAuthor.text = item.author - "cover" -> ivCover.load( - item.getDisplayCover(), - item.name, - item.author - ) - "refresh" -> upRefresh(this, item) - } - } - } - is BookGroup -> { - tvName.text = item.groupName + when { + bundle == null -> super.onBindViewHolder(holder, position, payloads) + holder is BookViewHolder -> onBindBook(holder.binding, position, bundle) + holder is GroupViewHolder -> onBindGroup(holder.binding, position, bundle) + } + } + + private fun onBindGroup(binding: ItemBookshelfListGroupBinding, position: Int, bundle: Bundle) { + binding.run { + val item = callBack.getItem(position) + if (item is BookGroup) { + tvName.text = item.groupName + } + } + } + + private fun onBindBook(binding: ItemBookshelfListBinding, position: Int, bundle: Bundle) { + binding.run { + val item = callBack.getItem(position) + if (item is Book) { + tvRead.text = item.durChapterTitle + tvLast.text = item.latestChapterTitle + bundle.keySet().forEach { + when (it) { + "name" -> tvName.text = item.name + "author" -> tvAuthor.text = item.author + "cover" -> ivCover.load( + item.getDisplayCover(), + item.name, + item.author + ) + "refresh" -> upRefresh(this, item) } } } } } - override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { - holder.binding.run { - when (val item = callBack.getItem(position)) { - is Book -> { - tvName.text = item.name - tvAuthor.text = item.author - tvRead.text = item.durChapterTitle - tvLast.text = item.latestChapterTitle - ivCover.load(item.getDisplayCover(), item.name, item.author) - flHasNew.visible() - ivAuthor.visible() - ivLast.visible() - ivRead.visible() - upRefresh(this, item) - } - is BookGroup -> { - tvName.text = item.groupName - flHasNew.gone() - ivAuthor.gone() - ivLast.gone() - ivRead.gone() - tvAuthor.gone() - tvLast.gone() - tvRead.gone() - } + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + when (holder) { + is BookViewHolder -> onBindBook(holder.binding, position) + is GroupViewHolder -> onBindGroup(holder.binding, position) + } + } + + private fun onBindGroup(binding: ItemBookshelfListGroupBinding, position: Int) { + binding.run { + val item = callBack.getItem(position) + if (item is BookGroup) { + tvName.text = item.groupName + flHasNew.gone() + ivAuthor.gone() + ivLast.gone() + ivRead.gone() + tvAuthor.gone() + tvLast.gone() + tvRead.gone() + } + root.setOnClickListener { + callBack.onItemClick(position) + } + root.onLongClick { + callBack.onItemLongClick(position) + } + } + } + + private fun onBindBook(binding: ItemBookshelfListBinding, position: Int) { + binding.run { + val item = callBack.getItem(position) + if (item is Book) { + tvName.text = item.name + tvAuthor.text = item.author + tvRead.text = item.durChapterTitle + tvLast.text = item.latestChapterTitle + ivCover.load(item.getDisplayCover(), item.name, item.author) + flHasNew.visible() + ivAuthor.visible() + ivLast.visible() + ivRead.visible() + upRefresh(this, item) } root.setOnClickListener { callBack.onItemClick(position) @@ -113,7 +141,10 @@ class BooksAdapterList(context: Context, callBack: CallBack) : } } - class ItemViewHolder(val binding: ItemBookshelfListBinding) : + class BookViewHolder(val binding: ItemBookshelfListBinding) : + RecyclerView.ViewHolder(binding.root) + + class GroupViewHolder(val binding: ItemBookshelfListGroupBinding) : RecyclerView.ViewHolder(binding.root) } \ No newline at end of file diff --git a/app/src/main/res/layout/item_bookshelf_grid_group.xml b/app/src/main/res/layout/item_bookshelf_grid_group.xml new file mode 100644 index 000000000..648d3b85f --- /dev/null +++ b/app/src/main/res/layout/item_bookshelf_grid_group.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_bookshelf_list_group.xml b/app/src/main/res/layout/item_bookshelf_list_group.xml new file mode 100644 index 000000000..00eb4966f --- /dev/null +++ b/app/src/main/res/layout/item_bookshelf_list_group.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file