2

我正在尝试创建一个可以滑动以关闭的视图,但它可以撤消。这些项目在FrameView. 我onChildDrawItemTouchHelper.Callback这样覆盖:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                        float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        if (viewHolder instanceof ItemViewHolder) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;

            holder.foreground.setTranslationX(dX);
        }
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

一切正常,直到我尝试根据用户操作撤消滑动操作,如下所示:

foreground.animate()
          .translationX(0)
          .setDuration(150);

这确实将实际视图返回到它的位置,但是如果用户尝试再次滑动该项目,则会再次显示背景,并且出于某种原因,使用大于屏幕宽度的 dX 参数调用 onChildDraw。点击该项目会在一瞬间显示背景视图,然后可以正常关闭前景。我怎样才能解决这个问题?

4

1 回答 1

1

解决方案是使用getDefaultUIUtil(). ItemTouchHelper.Callback工作代码如下:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                        float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        if (viewHolder instanceof TrackerListAdapter.ItemViewHolder) {
            TrackerListAdapter.ItemViewHolder holder =
                    (TrackerListAdapter.ItemViewHolder) viewHolder;

            getDefaultUIUtil().onDraw(c, recyclerView, holder.foreground, dX, dY, actionState, isCurrentlyActive);
        }
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}
于 2017-07-10T07:56:08.853 回答