From 5a6b13de84d030bed5939ce19000e98c54fcc4ed Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 15 Feb 2020 18:31:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/widget/LabelsBar.kt | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/LabelsBar.kt b/app/src/main/java/io/legado/app/ui/widget/LabelsBar.kt index b0bf97779..22f982cf6 100644 --- a/app/src/main/java/io/legado/app/ui/widget/LabelsBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/LabelsBar.kt @@ -3,15 +3,18 @@ package io.legado.app.ui.widget import android.content.Context import android.util.AttributeSet import android.widget.LinearLayout +import android.widget.TextView import io.legado.app.ui.widget.text.AccentBgTextView import io.legado.app.utils.dp class LabelsBar(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) { + private val unUsedViews = arrayListOf() + private val usedViews = arrayListOf() fun setLabels(labels: Array) { - removeAllViews() + clear() labels.forEach { addLabel(it) } @@ -25,18 +28,30 @@ class LabelsBar(context: Context, attrs: AttributeSet?) : LinearLayout(context, } fun clear() { + unUsedViews.addAll(usedViews) + usedViews.clear() removeAllViews() } fun addLabel(label: String) { - addView(AccentBgTextView(context, null).apply { - setPadding(3.dp, 0, 3.dp, 0) - setRadios(2) - val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) - lp.setMargins(0, 0, 2.dp, 0) - layoutParams = lp - text = label - }) + val tv = if (unUsedViews.isEmpty()) { + AccentBgTextView(context, null).apply { + setPadding(3.dp, 0, 3.dp, 0) + setRadios(2) + val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) + lp.setMargins(0, 0, 2.dp, 0) + layoutParams = lp + text = label + usedViews.add(this) + } + } else { + unUsedViews.last().apply { + usedViews.add(this) + unUsedViews.removeAt(unUsedViews.lastIndex) + } + } + tv.text = label + addView(tv) } } \ No newline at end of file