在哪里为与 RecyclerView 一起使用的 ItemTouchHelper 编写回调函数

Posted

技术标签:

【中文标题】在哪里为与 RecyclerView 一起使用的 ItemTouchHelper 编写回调函数【英文标题】:where to write callback function for ItemTouchHelper which is used with RecyclerView 【发布时间】:2020-10-20 12:15:46 【问题描述】:

我想在项目拖放结束的地方写一段代码。我得到了这个答案https://***.com/a/36275415/1684778,但我对如何实现这一点感到困惑(我必须在哪里实现这个答案)

我想写onDrop而不是onItemMove,如何实现。

DragItemTouchHelper

   public class DragItemTouchHelper extends ItemTouchHelper.Callback 
    public static final float ALPHA_FULL = 1.0f;

    private final MoveHelperAdapter mAdapter;

    public DragItemTouchHelper(MoveHelperAdapter adapter) 
        mAdapter = adapter;
    

    @Override
    public boolean isLongPressDragEnabled() 
        return true;
    

    @Override
    public boolean isItemViewSwipeEnabled() 
        return false;
    

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) 
        // Set movement flags based on the layout manager
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) 
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            final int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
         else 
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        
    

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) 
        if (source.getItemViewType() != target.getItemViewType()) 
            return false;
        

        // Notify the adapter of the move
        mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
        return true;
    

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) 
    

    @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) 
            // Fade out the view as it is swiped out of the parent's bounds
            final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
            viewHolder.itemView.setAlpha(alpha);
            viewHolder.itemView.setTranslationX(dX);
         else 
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        
    

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) 
        // We only want the active item to change
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) 
            if (viewHolder instanceof TouchViewHolder) 
                // Let the view holder know that this item is being moved or dragged
                TouchViewHolder itemViewHolder = (TouchViewHolder) viewHolder;
                itemViewHolder.onItemSelected();
            
        

        super.onSelectedChanged(viewHolder, actionState);
    

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) 
        super.clearView(recyclerView, viewHolder);

        viewHolder.itemView.setAlpha(ALPHA_FULL);

        if (viewHolder instanceof TouchViewHolder) 
            // Tell the view holder it's time to restore the idle state
            TouchViewHolder itemViewHolder = (TouchViewHolder) viewHolder;
            itemViewHolder.onItemClear();
        
    

    public interface MoveHelperAdapter 

        boolean onItemMove(int fromPosition, int toPosition);
    

    public interface TouchViewHolder 
        void onItemSelected();

        void onItemClear();
    

【问题讨论】:

【参考方案1】:

您可以将扩展 ItemTouchHelper 回调的类放在 RecyclerView 所在的活动类的末尾,并将其附加到活动或片段中定义的 RecyclerView,如下所示

ItemTouchHelper touchHelper = new ItemTouchHelper(your_custom_callback);
touchHelper.attachToRecyclerView(recyclerView);

【讨论】:

在你的recyclerview所在的activity中复制你的整个班级并添加我添加的两行。

以上是关于在哪里为与 RecyclerView 一起使用的 ItemTouchHelper 编写回调函数的主要内容,如果未能解决你的问题,请参考以下文章

将reduce函数转换为与IE一起使用

如何将日文 OTF 字体转换为与 FOP 一起使用

将现有共享点安装配置为与 tfs 2013:TF25043 一起使用时出错

将 npgsql 配置为与 .NET 3.5 和 EF 一起使用

无法将 Django 设置为与 smtp.gmail.com 一起使用

尝试将 Node.js (Express) 设置为与虚拟主机一起使用,并遇到意外错误