From 4230cca24b5827ac9926db4f87c77a5ff9b85116 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 9 Jul 2021 14:48:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E7=95=8C=E9=9D=A2=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/data/entities/ExploreKind.kt | 10 ++++- .../app/ui/main/explore/ExploreAdapter.kt | 41 +++++++++++++------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/ExploreKind.kt b/app/src/main/java/io/legado/app/data/entities/ExploreKind.kt index f683a7fef..5a44bd419 100644 --- a/app/src/main/java/io/legado/app/data/entities/ExploreKind.kt +++ b/app/src/main/java/io/legado/app/data/entities/ExploreKind.kt @@ -3,9 +3,17 @@ package io.legado.app.data.entities data class ExploreKind( val title: String, val url: String? = null, - val style: Style = Style() + val style: Style? = null ) { + companion object { + val defaultStyle = Style() + } + + fun style(): Style { + return style ?: defaultStyle + } + data class Style( val layout_flexGrow: Float = 0F, val layout_flexShrink: Float = 1F, diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt index 85c1a03b2..d780b6dd3 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt @@ -1,10 +1,11 @@ package io.legado.app.ui.main.explore import android.content.Context -import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.PopupMenu +import android.widget.TextView +import androidx.core.view.children import com.google.android.flexbox.FlexboxLayout import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder @@ -25,6 +26,8 @@ import splitties.views.onLongClick class ExploreAdapter(context: Context, private val scope: CoroutineScope, val callBack: CallBack) : RecyclerAdapter(context) { + + private val recycler = arrayListOf() private var exIndex = -1 private var scrollTo = -1 @@ -66,27 +69,24 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca } } } else { - binding.ivStatus.setImageResource(R.drawable.ic_arrow_right) - binding.rotateLoading.hide() - binding.flexbox.gone() + ivStatus.setImageResource(R.drawable.ic_arrow_right) + rotateLoading.hide() + recyclerFlexbox(flexbox) + flexbox.gone() } } } private fun upKindList(flexbox: FlexboxLayout, sourceUrl: String, kinds: List) { if (!kinds.isNullOrEmpty()) { + recyclerFlexbox(flexbox) flexbox.visible() - flexbox.removeAllViews() - kinds.map { kind -> - val tv = ItemFilletTextBinding.inflate( - LayoutInflater.from(context), - flexbox, - false - ).root + kinds.forEach { kind -> + val tv = getFlexboxChild(flexbox) flexbox.addView(tv) tv.text = kind.title val lp = tv.layoutParams as FlexboxLayout.LayoutParams - kind.style.let { style -> + kind.style().let { style -> lp.flexGrow = style.layout_flexGrow lp.flexShrink = style.layout_flexShrink lp.alignSelf = style.alignSelf() @@ -100,6 +100,23 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca } } + @Synchronized + private fun getFlexboxChild(flexbox: FlexboxLayout): TextView { + return if (recycler.isEmpty()) { + ItemFilletTextBinding.inflate(inflater, flexbox, false).root + } else { + recycler.last().also { + recycler.removeLast() + } as TextView + } + } + + @Synchronized + private fun recyclerFlexbox(flexbox: FlexboxLayout) { + recycler.addAll(flexbox.children) + flexbox.removeAllViews() + } + override fun registerListener(holder: ItemViewHolder, binding: ItemFindBookBinding) { binding.apply { llTitle.setOnClickListener {