如何以编程方式启动滑动以使用 RecyclerView 关闭

How to programmatically initiate a swipe to dismiss with RecyclerView

当我从 RecyclerView 中删除一个项目时,我想显示一个“滑动”动画,即该项目从屏幕侧向移动。我相信使用 ItemTouchHelper 支持“滑动关闭”,但这是触摸启动的,而我希望能够以编程方式启动滑动。

我还尝试通过扩展 DefaultItemAnimator 来设置 RecyclerView 项目动画。使用这种方法,我可以让项目左右滑动,但不幸的是,列表中的间隙很快就会关闭,因此滑动不会在列表项目间隙关闭之前完成。

有人知道怎么做吗?

您可以使用 this library 提供的 ItemAnimators。更具体地说,您将使用他们的 SlideInLeftAnimatorSlideInRightAnimator.

虽然已经用外部库回答了;可能有人想用原始 java.. 我已经用

中的解决方案完成了

想法是,您取消要删除的 RecyclerView 的列表项视图并为其设置动画;并通常将其从适配器中移除

private void removeListItem(View rowView, final int position) {

Animation anim = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);

new Handler().postDelayed(new Runnable() {

    public void run() {

        values.remove(position); //Remove the current content from the array

        adapter.notifyDataSetChanged(); //Refresh list
    }

}, anim.getDuration());
}