1

我觉得我很傻,我想在我的 RecyclerView 中的 ViewHolder 上使用 MotionLayout 在两种状态之间进行动画处理(当前播放的歌曲被扩展,而最后播放的歌曲被缩小)但是似乎 recyclerview 太好了,它只是改变内容而不改变视图,即当当前播放的歌曲改变时,视图已经处于结束转换状态,所以我的转换什么都不做。同样,我之前展开的项目被反弹到关闭状态,所以我的动画什么也不做。

所以好吧,我想让我们检查转换的状态并设置进度,但是如果我在前一行设置进度,这会导致转换无法运行。我试过了,增加了一些延迟,但没有真正的改善,

我觉得我可能过度设计了这个,或者我错过了一些关于如何重置运动布局动画的基本知识。任何帮助将不胜感激。

override fun onBindViewHolder(holder: SongViewHolder, position: Int) {

        if (songs.isNotEmpty() && position < songs.size) {
            val current = songs[position]
            holder.songName?.text = current.song
            holder.albumArt?.setImageResource(current.albumArtId)
            holder.artistName?.text = current.artist
            var ml = holder.motionLayout

            if (current.currentPlaying){
                //The view is recycled so its already in the end state... so set it to the start state and animate
                if (ml?.progress!! > 0f) {
                    ml?.progress = 0f //<- This resets the animation state
                }

                ml?.transitionToEnd() <- but at this point the animation does not work if i have manually set the progress the line above
            }else{

                if (current.previoussong){
                    //The view that was last expended is not in the end state, so set it then transation to start
                    if (ml?.progress!! < 1f) {
                        ml?.progress = 1f
                    }

                    ml?.transitionToStart()

                }
            }
        }

    }
4

1 回答 1

1

好吧,如果有人有同样的问题,我找到了解决我困境的“一个”答案。

我没有设置进度,而是明确设置了过渡,然后要求它过渡到结束,这适用于扩展。

为了让收缩工作,我必须创建一个带有倒置动态场景的不同初始布局,然后过渡到最后,并在我的 creatViewHolder 中将“previoussong”设置为不同的 viewType

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongViewHolder {

        //Use the int to switch?
        val itemView : View = when (viewType) {
            TYPEPLAYING -> inflater.inflate(R.layout.list_motion_layout, parent, false)
            TYPEUPCOMING -> inflater.inflate(R.layout.list_motion_layout, parent, false) 
            TYPEPREVIOUS -> inflater.inflate(R.layout.list_motion_layout_shrink, parent, false)
        else
           -> inflater.inflate(R.layout.footer, parent, false)
        }

       ...
    }

    override fun onBindViewHolder(holder: SongViewHolder, position: Int) {

        if (songs.isNotEmpty() && position < songs.size) {
            val current = songs[position]
            holder.songName?.text = current.song
            holder.albumArt?.setImageResource(current.albumArtId)
            holder.artistName?.text = current.artist

            var ml = holder.motionLayout
            if (current.currentPlaying){

                ml?.setTransition(list_motion_layout_start, currentplaying_song)
                ml?.transitionToEnd()

            }
        if (current.previouslyPlaying) {
             ml?.setTransition(currentplaying_song, list_motion_layout_start) // Not sure if this is actually required
            ml?.transitionToEnd()

        }

    }

}

总而言之,我有一个使用motionview的工作扩展/收缩列表适配器,它看起来很不错,但可能有更好的方法可以做到这一点,,抱歉不能分享任何照片。

于 2018-11-30T14:49:09.840 回答