如何以编程方式(在运行时)滑动一行 RecyclerView?

Jek*_*eka 5 android android-animation android-layout android-activity android-recyclerview

我有一个 RecyclerView,里面有这样的项目:

在此处输入图片说明

ItemTouchHelper.SimpleCallback当项目被滑动时,我用来监听 swipe 和 onChildDraw() 来绘制画布:

在此处输入图片说明

再滑动一点:

在此处输入图片说明

我的问题:

我想在项目列表中的第一个项目上模拟滑动(在运行时);我需要第一个项目在其 X 轴上(或多或少)-100 px,然后返回到原始位置。如何实现这一目标?

Ole*_*leh 10

我创建了一些实用方法,以防有人需要它

import android.animation.ValueAnimator
import android.os.SystemClock
import android.view.MotionEvent
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

/**
 * @author Oleh Haidaienko
 * @since 29.07.2020
 */
object SwipeUtils {
    /**
     * Programmatically swipe RecyclerView item
     * @param recyclerView RecyclerView which item will be swiped
     * @param index Position of item
     * @param distance Swipe distance
     * @param direction Swipe direction, can be [ItemTouchHelper.START] or [ItemTouchHelper.END]
     * @param time Animation time in milliseconds
     */
    fun swipeRecyclerViewItem(
        recyclerView: RecyclerView,
        index: Int,
        distance: Int,
        direction: Int,
        time: Long
    ) {
        val childView = recyclerView.getChildAt(index) ?: return
        val x = childView.width / 2F
        val viewLocation = IntArray(2)
        childView.getLocationInWindow(viewLocation)
        val y = (viewLocation[1] + childView.height) / 2F
        val downTime = SystemClock.uptimeMillis()
        recyclerView.dispatchTouchEvent(
            MotionEvent.obtain(
                downTime,
                downTime,
                MotionEvent.ACTION_DOWN,
                x,
                y,
                0
            )
        )
        ValueAnimator.ofInt(0, distance).apply {
            duration = time
            addUpdateListener {
                val dX = it.animatedValue as Int
                val mX = when (direction) {
                    ItemTouchHelper.END -> x + dX
                    ItemTouchHelper.START -> x - dX
                    else -> 0F
                }
                recyclerView.dispatchTouchEvent(
                    MotionEvent.obtain(
                        downTime,
                        SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_MOVE,
                        mX,
                        y,
                        0
                    )
                )
            }
        }.start()
    }
}
Run Code Online (Sandbox Code Playgroud)


k0s*_*0sh 0

我想您可以RecyclerView通过简单地注册DataObserver您的帐户来实现这一点Adapter,当某个项目被删除、插入或更改时,它会通知您。让我们考虑一下这种情况

    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override public void onChanged() {
            super.onChanged();
             adapter.unregisterAdapterDataObserver(this);//unregister the adapter since no longer needed.
        }
    });
Run Code Online (Sandbox Code Playgroud)

上面的代码将在插入项目时通知您,然后您可以使用它recyclerView.getChildAt(0).translateX(-100);来模拟滑动,然后简单地 滑动recyclerView.getChildAt(0).translateX(0);到方法中的背面。repositionXviewonChanged()