feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 8c4e590ad0
commit 5a6b13de84
  1. 21
      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.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView
import io.legado.app.ui.widget.text.AccentBgTextView import io.legado.app.ui.widget.text.AccentBgTextView
import io.legado.app.utils.dp import io.legado.app.utils.dp
class LabelsBar(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) { class LabelsBar(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
private val unUsedViews = arrayListOf<TextView>()
private val usedViews = arrayListOf<TextView>()
fun setLabels(labels: Array<String>) { fun setLabels(labels: Array<String>) {
removeAllViews() clear()
labels.forEach { labels.forEach {
addLabel(it) addLabel(it)
} }
@ -25,18 +28,30 @@ class LabelsBar(context: Context, attrs: AttributeSet?) : LinearLayout(context,
} }
fun clear() { fun clear() {
unUsedViews.addAll(usedViews)
usedViews.clear()
removeAllViews() removeAllViews()
} }
fun addLabel(label: String) { fun addLabel(label: String) {
addView(AccentBgTextView(context, null).apply { val tv = if (unUsedViews.isEmpty()) {
AccentBgTextView(context, null).apply {
setPadding(3.dp, 0, 3.dp, 0) setPadding(3.dp, 0, 3.dp, 0)
setRadios(2) setRadios(2)
val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
lp.setMargins(0, 0, 2.dp, 0) lp.setMargins(0, 0, 2.dp, 0)
layoutParams = lp layoutParams = lp
text = label text = label
}) usedViews.add(this)
}
} else {
unUsedViews.last().apply {
usedViews.add(this)
unUsedViews.removeAt(unUsedViews.lastIndex)
}
}
tv.text = label
addView(tv)
} }
} }
Loading…
Cancel
Save