使用 Android 的 RecyclerView 时,在滑动行下方添加带有文本/图标的彩色背景 [英] Adding a colored background with text/icon under swiped row when using Android's RecyclerView

查看:40
本文介绍了使用 Android 的 RecyclerView 时,在滑动行下方添加带有文本/图标的彩色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真正的问题是我的 LinearLayout 被包裹在另一个布局中,这导致了不正确的行为.与我在问题中提供的代码片段相比,Sanvywell 接受的答案有一个更好、更完整的示例,说明如何在滑动视图下绘制颜色.

The real problem was that my LinearLayout was wrapped in another layout, which caused the incorrect behavior. The accepted answer by Sanvywell has a better, more complete example of how to draw a color under swiped view than the code snippet I provided in the question.

现在 RecyclerView 小部件具有原生在 ItemTouchHelper 的帮助下支持行滑动类,我试图在一个应用程序中使用它,其中行的行为类似于谷歌的收件箱应用程序.也就是说,向左滑动执行一个动作,向右滑动执行另一个动作.

Now that RecyclerView widget has native support for row swiping with the help of ItemTouchHelper class, I'm attempting to use it in an app where rows will behave similarly to Google's Inbox app. That is, swiping to the left side performs one action and swiping to the right does another.

使用 ItemTouchHelper 可以轻松实现操作本身.SimpleCallbackonSwiped 方法.但是,我无法找到一种简单的方法来设置应显示在当前正在滑动的视图下的颜色和图标(例如在 Google 的收件箱应用中).

Implementing the actions themselves was easy using ItemTouchHelper.SimpleCallback's onSwiped method. However, I was unable to find a simple way to set color and icon that should appear under the view that's currently being swiped (like in Google's Inbox app).

为此,我试图覆盖 ItemTouchHelper.SimpleCallbackonChildDraw 方法如下:

To do that, I'm trying to override ItemTouchHelper.SimpleCallback's onChildDraw method like this:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
                        RecyclerView.ViewHolder viewHolder, float dX, float dY,
                        int actionState, boolean isCurrentlyActive) {
    RecyclerViewAdapter.ViewHolder vh = (RecyclerViewAdapter.ViewHolder) viewHolder;
    LinearLayout ll = vh.linearLayout;

    Paint p = new Paint();
    if(dX > 0) {
        p.setARGB(255, 255, 0, 0);
    } else {
        p.setARGB(255, 0, 255, 0);
    }

    c.drawRect(ll.getLeft(), ll.getTop(), ll.getRight(), ll.getBottom(), p);

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

从 dX 确定滑动方向并设置适当的颜色按预期工作,但我从 ViewHolder 获得的坐标始终对应于第一个 LinearLayout 所在的位置膨胀了.

Determining the swipe direction from dX and setting the appropriate color works as intended, but the coordinates I get from the ViewHolder always correspond to the place where the first LinearLayout was inflated.

如何获取当前滑动行中 LinearLayout 的正确坐标?有没有更简单的方法(不需要覆盖 onChildDraw)来设置背景颜色和图标?

How do I get the correct coordinates for the LinearLayout that's in the currently swiped row? Is there an easier way (that doesn't require to override onChildDraw) to set the background color and icon?

推荐答案

我也在努力实现此功能,但您为我指明了正确的方向.

I was struggling to implement this feature as well, but you steered me in the right direction.

@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) {
        // Get RecyclerView item from the ViewHolder
        View itemView = viewHolder.itemView;

        Paint p = new Paint();
        if (dX > 0) {
            /* Set your color for positive displacement */

            // Draw Rect with varying right side, equal to displacement dX
            c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                    (float) itemView.getBottom(), p);
        } else {
            /* Set your color for negative displacement */

            // Draw Rect with varying left side, equal to the item's right side plus negative displacement dX
            c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
                    (float) itemView.getRight(), (float) itemView.getBottom(), p);
        }

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

这篇关于使用 Android 的 RecyclerView 时,在滑动行下方添加带有文本/图标的彩色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆