Make a scrollView autoscroll with drag and drop in Android

前端 未结 4 2030
借酒劲吻你
借酒劲吻你 2020-12-17 03:05

I searched all over, but could not find a solution.

I have a view (lets call it myView) inside a scrollview. myView is bigger than the screen. Since I\'m able to get

4条回答
  •  -上瘾入骨i
    2020-12-17 03:54

    I have modified answer of Tiago A. I faced the same problem and the solution from Tiago A was small and easy but have some limitation so if others require this may help. Thanks to Tiago A.

    case DragEvent.ACTION_DRAG_LOCATION:
                        ScrollView myScrollView =findViewById(R.id.myScrollView);
                        int topOfDropZone = myScrollView.getChildAt(0).getTop();
                        int bottomOfDropZone = myScrollView.getChildAt(0).getBottom();
    
                        int scrollY = myScrollView.getScrollY();
                        int scrollViewHeight = myScrollView.getMeasuredHeight();
    
                        if (Math.round(event.getY()) > scrollViewHeight - (scrollViewHeight / 45))
                            if (bottomOfDropZone > (scrollY + scrollViewHeight - 100))
                                myScrollView.smoothScrollBy(0, 30);
    
                        if (Math.round(event.getY()) < (scrollViewHeight / 45))
                            if (topOfDropZone < (scrollY + 100))
                                myScrollView.smoothScrollBy(0, -30);
    
                        return true;
    

提交回复
热议问题