【问题标题】:RecyclerView ItemTouchHelper - Custom ViewRecyclerView ItemTouchHelper - 自定义视图
【发布时间】:2015-11-24 14:11:25
【问题描述】:

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

我即将让它工作,但是我在使用自定义图像时遇到了问题。

我要的效果是这样的:RecyclerView ItemTouchHelper swipe remove animation

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

下面是我的代码:

@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

【问题讨论】:

    标签: android swipe android-recyclerview


    【解决方案1】:

    您可以准备您的回收站视图项目布局以包含复选符号 ImageView 以及可能仅包含背景颜色的另一个空白视图。您还应该将所有可滑动的内容移动到嵌套布局中。在您的视图持有者中分配对可滑动内容的引用。然后只需在onChildDraw 中的可滑动内容上调用setTranslationX,例如:

    ((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 我已经用更多源代码更新了我的答案,希望对您有所帮助。
    • @Kustal - 在这个例子中,可滑动的内容区域在滑动之前如何隐藏?我也在使用卡片视图来进行操作项布局。谢谢!
    • 为什么要隐藏可滑动区域?正如您所解释的那样,该区域下方的区域(复选符号和紫色背景)将被隐藏,直到开始滑动并显示它。
    • 感谢您提供额外的代码,但我仍然没有任何运气。您能否创建一个简单的示例项目来展示您的示例?非常感谢您的帮助
    【解决方案2】:

    我不确定你是否有解决方案,也不确定你是否看过这个库。

    我已经为我的一个项目尝试了Android-SwipeListView 库,并为另一个项目尝试了SwipeMenuListView 以满足类似要求。

    两者都可以正常工作。您应该尝试一下,并检查它应该如何满足您的要求。

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

    享受编码... :)

    【讨论】:

    • 感谢我对这些的想法,但它们是自定义库。我正在寻找一个使用新的 recyclerview 的基本滑动功能的选项
    • @aherrick 很高兴听到您正在尝试让自己的代码像这样工作。您应该从该库中获得一些参考。我想这对你有帮助。
    【解决方案3】:

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

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多