San*_*ati 5 android scrollview android-recyclerview
我在滚动视图中遇到自动滚动问题。
就我而言,有两个 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;
Run Code Online (Sandbox Code Playgroud)
但是,如果 recyclerview 只有一项,则上述代码无法正常工作于多个项目,而不是正常工作。但是当 recyclerview 有多个项目时,滚动视图会在 itemX 位于两个项目之间时上下滚动。
编辑的问题: 现在,我将上面的代码放在第二个回收器视图的 OnDragListener 上。现在是拖动监听器的问题,所以我希望如果用户将第一个 Recyclerview 的项目拖动到第二个 Recyclerview的拖动监听器的下方/上方,则第二个 Recyclerview 的拖动监听器需要工作,否则第二个 Recyclerview 的项目的拖动监听器需要工作。
我通过在Second RecyclerView 项目的拖动侦听器的 ACTION_DRAG_LOCATION 事件中返回false解决了此问题。我禁用了ACTION_DRAG_LOCATION事件,因此第二个 RecyclerView 的 Item 的 DragListener不会跟踪该事件。那时它的Parent(Second 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;
Run Code Online (Sandbox Code Playgroud)
要禁用第二个 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;
}
Run Code Online (Sandbox Code Playgroud)
因此,无论您需要处理来自 Draglistener 的任何事件,您只需要返回 true,否则返回 false。
| 归档时间: |
|
| 查看次数: |
5105 次 |
| 最近记录: |