在Recyclerview中拖动项目时自动滚动滚动条 [英] Auto-scroll Scrollbar While Drag item In Recyclerview

查看:646
本文介绍了在Recyclerview中拖动项目时自动滚动滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动视图中遇到自动滚动问题.

I have an issue with Auto-scroll in scroll view.

就我而言,有两个Recyclerview.第一个Recyclerview,可水平滚动,第二个可垂直滚动.第一RecyclerView仅用于拖动,第二RecyclerView仅用于拖放.两个recyclerviews都在ScrollView内部,因此,我在第二个Recyclerview中禁用了垂直滚动. 我在Second Recyclerview的项目上添加了DragListener.每个项目都有拖动侦听器,因此基于我在放置项目时添加/替换项目的情况.

In my case, there are two Recyclerview. First Recyclerview, horizontally scrollable and second vertically. First RecyclerView only for drag and Second RecyclerView only for a drop. Both recyclerviews are inside ScrollView so, I disabled vertical scroll in second Recyclerview. I added DragListener on Second Recyclerview's item. Every item have drag listener so based on that I add/replace item while drop item.

所以我的主要问题是滚动,因此在拖动项目时滚动无法正常工作. 目前,我在拖动时使用下面的代码进行滚动.

So My main issue with scroll, so scroll not working properly while i drag item. currently i used below code for scroll while drag.

case DragEvent.ACTION_DRAG_LOCATION:
RecyclerView recyclerView = (RecyclerView) viewSource.getParent();
MyAdapter adapter = (MyAdapter) recyclerView.getAdapter();
int y = Math.round(dragEvent.getY());
Timber.d("onDrag(): " + y);
int translatedY = y - adapter.getScrollDistance();
Timber.d("onDrag(): translated : " + translatedY + "   getScrollDistance : " + adapter.getScrollDistance());
int threshold = 50;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold) {
    // make a scroll up by 30 px
     mScrollView.smoothScrollBy(0, -30);

} else
    // make a autoscrolling down due y has passed the 500 px border
    if (translatedY + threshold > 500) {
        // make a scroll down by 30 px
     mScrollView.smoothScrollBy(0, 30);
    }
break;

但是,如果recyclerview仅具有一项功能正常工作,则上述代码无法对多个项目正常工作.但是当recyclerview有多个项目时,时间scrollview会在两个项目之间的itemX上下滚动.

But above code not working properly for multiple items if recyclerview have only one item than its working fine. but when recyclerview having multiple items that time scrollview scroll above and below slitly while itemX in between two items.

已编辑的问题: 现在,我将上面的代码放在Second Recycler视图的OnDragListener上.现在是拖拽监听器的问题,所以我想如果用户将First Recyclerview的物品拖拽到第二Recyclerview的拖拽监听器以下/上方,则需要工作,否则第二次Recyclerview的拖拽监听器

Edited Question: Now, I put above code on Second Recycler view's OnDragListener. Now the problem with drag listener, so I want if user drag First Recyclerview's item below/above than second Recyclerview's drag listener needs to work else Second Recyclerview's item's drag listener needs to work.

推荐答案

我通过在第二个RecyclerView项目的拖动侦听器的ACTION_DRAG_LOCATION事件中返回 false 解决了此问题.我禁用了 ACTION_DRAG_LOCATION 事件,因此第二个RecyclerView的项目的DragListener 无法跟踪该事件.当时,其父级(第二个RecyclerView)的Draglistener 工作.在下面的代码中,我戴上第二个RecyclerView的DragListener

I resolved this issue by returning false in Second RecyclerView's item's drag listener's ACTION_DRAG_LOCATION event. I disabled ACTION_DRAG_LOCATION event, so that event not tracked by Second RecyclerView's Item's DragListener. That time Its Parent(Second RecyclerView)'s Draglistener work. Below code, I put on Second RecyclerView's DragListener

case DragEvent.ACTION_DRAG_LOCATION:
RecyclerView recyclerView = (RecyclerView) viewSource.getParent();
MyAdapter adapter = (MyAdapter) recyclerView.getAdapter();
int y = Math.round(dragEvent.getY());

int translatedY = y - adapter.getScrollDistance();

int threshold = 50;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold) {
    // make a scroll up by 30 px
     mScrollView.smoothScrollBy(0, -30);

} else
    // make a autoscrolling down due y has passed the 500 px border
    if (translatedY + threshold > 500) {
        // make a scroll down by 30 px
     mScrollView.smoothScrollBy(0, 30);
    }
break;

要禁用第二个RecyclerView项目的DragListener的ACTION_DRAG_LOCATION 事件,请使用以下代码:

To disable Second RecyclerView's item's DragListener's ACTION_DRAG_LOCATION event use below code:

 @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        int action = dragEvent.getAction();
        View viewSource = (View) dragEvent.getLocalState();
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                break;
            case DragEvent.ACTION_DRAG_ENTERED:

                break;
            case DragEvent.ACTION_DRAG_LOCATION:

                return false;
            case DragEvent.ACTION_DRAG_EXITED:

                break;
            case DragEvent.ACTION_DROP:

                break;
            default:
                break;
        }

        return true;
    }

因此,无论您需要处理Draglistener中的任何事件,您都只需返回true,否则返回false.

So whatever Event from Draglistener you need to handle you just need to return true otherwise return false.

这篇关于在Recyclerview中拖动项目时自动滚动滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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