区别搜索的adapter。搜索会单独高亮,并且使用PdfDiffCallback,因为搜索会频繁的刷新列表。
This commit is contained in:
parent
e15837b557
commit
9dfa05ef97
@ -73,11 +73,6 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
viewModel.pdfDocument.observe(this) { document ->
|
||||
document?.let {
|
||||
pdfDocument = it
|
||||
// 只有文件路径变化才重新加载 PDF
|
||||
// if (lastLoadedFilePath != pdfDocument.filePath) {
|
||||
// lastLoadedFilePath = pdfDocument.filePath
|
||||
// loadPdf()
|
||||
// }
|
||||
loadPdf()
|
||||
initView()
|
||||
} ?: run {
|
||||
@ -113,32 +108,6 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
showToast(getString(R.string.bookmark_add_fail))
|
||||
}
|
||||
}
|
||||
|
||||
// viewModel.fileActionEvent.observeEvent<FileActionEvent.Rename>(this) { event ->
|
||||
// if (event.renameResult.success) {
|
||||
// showToast(getString(R.string.rename_successfully))
|
||||
// event.renameResult.newFilePath?.let {
|
||||
// //修改名称需要重新load界面,因为路径都更改了
|
||||
// viewModel.getPDFDocument(it)
|
||||
// }
|
||||
// } else {
|
||||
// showToast(event.renameResult.errorMessage.toString())
|
||||
// }
|
||||
// }
|
||||
// viewModel.fileActionEvent.observeEvent<FileActionEvent.Duplicate>(this) { event ->
|
||||
// if (event.file != null) {
|
||||
// showToast(getString(R.string.duplicate_created_successfully))
|
||||
// } else {
|
||||
// showToast(getString(R.string.duplicate_created_failed))
|
||||
// }
|
||||
// }
|
||||
// viewModel.fileActionEvent.observeEvent<FileActionEvent.Favorite>(this) { event ->
|
||||
// if (event.isFavorite) {
|
||||
// showToast(getString(R.string.added_to_favorites))
|
||||
// } else {
|
||||
// showToast(getString(R.string.removed_from_favorites))
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
|
||||
@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.ActivitySearchPddBinding
|
||||
import com.all.pdfreader.pro.app.ui.adapter.PdfAdapter
|
||||
import com.all.pdfreader.pro.app.ui.adapter.SearchPdfAdapter
|
||||
import com.all.pdfreader.pro.app.ui.dialog.ListMoreDialogFragment
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
@ -32,7 +33,7 @@ class SearchActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private lateinit var binding: ActivitySearchPddBinding
|
||||
private lateinit var adapter: PdfAdapter
|
||||
private lateinit var adapter: SearchPdfAdapter
|
||||
private val pdfRepository = getRepository()
|
||||
private var searchJob: Job? = null
|
||||
|
||||
@ -92,7 +93,7 @@ class SearchActivity : BaseActivity() {
|
||||
|
||||
private fun initView() {
|
||||
binding.searchEdit.showKeyboard()
|
||||
adapter = PdfAdapter(onItemClick = { pdf ->
|
||||
adapter = SearchPdfAdapter(onItemClick = { pdf ->
|
||||
val intent = PdfViewActivity.createIntent(this, pdf.filePath)
|
||||
startActivity(intent)
|
||||
}, onMoreClick = { pdf ->
|
||||
|
||||
@ -4,88 +4,62 @@ import android.annotation.SuppressLint
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.AdapterPdfItemBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.dpToPx
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.toHighlightedSpannable
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toFormatFileSize
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toSlashDate
|
||||
import com.all.pdfreader.pro.app.util.PdfDiffCallback
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.resource.bitmap.CenterCrop
|
||||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
|
||||
|
||||
class PdfAdapter(
|
||||
private var pdfList: MutableList<PdfDocumentEntity>,
|
||||
private val onItemClick: (PdfDocumentEntity) -> Unit,
|
||||
private val onMoreClick: (PdfDocumentEntity) -> Unit
|
||||
) : ListAdapter<PdfDocumentEntity, PdfAdapter.PdfViewHolder>(PdfDiffCallback()) {
|
||||
) : RecyclerView.Adapter<PdfAdapter.PdfViewHolder>() {
|
||||
|
||||
inner class PdfViewHolder(val binding: AdapterPdfItemBinding) :
|
||||
RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PdfViewHolder {
|
||||
val binding = AdapterPdfItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context), parent, false
|
||||
)
|
||||
val binding =
|
||||
AdapterPdfItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
return PdfViewHolder(binding)
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: PdfViewHolder, position: Int) {
|
||||
bindItem(holder, getItem(position), null)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PdfViewHolder, position: Int, payloads: MutableList<Any>) {
|
||||
if (payloads.isNotEmpty()) {
|
||||
val payload = payloads[0] as? String
|
||||
bindItem(holder, getItem(position), payload)
|
||||
} else {
|
||||
super.onBindViewHolder(holder, position, payloads)
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindItem(holder: PdfViewHolder, item: PdfDocumentEntity, highlightKeyword: String?) {
|
||||
val context = holder.binding.root.context
|
||||
|
||||
// 文件名高亮,如果 highlightKeyword 为 null 或空字符串,就显示普通文本
|
||||
holder.binding.tvFileName.text = if (highlightKeyword.isNullOrBlank()) {
|
||||
item.fileName
|
||||
} else {
|
||||
item.fileName.toHighlightedSpannable(highlightKeyword, holder.binding.root.context.getColor(R.color.icon_sel_on_color))
|
||||
}
|
||||
|
||||
val item = pdfList[position]
|
||||
holder.binding.tvFileName.text = item.fileName
|
||||
holder.binding.tvFileSize.text = item.fileSize.toFormatFileSize()
|
||||
holder.binding.tvFileDate.text = item.lastModified.toSlashDate()
|
||||
|
||||
if (item.isPassword) {
|
||||
holder.binding.lockLayout.visibility = View.VISIBLE
|
||||
holder.binding.tvFileImg.visibility = View.GONE
|
||||
} else {
|
||||
holder.binding.lockLayout.visibility = View.GONE
|
||||
holder.binding.tvFileImg.visibility = View.VISIBLE
|
||||
Glide.with(context)
|
||||
.load(item.thumbnailPath)
|
||||
.transform(CenterCrop(), RoundedCorners(8.dpToPx(context)))
|
||||
Glide.with(holder.binding.root).load(item.thumbnailPath)
|
||||
.transform(CenterCrop(), RoundedCorners(8.dpToPx(holder.binding.root.context)))
|
||||
.into(holder.binding.tvFileImg)
|
||||
}
|
||||
|
||||
holder.binding.root.setOnClickListener { onItemClick(item) }
|
||||
holder.binding.moreBtn.setOnClickListener { onMoreClick(item) }
|
||||
holder.binding.root.setOnClickListener {
|
||||
onItemClick(item)
|
||||
}
|
||||
holder.binding.moreBtn.setOnClickListener {
|
||||
onMoreClick(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = pdfList.size
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun updateData(newList: List<PdfDocumentEntity>) {
|
||||
submitList(newList.toList())
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索场景使用:只刷新高亮,不刷新整个列表
|
||||
*/
|
||||
fun highlightItems(keyword: String) {
|
||||
for (i in 0 until itemCount) {
|
||||
notifyItemChanged(i, keyword)
|
||||
}
|
||||
pdfList.clear()
|
||||
pdfList.addAll(newList)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
package com.all.pdfreader.pro.app.ui.adapter
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.AdapterPdfItemBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.dpToPx
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.toHighlightedSpannable
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toFormatFileSize
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toSlashDate
|
||||
import com.all.pdfreader.pro.app.util.PdfDiffCallback
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.resource.bitmap.CenterCrop
|
||||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
|
||||
|
||||
class SearchPdfAdapter(
|
||||
private val onItemClick: (PdfDocumentEntity) -> Unit,
|
||||
private val onMoreClick: (PdfDocumentEntity) -> Unit
|
||||
) : ListAdapter<PdfDocumentEntity, SearchPdfAdapter.PdfViewHolder>(PdfDiffCallback()) {
|
||||
|
||||
inner class PdfViewHolder(val binding: AdapterPdfItemBinding) :
|
||||
RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PdfViewHolder {
|
||||
val binding = AdapterPdfItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context), parent, false
|
||||
)
|
||||
return PdfViewHolder(binding)
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: PdfViewHolder, position: Int) {
|
||||
bindItem(holder, getItem(position), null)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PdfViewHolder, position: Int, payloads: MutableList<Any>) {
|
||||
if (payloads.isNotEmpty()) {
|
||||
val payload = payloads[0] as? String
|
||||
bindItem(holder, getItem(position), payload)
|
||||
} else {
|
||||
super.onBindViewHolder(holder, position, payloads)
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindItem(holder: PdfViewHolder, item: PdfDocumentEntity, highlightKeyword: String?) {
|
||||
val context = holder.binding.root.context
|
||||
|
||||
// 文件名高亮,如果 highlightKeyword 为 null 或空字符串,就显示普通文本
|
||||
holder.binding.tvFileName.text = if (highlightKeyword.isNullOrBlank()) {
|
||||
item.fileName
|
||||
} else {
|
||||
item.fileName.toHighlightedSpannable(highlightKeyword, holder.binding.root.context.getColor(R.color.icon_sel_on_color))
|
||||
}
|
||||
|
||||
holder.binding.tvFileSize.text = item.fileSize.toFormatFileSize()
|
||||
holder.binding.tvFileDate.text = item.lastModified.toSlashDate()
|
||||
|
||||
if (item.isPassword) {
|
||||
holder.binding.lockLayout.visibility = View.VISIBLE
|
||||
holder.binding.tvFileImg.visibility = View.GONE
|
||||
} else {
|
||||
holder.binding.lockLayout.visibility = View.GONE
|
||||
holder.binding.tvFileImg.visibility = View.VISIBLE
|
||||
Glide.with(context)
|
||||
.load(item.thumbnailPath)
|
||||
.transform(CenterCrop(), RoundedCorners(8.dpToPx(context)))
|
||||
.into(holder.binding.tvFileImg)
|
||||
}
|
||||
|
||||
holder.binding.root.setOnClickListener { onItemClick(item) }
|
||||
holder.binding.moreBtn.setOnClickListener { onMoreClick(item) }
|
||||
}
|
||||
|
||||
fun updateData(newList: List<PdfDocumentEntity>) {
|
||||
submitList(newList.toList())
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索场景使用:只刷新高亮,不刷新整个列表
|
||||
*/
|
||||
fun highlightItems(keyword: String) {
|
||||
for (i in 0 until itemCount) {
|
||||
notifyItemChanged(i, keyword)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import com.all.pdfreader.pro.app.model.RenameType
|
||||
import com.all.pdfreader.pro.app.room.entity.BookmarkEntity
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.ui.adapter.BookmarkAdapter
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import com.all.pdfreader.pro.app.databinding.DialogFileDetailsBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toFormatFileSize
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toSlashDateTime
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
|
||||
|
||||
@ -24,6 +24,7 @@ import com.all.pdfreader.pro.app.util.AppUtils.setClickWithAnimation
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.shareFile
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toFormatFileSize
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.toSlashDate
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.resource.bitmap.CenterCrop
|
||||
|
||||
@ -15,6 +15,7 @@ import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.DialogPdfSetPasswordBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import kotlin.getValue
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ import androidx.core.graphics.drawable.toDrawable
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.DialogDeleteBinding
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
|
||||
class PromptDialogFragment(
|
||||
private val title: String,
|
||||
|
||||
@ -17,6 +17,7 @@ import com.all.pdfreader.pro.app.model.RenameType
|
||||
import com.all.pdfreader.pro.app.room.entity.BookmarkEntity
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
||||
import com.all.pdfreader.pro.app.util.FileUtils
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import java.io.File
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.sp.AppStore
|
||||
import com.all.pdfreader.pro.app.ui.view.CustomSwitchButton
|
||||
import com.all.pdfreader.pro.app.ui.view.CustomSwitchButton.OnCheckedChangeListener
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
|
||||
|
||||
@ -40,7 +40,7 @@ class FavoriteFrag : BaseFrag() {
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
adapter = PdfAdapter(onItemClick = { pdf ->
|
||||
adapter = PdfAdapter(pdfList = mutableListOf(),onItemClick = { pdf ->
|
||||
val intent = PdfViewActivity.createIntent(requireContext(), pdf.filePath)
|
||||
startActivity(intent)
|
||||
}, onMoreClick = { pdf ->
|
||||
|
||||
@ -42,7 +42,7 @@ class HomeFrag : BaseFrag(), MainActivity.SortableFragment {
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
adapter = PdfAdapter(onItemClick = { pdf ->
|
||||
adapter = PdfAdapter(pdfList = mutableListOf(), onItemClick = { pdf ->
|
||||
val intent = PdfViewActivity.createIntent(requireContext(), pdf.filePath)
|
||||
startActivity(intent)
|
||||
}, onMoreClick = { pdf ->
|
||||
|
||||
@ -39,7 +39,7 @@ class RecentlyFrag : BaseFrag() {
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
adapter = PdfAdapter(onItemClick = { pdf ->
|
||||
adapter = PdfAdapter(pdfList = mutableListOf(),onItemClick = { pdf ->
|
||||
val intent = PdfViewActivity.createIntent(requireContext(), pdf.filePath)
|
||||
startActivity(intent)
|
||||
}, onMoreClick = { pdf ->
|
||||
|
||||
Loading…
Reference in New Issue
Block a user