如何在AndroidKotlin中让Talk Back读取进度条数量而不是百分比

mzsu5hc0  于 2023-05-07  发布在  Kotlin
关注(0)|答案(1)|浏览(157)

我有一个编程制作的进度条,我想让talkback读给我的项目的数量,而不是百分比。我想让他在Instagram上看到同样的内容。故事1/6故事2/6我该怎么解决这个问题?我的进度条形码是这样的,在xml中我只有一个线性布局

private fun storyState.setupStoryProgressBar() {
    if (binding.linearLayoutStoryProgressBar.childCount == 0) { createProgressBars() }
    setProgressBarAnimation(
        binding.linearLayoutStoryProgressBar.getChildAt(currentPosition) as LinearProgressIndicator,
        currentStoryScreen.storyTimeout.toLong(),
        getColorByMode(currentStoryScreen.mode.value)
    )
}

private fun storyState.createProgressBars() {
    var progressBarWidth = context?.resources?.displayMetrics?.widthPixels?.div(stories.size) ?: STORY_PROGRESS_BAR
    progressBarWidth -= (STORY_PROGRESS_BAR_SPACING * 2)
    (0 until stories.size).forEach { _ ->
        val progressBar = LinearProgressIndicator(requireContext())
        progressBar.layoutParams = LinearLayout.LayoutParams(progressBarWidth, STORY_PROGRESS_BAR_SPACING, 1f)
        progressBar.setIndicatorColor(getColorByMode(currentStoryScreen.mode.value))
        progressBar.trackCornerRadius = STORY_PROGRESS_BAR_SPACING
        progressBar.trackThickness = STORY_PROGRESS_BAR_SPACING
        progressBar.trackColor = Color.parseColor("#A79AA9")
        progressBar.alpha = STORY_PROGRESS_BAR_ALPHA
        progressBar.setPadding(STORY_PROGRESS_BAR_SPACING, 0, STORY_PROGRESS_BAR_SPACING, 0)
        binding.linearLayoutStoryProgressBar.addView(progressBar)
    }
    binding.linearLayoutStoryProgressBar.requestLayout()
}


private fun setProgressBarAnimation(progressBar: LinearProgressIndicator, seconds: Long, color: Int) {
    resetProgressBars(color)
    val animator = ObjectAnimator.ofInt(progressBar, "progress", 0, STORY_PROGRESS_BAR)
    animator.duration = seconds * STORY_PROGRESS_BAR_ANIMATION_DURATION
    val screenPosition = viewModel.getCurrentPosition()
    animator.start()
    animator.setAutoCancel(true)
    animator.doOnEnd {
        if (viewModel.getCurrentPosition() == screenPosition && isTalkbackOff()) {
            goToNextStory()

        }
    }
}

private fun resetProgressBars(color: Int) {
    val totalStories = binding.linearLayoutStoryProgressBar.childCount
    val currentPosition = viewModel.getCurrentPosition()
    (0 until totalStories).forEach {
        val progressBar = binding.linearLayoutStoryProgressBar.getChildAt(it) as LinearProgressIndicator
        progressBar.setIndicatorColor(color)
        if (it >= currentPosition || currentPosition == totalStories) {
            resetProgressBarAnimation(progressBar, 0)
        } else {
            resetProgressBarAnimation(progressBar, STORY_PROGRESS_BAR)
        }
    }
}

private fun resetProgressBarAnimation(progressBar: LinearProgressIndicator, amount: Int) {
    progressBar.clearAnimation()
    val animator = ObjectAnimator.ofInt(progressBar, "progress", amount, amount)
    animator.duration = 0
    animator.start()
    progressBar.progress = amount
}
v1l68za4

v1l68za41#

每次当进度移动到下一个故事时,您需要为linearLayoutStoryProgressBar设置contentDescription。您没有提供所有代码,但我认为它将在goToNextStory()方法中:

fun goToNextStory(currentStory: Int) {
    ...
    linearLayoutStoryProgressBar.contentDescription = "Story ${currentStory} of ${stories.size}"
    ...
}

相关问题