修复发现列表错位

This commit is contained in:
HapeLee
2025-09-18 02:14:15 +08:00
parent 2ecd2507b7
commit e486a2eda8
2 changed files with 54 additions and 26 deletions
@@ -35,8 +35,16 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
RecyclerAdapter<BookSourcePart, ItemFindBookBinding>(context) { RecyclerAdapter<BookSourcePart, ItemFindBookBinding>(context) {
private val recycler = arrayListOf<View>() private val recycler = arrayListOf<View>()
private var exIndex = -1 private var expandedId: String? = null
private var scrollTo = -1 private var scrollToId: String? = null
init {
setHasStableIds(true)
}
override fun getItemId(position: Int): Long {
return getItem(position)?.bookSourceUrl.hashCode().toLong()
}
override fun getViewBinding(parent: ViewGroup): ItemFindBookBinding { override fun getViewBinding(parent: ViewGroup): ItemFindBookBinding {
return ItemFindBookBinding.inflate(inflater, parent, false) return ItemFindBookBinding.inflate(inflater, parent, false)
@@ -53,12 +61,16 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
tvName.text = item.bookSourceName tvName.text = item.bookSourceName
} }
if (exIndex == holder.layoutPosition) { val isExpanded = (item.bookSourceUrl == expandedId)
if (isExpanded) {
ivStatus.icon = ContextCompat.getDrawable(context, R.drawable.ic_arrow_right) ivStatus.icon = ContextCompat.getDrawable(context, R.drawable.ic_arrow_right)
ivStatus.rotation = 90f ivStatus.rotation = 90f
if (scrollTo >= 0) { if (scrollToId == item.bookSourceUrl) {
callBack.scrollTo(scrollTo) val pos = holder.bindingAdapterPosition
if (pos >= 0) callBack.scrollTo(pos)
scrollToId = null
} }
Coroutine.async(callBack.scope) { Coroutine.async(callBack.scope) {
@@ -102,7 +114,6 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
} }
} }
@Synchronized @Synchronized
private fun getFlexboxChild(flexbox: FlexboxLayout): MaterialCardView { private fun getFlexboxChild(flexbox: FlexboxLayout): MaterialCardView {
return if (recycler.isEmpty()) { return if (recycler.isEmpty()) {
@@ -121,33 +132,43 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
override fun registerListener(holder: ItemViewHolder, binding: ItemFindBookBinding) { override fun registerListener(holder: ItemViewHolder, binding: ItemFindBookBinding) {
binding.apply { binding.apply {
llTitle.setOnClickListener { llTitle.setOnClickListener {
val position = holder.layoutPosition val item = getItem(holder.bindingAdapterPosition) ?: return@setOnClickListener
val oldEx = exIndex val oldId = expandedId
exIndex = if (exIndex == position) -1 else position expandedId = if (expandedId == item.bookSourceUrl) null else item.bookSourceUrl
notifyItemChanged(oldEx, false) notifyItemChangedById(oldId)
if (exIndex != -1) { notifyItemChangedById(expandedId)
scrollTo = position
callBack.scrollTo(position) if (expandedId != null) {
notifyItemChanged(position, false) scrollToId = expandedId
callBack.scrollTo(holder.bindingAdapterPosition)
} }
} }
llTitle.onLongClick { llTitle.onLongClick {
showMenu(llTitle, holder.layoutPosition) showMenu(llTitle, holder.bindingAdapterPosition)
} }
} }
} }
fun compressExplore(): Boolean { fun compressExplore(): Boolean {
return if (exIndex < 0) { return if (expandedId == null) {
false false
} else { } else {
val oldExIndex = exIndex val oldId = expandedId
exIndex = -1 expandedId = null
notifyItemChanged(oldExIndex) notifyItemChangedById(oldId)
true true
} }
} }
private fun notifyItemChangedById(id: String?) {
id ?: return
for (pos in 0 until itemCount) {
val item = getItem(pos) ?: continue
if (item.bookSourceUrl == id) {
notifyItemChanged(pos)
break
}
}
}
private fun showMenu(view: View, position: Int) { private fun showMenu(view: View, position: Int) {
val source = getItem(position) ?: return val source = getItem(position) ?: return
val popupMenu = PopupMenu(context, view) val popupMenu = PopupMenu(context, view)
@@ -162,13 +183,11 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
putExtra("type", "bookSource") putExtra("type", "bookSource")
putExtra("key", source.bookSourceUrl) putExtra("key", source.bookSourceUrl)
} }
R.id.menu_refresh -> Coroutine.async(callBack.scope) { R.id.menu_refresh -> Coroutine.async(callBack.scope) {
source.clearExploreKindsCache() source.clearExploreKindsCache()
}.onSuccess { }.onSuccess {
notifyItemChanged(position) notifyItemChanged(position)
} }
R.id.menu_del -> callBack.deleteSource(source) R.id.menu_del -> callBack.deleteSource(source)
} }
true true
@@ -188,11 +188,13 @@ class ExploreFragment() : VMBaseFragment<ExploreViewModel>(R.layout.fragment_exp
private fun upGroupsMenu() = groupsMenu?.transaction { subMenu -> private fun upGroupsMenu() = groupsMenu?.transaction { subMenu ->
subMenu.removeGroup(R.id.menu_group_text) subMenu.removeGroup(R.id.menu_group_text)
subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, getString(R.string.all))
groups.forEach { groups.forEach {
subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it) subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it)
} }
} }
override val scope: CoroutineScope override val scope: CoroutineScope
get() = viewLifecycleOwner.lifecycleScope get() = viewLifecycleOwner.lifecycleScope
@@ -205,9 +207,15 @@ class ExploreFragment() : VMBaseFragment<ExploreViewModel>(R.layout.fragment_exp
override fun onCompatOptionsItemSelected(item: MenuItem) { override fun onCompatOptionsItemSelected(item: MenuItem) {
super.onCompatOptionsItemSelected(item) super.onCompatOptionsItemSelected(item)
if (item.groupId == R.id.menu_group_text) { if (item.groupId == R.id.menu_group_text) {
val query = "group:${item.title}" val title = item.title.toString()
if (title == getString(R.string.all)) {
searchView.setText("")
upExploreData(null)
} else {
val query = "group:$title"
searchView.setText(query) searchView.setText(query)
upExploreData(query) upExploreData(query)
}
} else if (item.itemId == R.id.menu_exp_search) { } else if (item.itemId == R.id.menu_exp_search) {
TransitionManager.beginDelayedTransition(binding.rootView) TransitionManager.beginDelayedTransition(binding.rootView)
binding.searchBar.visibility = binding.searchBar.visibility =
@@ -215,6 +223,7 @@ class ExploreFragment() : VMBaseFragment<ExploreViewModel>(R.layout.fragment_exp
} }
} }
override fun scrollTo(pos: Int) { override fun scrollTo(pos: Int) {
(binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0) (binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0)
} }