在 Recyclerview 中拖动项目时自动滚动滚动条

2024-03-03

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

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

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

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 有多个项目时,滚动视图会在 itemX 位于两个项目之间时上下滚动。

编辑的问题:现在,我将上面的代码放在第二个回收器视图 OnDragListener 上。现在是拖动侦听器的问题,所以我希望用户将第一个 Recyclerview 项目拖动到下方/上方第二个 Recyclerview 的拖动监听器需要做其他工作第二个Recyclerview项目拖动监听器需要工作。


我通过返回解决了这个问题false in 第二个 RecyclerView 项目拖动侦听器 ACTION_DRAG_LOCATION 事件。我禁用了ACTION_DRAG_LOCATION事件,以便该事件不被跟踪第二个 RecyclerView 项目拖动监听器。那个时候它父级(第二个 RecyclerView)的 Draglistener工作。下面的代码,我把第二个 RecyclerView 的 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事件使用下面的代码:

 @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。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Recyclerview 中拖动项目时自动滚动滚动条 的相关文章

随机推荐