【问题标题】:MotionLayout Expanding Recycler List View - how to reset a transitionMotionLayout 扩展回收器列表视图 - 如何重置过渡
【发布时间】:2018-11-30 14:39:56
【问题描述】:

我觉得我很傻, 我想在我的 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()

                }
            }
        }

    }

【问题讨论】:

    标签: android recyclerview-layout android-motionlayout


    【解决方案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 的工作扩展/收缩列表适配器,它看起来很不错,但可能有更好的方法可以做到这一点,,抱歉不能分享任何图片。

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多