kotlin 需要使用数据绑定树的MotionLayout引用

dl5txlt9  于 2023-02-24  发布在  Kotlin
关注(0)|答案(1)|浏览(125)

我尝试在RecyclerView中的Fragment中获取对MotionLayout的引用,以便可以附加addTransitionListener。我已准备好侦听器,但不知道此对象的有效路径。
我见过很多引用motionLayout变量的好例子,但是我似乎无法使用DataBinding方法得到一个有效的例子。

我的RecyclerView位于我的片段内,如下所示:

<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/shipper_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:background="@color/mainList"
                tools:listitem="@layout/row_shipper_line"
                />

我的MotionLayout位于我的“row_shipper_line”内,如:

<androidx.constraintlayout.motion.widget.MotionLayout
    android:id="@+id/line_container"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/mainList"
    app:layoutDescription="@xml/row_shipper_line_container_scene"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

我的绑定变量设置为:

binding = DataBindingUtil.inflate(
            inflater,
            R.layout.create_shipper_fragment,
            container,
            false
        )

我输入了以下代码,它会编译,但没有调用事件:

rec = LayoutInflater.from(context).inflate(R.layout.shipper_list, null)

ml = rec.findViewById(R.id.line_container)

var transitionListener = object : MotionLayout.TransitionListener {

     override fun onTransitionStarted(p0: MotionLayout?, startId: Int, endId: Int) {
                Log.d("MyApp", "Transition onTransitionStarted.")
            }

     override fun onTransitionChange(
                p0: MotionLayout?,
                startId: Int,
                endId: Int,
                progress: Float
            ) {
                Log.d("MyApp", "Transition onTransitionChange.")
            }

     override fun onTransitionCompleted(p0: MotionLayout?, currentId: Int) {
                Log.d("MyApp", "Transition onTransitionCompleted.")
            }

      override fun onTransitionTrigger(
                p0: MotionLayout?,
                triggerId: Int,
                positive: Boolean,
                progress: Float
            ) {
                Log.d("MyApp", "Transition onTransitionTrigger.")
            }
        }

 ml.setTransitionListener(transitionListener)

我的线完美地向左滑动。我只需要听onTransitionComplete
我的binding树如下所示:
包含RecyclerViewFragment-〉包含MotionLayout标记的行布局。

我想做的是这样的事

binding.shipper_list.line_container

先谢谢你!

t40tm48m

t40tm48m1#

通过试验,我终于找到了一个解决方案。关键是在onBindViewHolder期间将转换侦听器放置在列表适配器类中。同样,如果您在Fragment中使用DataBinding,这是我找到的唯一解决方案。它立即起作用。经过进一步考虑,将此处理程序放置在列表适配器类中对于保持内容封装更有意义。

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

    holder.bind(shippers[position])

    var ml = holder.itemView.findViewById<MotionLayout>(R.id.uldMotionLayout)

    var transitionListener = object : MotionLayout.TransitionListener {

        override fun onTransitionStarted(p0: MotionLayout?, startId: Int, endId: Int) {
            Log.d("MyApp", "Transition onTransitionStarted.")
        }

        override fun onTransitionChange(
            p0: MotionLayout?,
            startId: Int,
            endId: Int,
            progress: Float
        ) {
            Log.d("MyApp", "Transition onTransitionChange.")
        }

        override fun onTransitionCompleted(p0: MotionLayout?, currentId: Int) {
            //viewToVibrate.vibrate()
            Log.d("MyApp", "Transition onTransitionCompleted.")
        }

        override fun onTransitionTrigger(
            p0: MotionLayout?,
            triggerId: Int,
            positive: Boolean,
            progress: Float
        ) {
            Log.d("MyApp", "Transition onTransitionTrigger.")
        }
    }

    ml.setTransitionListener(transitionListener)
}

相关问题