是否可以将项目拖到RecyclerView之外? [英] Is it possible to drag an item outside of RecyclerView?

查看:457
本文介绍了是否可以将项目拖到RecyclerView之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个项目从另一个 RecyclerView 拖放到它上面时,我需要以某种方式通知 RecyclerView




  • 有可能吗?

  • 或者我应该使用经典的



    包含蓝色商品的RecyclerView 位于一个片段中, RecyclerView 包含红色商品在另一个片段中。



    我也尝试过使用 ItemTouchHelper 但它是 onMove()方法从$ code> RecyclerView 移动
    以外的商品时,不调用rel =noreferrer> ItemTouchHelper.Callback 。

     私有类CustomItemTouchCallback扩展回调{

    @Override
    public int getMovementFlags(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder){
    return makeMovementFlags(UP | DOWN | START | END,0);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,RecyclerView.ViewHolder target){
    android.util.Log.d(TAG ,移动项目:+ viewHolder.getAdapterPosition()+to:+ target.getAdapterPosition());
    返回true;
    }

    @Override
    public void onMoved(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,int fromPos,RecyclerView.ViewHolder target,int toPos,int x,int y){
    android.util.Log.d(TAG,Moved item from:+ fromPos +to:+ toPos +x:+ x +y:+ y);
    super.onMoved(recyclerView,viewHolder,fromPos,target,toPos,x,y);
    }

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

    }

    @覆盖
    public boolean isLongPressDragEnabled(){
    return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled(){
    return false;
    }
    }

    我也发现这个问题,但它并没有解决我的问题。

    解决方案

    老问题,但还没有回答,所以我提供一个简短的...



    要将某个视图中的内容拖动到另一个视图,您可以使用 DragShadowBuilder ItemTouchHelper 仅用于在 RecyclerView 中移动项目,您需要根据需要自定义实现。



    查看此文档: https://developer.android.com/guide/topics/ui/drag-drop (是的,你想要的就是你应该使用的这个classid拖放框架)



    它向您展示了如何创建阴影,如何移动阴影(自动完成)以及如何在第二个视图中对拖动事件做出反应。



    工作流程如下:


    1. 创建拖动阴影时,例如长按 RecyclerView 1中的项目(它是项目的副本,在应用程序上绘制)

    2. 阴影可以移动,已经由框架处理;这将调用拖动进入/退出并在拖放的视图上拖动事件

    3. 在第二个上添加 View.OnDragListener code> RecyclerView 并处理事件(例如,如果用户将数据放在它上面,则将数据添加到第二个 RecyclerView 中并将其从第一个 RecyclerView

    的帮助下DragShadowBuilder 你可以找到这方面的教程,例如这里的一个: http://www.vogella.com/tutorials/AndroidDragAndDrop/article。 html


    I need to somehow notify RecyclerView when I drag and drop an item from another RecyclerView onto it.

    RecyclerView with blue items is in one fragment and RecyclerView with red items is in another fragment.

    I also tried using ItemTouchHelper but it's onMove() method from ItemTouchHelper.Callback is not called while moving with item outside from RecyclerView.

    private class CustomItemTouchCallback extends Callback {
    
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            return makeMovementFlags(UP|DOWN|START|END, 0);
        }
    
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            android.util.Log.d(TAG, "Move item  from:" + viewHolder.getAdapterPosition() + " to: " + target.getAdapterPosition());
            return true;
        }
    
        @Override
        public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
            android.util.Log.d(TAG, "Moved item  from:" + fromPos + " to: " + toPos + " x: " + x + " y: " + y);
            super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
        }
    
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    
        }
    
        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }
    
        @Override
        public boolean isItemViewSwipeEnabled() {
            return false;
        }
    }
    

    I also find this question, but it doesn't solve my problem.

    解决方案

    Old question, but no answer yet, so I provide a short one...

    For dragging something from one view to another you can use the DragShadowBuilder. The ItemTouchHelper is only meant for moving items within the RecyclerView, you need a custom implementation for what you want.

    Check out this documentation: https://developer.android.com/guide/topics/ui/drag-drop (so yes, for what you want you should is this classid drag&drop framework as you called it)

    It shows you how to create the shadow, how to move it (done automatically) and how to react to the drag event in the second view.

    The workflow is following:

    1. Create the drag shadow when e.g. long pressing an item in RecyclerView 1 (it's a copy of the item, drawn over the app)
    2. The shadow can be moved around, handled by the framework already; this will call drag enter/exits and drag events on the views it is draggede over
    3. add a View.OnDragListener on the second RecyclerView and handle the events (e.g. add the data in the second RecyclerView if the user drops it over it and remove it from the first RecyclerView)

    With the help of DragShadowBuilder you can find tutorials on this, like e.g. the one here: http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html

    这篇关于是否可以将项目拖到RecyclerView之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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