Android在Cardview上使用Recyclerview中的itemTouchHelper拖动高程平移z

时间:2018-10-18 11:16:42

标签: android android-recyclerview cardview android-elevation

我在解决如何在拖动时暂时赋予cardViews高度时遇到问题。我将回收站视图与cardViews一起使用,这是我的itemtouchhelper:

class ListTouchHelper extends ItemTouchHelper.Callback {

    private final ActionCompletionContract contract;

    public ListTouchHelper(ActionCompletionContract contract) {
        this.contract = contract;
    }
    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        contract.onViewMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        if (direction == ItemTouchHelper.LEFT) {
            contract.onViewSwipedLeft(viewHolder.getAdapterPosition());
        } else if (direction == ItemTouchHelper.RIGHT) {
            contract.onViewSwipedRight(viewHolder.getAdapterPosition());
        }
    }

    public interface ActionCompletionContract {
        void onViewMoved(int oldPosition, int newPosition);
        void onViewSwipedLeft(int position);
        void onViewSwipedRight(int position);
    }
}


我设法通过以下方式暂时将其提升:
enter image description here

导致的结果:(阴影被以某种方式剪掉了?)
enter image description here

但是,只要稍微移动视图,高程就会消失:
enter image description here

我的问题是:拖动卡片时如何获得高程(包括阴影)?

谢谢!

2 个答案:

答案 0 :(得分:1)

好,所以我解决了,有点。我认为由于其他原因其默认情况下不起作用,但是我设法编写了一种解决方法。

private boolean first = true; //first draw of cardView?
private boolean last = false; //last draw of cardView?

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    //add elevation on first draw
    if (first) {
        ViewPropertyAnimator animator = viewHolder.itemView.animate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //consider SDK version
            viewHolder.itemView.setTranslationZ(7);
            animator.start();
        }
        first = false;
    }
    //remove translationZ in last edit
    if (last) {
        ViewPropertyAnimator animator = viewHolder.itemView.animate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //consider SDK version
            viewHolder.itemView.setTranslationZ(0);
            animator.start();
        }
        //reset values
        last=false;
        first=true;
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    last = true; //only one more OnChildDrawWillBeCalled
}

以上代码已添加到您的ItemTouchHelper.Callback中,所有代码均应正常工作。 基本思想是手动控制平移图的绘制。 为此,我确定何时绘制了第一个画布,何时绘制了最后一个画布,而cancas将显示阴影。

再加上一点评论:将ViewPropertyAnimator与xml布局文件结合使用是非常直观和错误的,因此,如果可以避免的话,我建议这样做,而是尝试以编程方式对视图的更改和效果进行动画处理。

希望这可以帮助某人。

答案 1 :(得分:1)

@LivinTheNoobLife,在您的解决方案中,您使用的是ViewPropertyAnimator,但没有为其设置任何转换,因此将不应用动画。

这是我使用正确工作的浮动动画的解决方案:

class DragHelper extends ItemTouchHelper.Callback {    
private boolean cardPicked = true;
private boolean reset = false;

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

    // elevate only when picked for the first time
    if (cardPicked) { 
        ViewPropertyAnimator animator = viewHolder.itemView.animate();
        animator.translationZ(16);
        animator.setDuration(200);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.start();

        cardPicked = false;
    }

    // when your item is not floating anymore
    if (reset){
        ViewPropertyAnimator animator = viewHolder.itemView.animate();

        animator.translationZ(0);
        animator.setDuration(200);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();

        cardPicked = true;
        reset = false;
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

// As the doc says
// Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);

     // interaction is over, time to reset our elevation
     reset = true;
}
}