diff --git a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt index 445abca97..e73ca9da7 100644 --- a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt @@ -1,6 +1,9 @@ package io.legado.app.ui.bookshelf import android.os.Bundle +import android.widget.LinearLayout +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.utils.getViewModel @@ -12,6 +15,8 @@ class BookshelfActivity : BaseActivity() { override val layoutID: Int get() = R.layout.activity_bookshelf + private lateinit var bookshelfAdapter: BookshelfAdapter + override fun onViewModelCreated(viewModel: BookshelfViewModel, savedInstanceState: Bundle?) { if (viewModel.bookGroup == null) { viewModel.bookGroup = intent.getParcelableExtra("data") @@ -19,6 +24,23 @@ class BookshelfActivity : BaseActivity() { viewModel.bookGroup?.let { title_bar.title = it.groupName } + initRecyclerView() + upRecyclerData() + } + + private fun initRecyclerView() { + rv_bookshelf.layoutManager = LinearLayoutManager(this) + rv_bookshelf.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL)) + bookshelfAdapter = BookshelfAdapter() + rv_bookshelf.adapter = bookshelfAdapter + } + + private fun upRecyclerData() { + viewModel.bookGroup?.let { + when (it.groupId) { + + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt new file mode 100644 index 000000000..b7fdf7b8d --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt @@ -0,0 +1,87 @@ +package io.legado.app.ui.bookshelf + +import android.text.TextUtils.isEmpty +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 com.bumptech.glide.Glide +import com.bumptech.glide.load.engine.DiskCacheStrategy +import io.legado.app.R +import io.legado.app.data.entities.Book +import io.legado.app.lib.theme.ThemeStore +import kotlinx.android.synthetic.main.item_bookshelf_list.view.* +import kotlinx.android.synthetic.main.item_relace_rule.view.tv_name +import java.io.File + +class BookshelfAdapter : PagedListAdapter(DIFF_CALLBACK) { + + companion object { + @JvmField + val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: Book, newItem: Book): Boolean = + oldItem.descUrl == newItem.descUrl + + override fun areContentsTheSame(oldItem: Book, newItem: Book): Boolean = + oldItem.descUrl == newItem.descUrl + && oldItem.durChapterTitle == newItem.durChapterTitle + && oldItem.latestChapterTitle == newItem.latestChapterTitle + } + } + + var callBack: CallBack? = null + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { + return MyViewHolder( + LayoutInflater.from(parent.context).inflate( + R.layout.item_bookshelf_list, + parent, + false + ) + ) + } + + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { + currentList?.get(position)?.let { + holder.bind(it, callBack) + } + } + + class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + init { + itemView.setBackgroundColor(ThemeStore.backgroundColor(itemView.context)) + } + + fun bind(book: Book, callBack: CallBack?) = with(itemView) { + tv_name.text = book.name + tv_author.text = book.author + tv_read.text = book.durChapterTitle + tv_last.text = book.latestChapterTitle + val cover = if (isEmpty(book.customCoverUrl)) book.coverUrl else book.customCoverUrl + cover?.let { + if (it.startsWith("http")) { + Glide.with(itemView).load(it) + .placeholder(R.drawable.img_cover_default) + .centerCrop() + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .into(iv_cover) + } else { + Glide.with(itemView).load(File(it)) + .placeholder(R.drawable.img_cover_default) + .centerCrop() + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .into(iv_cover) + } + } + itemView.setOnClickListener { callBack?.open(book) } + } + } + + interface CallBack { + fun open(book: Book) + fun search() + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_bookshelf.xml b/app/src/main/res/layout/activity_bookshelf.xml index 49c8c9d5d..a18920e81 100644 --- a/app/src/main/res/layout/activity_bookshelf.xml +++ b/app/src/main/res/layout/activity_bookshelf.xml @@ -9,4 +9,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + + \ No newline at end of file