在不使用库的情况下滑动(滑动以关闭)时在 recyclerview 项下创建按钮和按钮单击 [英] Creating buttons and button click under recyclerview item when it swiped (swip to dismiss) without using library

查看:17
本文介绍了在不使用库的情况下滑动(滑动以关闭)时在 recyclerview 项下创建按钮和按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RecyclerView 来显示我的列表.我使用 ItemTouchHelper 实现 Swipe 以关闭 RecyclerView.底层布局是通过使用画布在 OnchildDraw 方法中实现的.现在我有一个问题:我想在我的图标上设置 onclick.通过点击图标,我想做一些功能.这是我的课:

I am using RecyclerView for showing my list. I implement Swipe to dismiss on the RecyclerView with ItemTouchHelper. The underlying layout is implemented in OnchildDraw method by using canvas. Now I have a problem: I want to set onclick on my icon. By clicking on the icon, I want to do some functions. Here is My class:

public class ItemTouchHelperCallback : ItemTouchHelper.SimpleCallback
{
    private ContactSearchedResultAdapter _adapter;
    private RecyclerView _mRecyclerView;
    private int _swipeCount;
    private Android.Content.Res.Resources _resources;
    public ItemTouchHelperCallback(ContactSearchedResultAdapter adapter, RecyclerView mRecyclerView, Android.Content.Res.Resources resources)
        : base(0, ItemTouchHelper.Left | ItemTouchHelper.Right)
    {
        this._adapter = adapter;
        this._mRecyclerView = mRecyclerView;
        this._resources = resources;
    }
    public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
    {
        return false;
    }

    public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
    {
        if (direction == ItemTouchHelper.Left)
        {
            _adapter.RemoveViewWithDialog(viewHolder.AdapterPosition, _mRecyclerView, _swipeCount);
            if (_swipeCount == 0)
                _swipeCount++;
        }
        else
        {
            _adapter.SaveContactToDataBase(viewHolder.AdapterPosition, _mRecyclerView);
        }
    }
    public override void OnChildDraw(Canvas cValue, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive)
    {
        Paint paint = new Paint();
        View itemView = viewHolder.ItemView;
        float height = (float)itemView.Bottom - (float)itemView.Top;
        float width = height / 3;
        Bitmap icon;

        if (dX > 0)
        {
            paint.Color = Color.ParseColor("#388E3C");
            RectF background = new RectF((float)itemView.Left, (float)itemView.Top, dX, (float)itemView.Bottom);
            cValue.DrawRect(background, paint);
            icon = BitmapFactory.DecodeResource(_resources, Resource.Drawable.addoption);
            RectF icon_dest = new RectF((float)itemView.Left + width, (float)itemView.Top + width, (float)itemView.Left + 2 * width, (float)itemView.Bottom - width);
            cValue.DrawBitmap(icon, null, icon_dest, paint);
        }
        else
        {
            paint.Color = Color.ParseColor("#D32F2F");
            RectF background = new RectF((float)itemView.Right + dX, (float)itemView.Top, (float)itemView.Right, (float)itemView.Bottom);
            cValue.DrawRect(background, paint);
            icon = BitmapFactory.DecodeResource(_resources, Resource.Drawable.removeoption);
            RectF icon_dest = new RectF((float)itemView.Right - 2 * width, (float)itemView.Top + width, (float)itemView.Right - width, (float)itemView.Bottom - width);
            cValue.DrawBitmap(icon, null, icon_dest, paint);
        }

        float alpha = (float)1.0- Math.Abs(dX)/(float) itemView.Width;
        itemView.Alpha = alpha;
        itemView.TranslationX = dX;

        base.OnChildDraw(cValue, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

}

如您所见,我正在 OnSwiped 方法中调用 ItemRemoving 或 ItemSaving.我现在想做的是在点击图标时调用这些方法(在 OnchildDraw 中由画布绘制的图标)我搜索了很多有关此主题的内容,但找不到任何不使用任何库即可实现此功能的解决方案.我不想使用图书馆.

As you can see I am calling ItemRemoving or ItemSaving in OnSwiped method. What I want to do now is calling these methods on icons' click (icons that are drawn by canvas in OnchildDraw) I searched a lot about this topic and couldn't find any solution that implemented this feature without using any library. I don't want to use library.

推荐答案

我遇到了和你一样的问题.码了半天,终于找到方法了.

I had the same problem as yours. I coded half a day and finally find one way.

首先,我认为这是android ItemTouchHelper 的问题,没有方法可以解决.

First of all, I think this is android ItemTouchHelper problem that there are no methods to solve that.

所以不能使用ItemTouchHelper等系统方法.

So it can't use ItemTouchHelper and other system methods.

我要做的是:1. 设置 recylerView.setOnTouchListener 以在屏幕上获取 (x,y).

What I do is this: 1. Set recylerView.setOnTouchListener to get (x,y) on screen.

  1. 当 ItemTouchHelper onSwiped() 时,设置状态并找到可点击区域.

  1. When ItemTouchHelper onSwiped(), set it a state and find the clickable region.

检查您的触摸点是否触及图标区域.

Check your touch point if it hits the icon region.

在 onTouch() 方法中被点击时做一些事情.

Do something when hitted in your onTouch() method.

您可以在我的 Github 存储库中找到我的解决方案.https://github.com/TindleWei/WindGod

You can find my solution on my Github repo. https://github.com/TindleWei/WindGod

这里有 3 种使用 ItemTouchHelper 的方法:

Here 3 ways to use ItemTouchHelper:

  1. 像你这样的安卓正常方式

  1. android normal way like yours

前景和背景区域方式

viewpager 方式

viewpager way

在我的 repo 中,您可以查看第二种方式.

On my repo, you can look at 2nd way.

这篇关于在不使用库的情况下滑动(滑动以关闭)时在 recyclerview 项下创建按钮和按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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