简体   繁体   English

Android CardView拖放ItemTouchHelper

[英]Android CardView Drag and Drop ItemTouchHelper

I tried to look all over google and SO but couldn't find a solution to my problem. 我试图遍及Google和SO,但找不到解决我问题的方法。

Basically, I've got a RecyclerView utilizing CardViews, and I want to allow Drag&Drop on those cardviews. 基本上,我有一个使用CardViews的RecyclerView,我想在这些Cardview上允许拖放。 Swiping works fine, but in the ItemTouchHelper, Drag and drop doesn't work. 滑动工作正常,但在ItemTouchHelper中,拖放无效。 I'm not sure why, I've specified the correct movement directions. 我不确定为什么,我指定了正确的运动方向。

I can swipe left and right for the swipeDirs, but Moving up and down doesn't work for the dragDirs. 对于swipeDirs,我可以左右滑动,但是对于dragDirs,不能上下移动。 I'm not sure if its an issue with the emulator itself not recognizing "drags" because if I attach UP and DOWN to the swipeDirs, I can swipe in every direction as a test. 我不确定模拟器本身是否无法识别“拖动”问题,因为如果在swipeDirs上附加了UP和DOWN,则可以在各个方向上滑动以进行测试。

I'm not getting any visuals of Drag and Drop working 我没有任何拖放的视觉效果

ItemTouchHelper.Callback scb = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT){
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target){
        //call back to adapter to swap positions, the Error is not with this line of code (at least not yet)
        return true;
    }

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

    }
};

ItemTouchHelper ith = new ItemTouchHelper(scb);
ith.attachToRecyclerView(recyclerView);

Any help is appreciated, Thanks 任何帮助表示赞赏,谢谢

Figured it out, you need to Override onBindViewHolder in your Adapter 弄清楚了,您需要在适配器中覆盖onBindViewHolder

@Override
public void onBindViewHolder(Adapter.ViewHolder  holder, int position){
    final Adapter.ViewHolder xholder = holder;
    holder.card.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View view, MotionEvent event){
            if(MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN)
                listener.onStartDrag(xholder);
            return false;
        }
    });
}

And then in your RecyclerList containing the ItemTouchHelper 然后在您的RecyclerList中包含ItemTouchHelper

@Override
public void onStartDrag(ViewHolder holder){
    itemTouchHelper.startDrag(holder);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Recyclerview 中的 itemTouchHelper 在 cardview 上拖动高程平移z - Android drag elevation translationz on cardview with itemTouchHelper in Recyclerview Android Cardview通过拖放更改位置 - Android cardview change position with drag and drop 如何使用ItemTouchHelper和游标实现拖放 - How to implement drag and drop with ItemTouchHelper and cursors 将ListView拖放到CardView列表中 - Drag and Drop ListView into CardView list Android - 使用 ItemTouchHelper.Callback 拖动 RecyclerView - Android - RecyclerView drag using ItemTouchHelper.Callback 使用 itemtouchhelper、recyclerview 和 Firestore 拖放项目,例如 Google Tasks - Drag & drop items with itemtouchhelper, recyclerview and Firestore like Google Tasks 类似于 ItemTouchHelper 使用 jetpack 组合拖放移动视图 - Using jetpack compose drag and drop to move views similar to ItemTouchHelper RecyclerView通过itemTouchHelper拖放,拖动时快速拖动 - RecyclerView drag & drop via itemTouchHelper bahaving strange when dragging fast android - 如何捕捉与 RecyclerView 一起使用的 ItemTouchHelper 的 Drop 动作 - android - how to catch Drop action of ItemTouchHelper which is used with RecyclerView Android在拖放中,Drop无法正常工作 - Android in drag and drop , Drop is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM