RecyclerView ItemTouchHelper - 自定义视图

14

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

我接近实现它,但是使用自定义图像时遇到了问题。

我想要的效果类似于这个链接中的效果:RecyclerView ItemTouchHelper swipe remove animation

请看下面发生的情况。随着滑动的发生,它会扭曲图像:

enter image description here

下面是我的代码:

@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);
    }
}

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

3个回答

5

您可以准备您的Recycler View项布局,包括勾选符号ImageView和另一个仅具有背景颜色的空白视图。您还应将所有可滑动内容移动到嵌套布局中。在视图持有者中分配对可滑动内容的引用。然后只需在onChildDraw中调用setTranslationX来设置可滑动内容的平移X值,例如:

((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);

源代码示例更新:

回收站视图项布局:

<?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>

视图持有者类:

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
        */
    }
}

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);
        }
    }

}

嗯,我应该默认隐藏可滑动内容然后再显示吗?否则它会显示在卡片上。有没有显示XML视图的示例? - aherrick
@aherrick,我已经更新了我的答案,并添加了更多的源代码,希望能对你有所帮助。 - kukabi
@Kustal - 在这个例子中,可滑动的内容区域如何在滑动之前隐藏?另外,我正在使用一个cardview来布局操作项。谢谢! - aherrick
1
为什么要隐藏可滑动区域?在滑动开始并显示出来之前,下面的区域(勾号和紫色背景)将被隐藏,正如您所解释的那样。 - kukabi
谢谢您提供的额外代码,但我仍然无法成功。您能否创建一个简单的示例项目来演示您的示例?非常感谢您的帮助。 - aherrick

0

只需将 d.setBounds() 中的 (int)dX 更改为您想要的任何 整数

例如:

d.setBounds(itemView.getLeft(), itemView.getTop(), 50, itemView.getBottom());

0

我不确定你是否已经得到解决方案,也不确定你是否看过这个库。

我曾经在一个项目中尝试使用Android-SwipeListView库,在另一个类似需求的项目中使用了SwipeMenuListView

两者在各自的场合都能正常工作。你应该尝试一下,并检查它们如何适用于你的需求。

如果需要更多帮助,请告诉我。

享受编码... :)


谢谢您的建议。我已经查看了这些内容,但它们都是自定义库。我正在寻找一种使用基本滑动功能和新的RecyclerView选项。 - aherrick
@aherrick 很高兴听到你正在尝试让自己的代码像那样工作。你应该从那个库中获取一些参考。我猜那会对你有所帮助。 - Shreyash Mahajan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接