【问题标题】:Drag and drop scrolling in a linear layout在线性布局中拖放滚动
【发布时间】:2017-08-22 14:56:33
【问题描述】:

我目前正在开发一个应用程序,该应用程序允许用户在线性布局(滚动视图的子项)中添加相对布局、线性布局或嵌套线性布局。仅添加相对布局或线性布局时,滚动似乎可以正常工作,但是,一旦添加了嵌套线性布局,滚动就会成为问题。基本上,一旦我的拖动视图进入屏幕顶部/底部的 x 量内,我希望滚动视图开始滚动。有什么建议吗?以下是我的一些帮助代码

    protected class myDragEventListener implements View.OnDragListener {
    // This is the method that the system calls when it dispatches a drag event to the listener.
    public boolean onDrag(View v, DragEvent event) {
        // Defines a variable to store the action type for the incoming event
        final int action = event.getAction();
        Point touchPosition = getTouchPositionFromDragEvent(v, event);
        Log.d("y", String.format("%s", String.format("%s", eventTouchYCoord)));
        //View dragView = (View) event.getLocalState();
        // Handles each of the expected events
        switch(action) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DRAG_ENTERED:
                return true;
            case DragEvent.ACTION_DRAG_LOCATION:
                Log.d("point", touchPosition.toString());
                if (touchPosition.y > (absoluteBottom + mScrollDistance - 350)){
                    Log.d("bottom", "greater");
                    Log.d("actionbar", String.format("%s", actionBarHeight()));
                    homeScroll.scrollBy(0, 30);
                }
                if (touchPosition.y < (actionBarHeight + 350)){
                    Log.d("top", "greater");
                    homeScroll.scrollBy(0, -30);
                }
                break;

getTouchPositionFromDragEvent 方法

public static Point getTouchPositionFromDragEvent(View item, DragEvent event) {
    Rect rItem = new Rect();
    item.getGlobalVisibleRect(rItem);
    return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY()));
}

onScrollChangedListener 方法

    homeScroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            mScrollDistance = homeScroll.getScrollY();
            int[] coords = new int[2];
            homeScroll.getLocationOnScreen(coords);
            absoluteTop = coords[1];
            absoluteBottom = coords[1] + homeScroll.getHeight();
        }
    });

【问题讨论】:

  • 我想一个更好的问题可能是,我如何获得拖动视图的绝对位置,这意味着无论我滚动多少,y 位置都将介于 absoluteTop 和 absoluteBottom 之间。截至目前,getTouchPositionFromDragEvent 返回整个布局的 x、y 坐标,考虑到滚动。

标签: java android drag-and-drop android-scrollview


【解决方案1】:

在 xml 文件中实现滚动属性始终是最佳实践。这是您想要在其中滚动的内容。或在调色板中,您会找到垂直和水平滚动的滚动视图选项。只需拖放到您想要放置的位置。

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    </ScrollView>

【讨论】:

  • 我已经在我的 xml 中实现了滚动视图。我可以很好地滚动我的容器。我的问题是当我拖动视图并尝试滚动时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多