发现界面添加自定义样式

pull/1111/head 3.21.070914
gedoor 3 years ago
parent 1f2684d5a1
commit 4230cca24b
  1. 10
      app/src/main/java/io/legado/app/data/entities/ExploreKind.kt
  2. 41
      app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt

@ -3,9 +3,17 @@ package io.legado.app.data.entities
data class ExploreKind( data class ExploreKind(
val title: String, val title: String,
val url: String? = null, 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( data class Style(
val layout_flexGrow: Float = 0F, val layout_flexGrow: Float = 0F,
val layout_flexShrink: Float = 1F, val layout_flexShrink: Float = 1F,

@ -1,10 +1,11 @@
package io.legado.app.ui.main.explore package io.legado.app.ui.main.explore
import android.content.Context import android.content.Context
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.PopupMenu import android.widget.PopupMenu
import android.widget.TextView
import androidx.core.view.children
import com.google.android.flexbox.FlexboxLayout import com.google.android.flexbox.FlexboxLayout
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder 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) : class ExploreAdapter(context: Context, private val scope: CoroutineScope, val callBack: CallBack) :
RecyclerAdapter<BookSource, ItemFindBookBinding>(context) { RecyclerAdapter<BookSource, ItemFindBookBinding>(context) {
private val recycler = arrayListOf<View>()
private var exIndex = -1 private var exIndex = -1
private var scrollTo = -1 private var scrollTo = -1
@ -66,27 +69,24 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca
} }
} }
} else { } else {
binding.ivStatus.setImageResource(R.drawable.ic_arrow_right) ivStatus.setImageResource(R.drawable.ic_arrow_right)
binding.rotateLoading.hide() rotateLoading.hide()
binding.flexbox.gone() recyclerFlexbox(flexbox)
flexbox.gone()
} }
} }
} }
private fun upKindList(flexbox: FlexboxLayout, sourceUrl: String, kinds: List<ExploreKind>) { private fun upKindList(flexbox: FlexboxLayout, sourceUrl: String, kinds: List<ExploreKind>) {
if (!kinds.isNullOrEmpty()) { if (!kinds.isNullOrEmpty()) {
recyclerFlexbox(flexbox)
flexbox.visible() flexbox.visible()
flexbox.removeAllViews() kinds.forEach { kind ->
kinds.map { kind -> val tv = getFlexboxChild(flexbox)
val tv = ItemFilletTextBinding.inflate(
LayoutInflater.from(context),
flexbox,
false
).root
flexbox.addView(tv) flexbox.addView(tv)
tv.text = kind.title tv.text = kind.title
val lp = tv.layoutParams as FlexboxLayout.LayoutParams val lp = tv.layoutParams as FlexboxLayout.LayoutParams
kind.style.let { style -> kind.style().let { style ->
lp.flexGrow = style.layout_flexGrow lp.flexGrow = style.layout_flexGrow
lp.flexShrink = style.layout_flexShrink lp.flexShrink = style.layout_flexShrink
lp.alignSelf = style.alignSelf() 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) { override fun registerListener(holder: ItemViewHolder, binding: ItemFindBookBinding) {
binding.apply { binding.apply {
llTitle.setOnClickListener { llTitle.setOnClickListener {

Loading…
Cancel
Save