RecyclerView ItemTouchHelper - 自定义视图

ahe*_*ick 14 android swipe android-recyclerview

我试图在向左/向右滑动时显示自定义视图(或图像).

我接近它的工作,但是我在使用自定义图像方面遇到了问题.

我想要的效果类似于:RecyclerView ItemTouchHelper滑动删除动画

看看下面发生了什么.当滑动发生时,它会扭曲图像:

在此输入图像描述

以下是我的代码:

@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) {
        View itemView = viewHolder.itemView;

        Paint p = new Paint();
        if (dX > 0) {
            Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.test_check);
            d.setBounds(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
            d.draw(c);

            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        } 

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在继续绘制背景颜色的同时保持图像静止?甚至可以使用Android视图而不是图像?与此类似:https://github.com/wdullaer/SwipeActionAdapter

K K*_*K B 5

您可以准备回收站视图项目布局以包括检查符号ImageView,也可以使用另一个仅包含背景颜色的空白视图.您还应将所有可滑动内容移动到嵌套布局中.为视图持有者中的可滑动内容指定引用.然后只需调用setTranslationX您的可刷卡内容onChildDraw,例如:

((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);
Run Code Online (Sandbox Code Playgroud)

使用源代码更新示例:

Recycler视图项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/recycler_view_item_height"
    android:background="@color/transparent">

    <!-- background color view -->
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/purple" />

    <!-- check symbol image view -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="@dimen/check_symbol_width"
        android:contentDescription="@null"
        android:src="@drawable/check_symbol" />

    <!-- swipeable content container -->
    <LinearLayout
        android:id="@+id/swipeable_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <!-- here goes your swipeable content -->
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

查看持有人类:

class MyViewHolder extends RecyclerView.ViewHolder {

    // Swipeable content container.
    LinearLayout swipeableContent;

    /*
     ... here goes other sub-views
    */

    public ExpenseViewHolder(final View itemView) {
        super(itemView);
        // bind swipeable content container view
        swipeableContent = (LinearLayout) itemView.findViewById(R.id.swipeable_content);

        /*
         ... bind other sub-views
        */
    }
}
Run Code Online (Sandbox Code Playgroud)

ItemTouchHelper.Callback子类:

class ItemTouchHelperCallback extends ItemTouchHelper.Callback {

    @Override
    public void onChildDraw(final Canvas c,
                            final RecyclerView recyclerView,
                            final RecyclerView.ViewHolder viewHolder,
                            final float dX,
                            final float dY,
                            final int actionState,
                            final boolean isCurrentlyActive) {
        if (viewHolder instanceof MyViewHolder) {
            // translate by swipe amount, 
            // the check symbol and the background will stay in place
            ((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)