Я перемещаю устаревший пользовательский интерфейс Android в JetPack Compose. Я хочу показать существующий фрагмент (с переработкой, содержащим еще один переработчик в качестве одного из его элементов адаптера) внутри экрана Compose с использованием AndroidViewBinding. Тем не менее, я сталкиваюсь с проблемой, в которой элементы внутреннего переработки имеют ширину 0 и не видны. /> Сначала у меня есть < /p>
< /code>
on Ittem в этом Recycleview имеет еще один переоборудование в виде диаграммы < /p>
, затем я использовал как в Doc от Android, как интегрировать фрагмент, чтобы составить < /p>
< /code>
AndroidViewBinding(FragmentPubinsTabHolderBinding::inflate)
< /code>
Это моя пользовательская диаграмма < /p>
class BarChartView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
private val barView: View
private var max: Double = 1.0
private var position: Double = 0.0
private var horizontal: Boolean = true
private var animator: ValueAnimator? = null
private var barBackground: Drawable
private var animationDuration = ANIMATION_DURATION
init {
barView = View(context)
addView(barView)
barBackground = PaintDrawable(ContextCompat.getColor(context, HoneycombRes.color.background)).apply {
setCornerRadius(resources.getDimension(HoneycombRes.dimen.spacing0_5x))
}
var orientation = LinearLayout.HORIZONTAL
var barTint: Int? = null
attrs?.let { attributes ->
val styledAttributes = context.obtainStyledAttributes(attributes, R.styleable.BarChartView)
styledAttributes.use {
for (i in 0 until styledAttributes.indexCount) {
when (val attr = styledAttributes.getIndex(i)) {
R.styleable.BarChartView_barBackground ->
ResourcesCompat.getDrawable(
resources,
styledAttributes.getResourceIdOrThrow(attr),
context.theme
)?.let { background ->
barBackground = background
}
R.styleable.BarChartView_barOrientation -> orientation = styledAttributes.getInt(attr, LinearLayout.HORIZONTAL)
R.styleable.BarChartView_barColor -> barTint = styledAttributes.getColor(attr, Color.BLACK)
R.styleable.BarChartView_barCornerRadius -> setCornerRadius(
styledAttributes.getDimension(
attr,
resources.getDimension(HoneycombRes.dimen.corner_radius_rounded_default)
)
)
R.styleable.BarChartView_barAnimationDuration ->
animationDuration = styledAttributes.getInt(attr, ANIMATION_DURATION.toInt()).toLong()
R.styleable.BarChartView_barThickness ->
setBarThickness(
styledAttributes.getDimensionPixelSize(
attr,
resources.getDimensionPixelSize(HoneycombRes.dimen.sizing48)
)
)
}
}
}
}
barView.background = barBackground
setOrientation(orientation)
barTint?.let { setColor(it) }
setPositionInt(false)
}
fun setOrientation(orientation: Int) {
horizontal = (orientation == LinearLayout.HORIZONTAL)
when {
horizontal -> barView.updateLayoutParams {
width = 0
height = ViewGroup.LayoutParams.MATCH_PARENT
gravity = Gravity.START
}
!horizontal -> barView.updateLayoutParams {
width = ViewGroup.LayoutParams.MATCH_PARENT
height = 0
gravity = Gravity.BOTTOM
}
}
setPositionInt(false)
}
fun setPosition(position: Double, animate: Boolean = true) {
this.position = position.coerceAtLeast(0.0)
setPositionInt(animate)
}
fun setMax(max: Double, animate: Boolean = true) {
this.max = max.coerceAtLeast(1.0)
setPositionInt(animate)
}
fun setPosition(position: Double, max: Double, animate: Boolean = true) {
this.position = position.coerceAtLeast(0.0)
this.max = max.coerceAtLeast(1.0)
setPositionInt(animate)
}
fun setColor(@ColorInt color: Int) {
barView.backgroundTintList = ColorStateList.valueOf(color)
}
/**
* This function will only work if you do not specify a background in the attributes
*/
fun setCornerRadius(radius: Float) {
(barBackground as? PaintDrawable)?.setCornerRadius(radius)
}
fun setBarBackground(background: Drawable) {
barBackground = background
barView.background = barBackground
}
fun setBarBackground(@DrawableRes id: Int) {
ResourcesCompat.getDrawable(resources, id, context.theme)?.let { background ->
barBackground = background
setBarBackground(barBackground)
}
}
/**
* Please note that this takes precedence over setting the view width
* and/or height in the XML
*/
fun setBarThickness(thickness: Int) {
updateLayoutParams {
if (horizontal) {
height = thickness
} else {
width = thickness
}
}
}
/**
* If used, please call this method before calling setPosition(Double, Boolean),
* or simply set the animation duration on the view attribute itself.
*/
fun setAnimationDuration(animationDuration: Long) {
this.animationDuration = animationDuration
}
private fun setPositionInt(animate: Boolean) {
barView.doOnLayout {
val targetAmount = position / max
val currentSize = if (horizontal) barView.layoutParams.width else barView.layoutParams.height
val maxSize = if (horizontal) width else height
val targetSize = (maxSize.toDouble() * targetAmount).toInt()
if (animate && (currentSize != targetSize)) {
startAnimation(currentSize, targetSize)
} else {
barView.updateLayoutParams {
if (horizontal) width = targetSize else height = targetSize
}
}
}
}
private fun startAnimation(currentSize: Int, targetSize: Int) {
animator?.cancel()
animator = ValueAnimator.ofInt(currentSize, targetSize).apply {
duration = animationDuration
addUpdateListener {
val targetValue = it.animatedValue as? Int ?: targetSize
barView.updateLayoutParams {
if (horizontal) width = targetValue else height = targetValue
}
}
}.also {
it.start()
}
}
companion object {
private const val ANIMATION_DURATION = 600L
}
}
[*] Я думал, что это может быть некоторая проблема с проходом макета, я использовал различные оценки, как doonlayout , он не работает
Я не работал () , что является Adapter in reclevip /> < /li>
< /ul>
Мой подозреваемый заключается в том, что это связано с некоторым временем рендеринга макета, но я не знаю, как это исправить, любая помощь очень признателен < /p>
Я перемещаю устаревший пользовательский интерфейс Android в JetPack Compose. Я хочу показать существующий фрагмент (с переработкой, содержащим еще один переработчик в качестве одного из его элементов адаптера) внутри экрана Compose с использованием AndroidViewBinding. Тем не менее, я сталкиваюсь с проблемой, в которой элементы внутреннего переработки имеют ширину 0 и не видны. /> Сначала у меня есть < /p> [code]
< /code> on Ittem в этом Recycleview имеет еще один переоборудование в виде диаграммы < /p> , затем я использовал как в Doc от Android, как интегрировать фрагмент, чтобы составить < /p>
fun setColor(@ColorInt color: Int) { barView.backgroundTintList = ColorStateList.valueOf(color) }
/** * This function will only work if you do not specify a background in the attributes */ fun setCornerRadius(radius: Float) { (barBackground as? PaintDrawable)?.setCornerRadius(radius) }
/** * Please note that this takes precedence over setting the view width * and/or height in the XML */ fun setBarThickness(thickness: Int) { updateLayoutParams { if (horizontal) { height = thickness } else { width = thickness } } }
/** * If used, please call this method before calling setPosition(Double, Boolean), * or simply set the animation duration on the view attribute itself. */ fun setAnimationDuration(animationDuration: Long) { this.animationDuration = animationDuration }
private fun setPositionInt(animate: Boolean) { barView.doOnLayout { val targetAmount = position / max
val currentSize = if (horizontal) barView.layoutParams.width else barView.layoutParams.height val maxSize = if (horizontal) width else height val targetSize = (maxSize.toDouble() * targetAmount).toInt()
[*] Я думал, что это может быть некоторая проблема с проходом макета, я использовал различные оценки, как doonlayout , он не работает
Я не работал () , что является Adapter in reclevip /> < /li> < /ul> Мой подозреваемый заключается в том, что это связано с некоторым временем рендеринга макета, но я не знаю, как это исправить, любая помощь очень признателен < /p>