Как запустить анимацию в RecyclerView ViewHolder с помощью Runnable и ThreadHandler - PullRequest
0 голосов
/ 28 октября 2019

У меня есть пользовательский вид с анимацией точек. Этот вид находится внутри ViewHolder. Моя проблема в том, что когда представление перерабатывается, Runnable все еще работает.

class TransactionPendingStatusView : ConstraintLayout {

    constructor(context: Context) : super(context) {
        initializeViews()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initializeViews()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initializeViews()
    }

    private fun initializeViews() {
        inflate(context, R.layout.view_transaction_pending_status, this)
    }

    private var threadHandler: Handler? = null
    private var runnable: Runnable? = null
    private var animationDelayTime: Long = 150

    fun startAnimation() {
        if (threadHandler == null && runnable == null) {
            var lastAnimatedIndex = 1
            threadHandler = Handler()
            runnable = object : Runnable {
                override fun run() {

                    threadHandler?.postDelayed(this, animationDelayTime)
                    if (lastAnimatedIndex == 1) {
                        pendingText.text = context.getString(R.string.Transactions_PendingOneDot)
                        lastAnimatedIndex = 2
                    } else if (lastAnimatedIndex == 2) {
                        pendingText.text = context.getString(R.string.Transactions_PendingTwoDots)
                        lastAnimatedIndex = 3
                    } else if (lastAnimatedIndex == 3) {
                        pendingText.text = context.getString(R.string.Transactions_PendingThreeDots)
                        lastAnimatedIndex = 4
                    } else {
                        pendingText.text = context.getString(R.string.Transactions_Pending)
                        lastAnimatedIndex = 1
                    }
                    invalidate()
                }
            }
            runnable?.run()
        }
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...