采用Android的RecyclerView时添加彩色背景,文字/图标刷下排 [英] Adding a colored background with text/icon under swiped row when using Android's RecyclerView

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

问题描述

编辑:真正的问题是,我的的LinearLayout 被包裹在另一个布局,从而导致不​​正确的行为。通过Sanvywell接受的答案有怎样画下刷卡观点比我在问题中所提供的code段色更好,更完整的示例。

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.

现在的<一个href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html">RecyclerView部件具有行的帮助下<一刷原生支持href="https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.html">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.

落实措施本身很容易使用<一个href="https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.SimpleCallback.html">ItemTouchHelper.SimpleCallback's onSwiped 方法。但是,我无法找到一个简单的方法来设置颜色和图标应显示多数民众赞成目前正在刷卡(就像谷歌的收件箱中的应用程序)视野下的。

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).

要做到这一点,我想重写<一个href="https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.SimpleCallback.html">ItemTouchHelper.SimpleCallback's onChildDraw 这样的方法:

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

确定从仪表上的滑动方向,并设置适当的颜色作品如预期,但坐标我从 ViewHolder 总是对应于地方,第一个<$ C得到$ C>的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天全站免登陆