首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓拖拽和DRop自动滚动

安卓拖拽和DRop自动滚动
EN

Stack Overflow用户
提问于 2020-06-01 19:02:07
回答 1查看 213关注 0票数 0

我正在尝试弄清楚当用户将项目拖到屏幕边缘时如何自动滚动滚动视图。我真的没有从这段代码中得到我所期望的行为。有没有人有一个有效的例子或技术?

我的电脑里有这个:

代码语言:javascript
复制
onDrag(View v, DragEvent e)
{
   case DragAction.Location:

    var currentPosition = (int)e.GetX();

    var point = GetTouchPositionFromDragEvent(v, e);

    System.Diagnostics.Debug.WriteLine($"DragAction.Location from {v.GetType()} => {currentPosition}");
    System.Diagnostics.Debug.WriteLine($"DragAction.GLOBAL   from {v.GetType()} => {point.X}");

    if (point.X > App.AppScreenWidth - 50)
    {
        _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX + 30, 0, true);
    }

    if (point.X < 50)
    {
        _hostScrollView.ScrollToAsync(_hostScrollView.ScrollX - 30, 0, true);
    }    
}

public static Point GetTouchPositionFromDragEvent(View item, DragEvent e) 
{
    Rect rItem = new Rect();
    item.GetGlobalVisibleRect(rItem);
    return new Point(rItem.Left + (int)Math.Round(e.GetX()), rItem.Top + (int)Math.Round(e.GetY()));
}

这也有奇怪的只在一个方向上滚动的连锁反应,而且还需要用户不断移动项目才能触发事件,这让我认为这是完全错误的位置,甚至试图进行滚动。

在正确的方向上的任何指针或轻推都将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-01 21:10:44

在你的活动中

代码语言:javascript
复制
public class MainActivity : AppCompatActivity, View.IOnDragListener,View.IOnScrollChangeListener
代码语言:javascript
复制
private int mScrollDistance;
代码语言:javascript
复制
 ScrollView _hostScrollView= FindViewById<ScrollView>(Resource.Id.xxx);
_hostScrollView.SetOnScrollChangeListener(this);
代码语言:javascript
复制
public bool OnDrag(View v, DragEvent e)
        {
            var action = e.Action;

            var scrollView = v as ScrollView;

            switch (action)
            {
                case DragAction.Started:

                    break;
                case DragAction.Location:
                    
                        int y = Java.Lang.Math.Round(e.GetY());
                        int translatedY = y - mScrollDistance;
                        int threshold = 50;
                        // make a scrolling up due the y has passed the threshold
                        if (translatedY < threshold)
                        {
                            // make a scroll up by 30 px
                            scrollView.ScrollBy(0, -30);
                        }
                        // make a autoscrolling down due y has passed the 500 px border
                        if (translatedY + threshold > 500)
                        {
                            // make a scroll down by 30 px
                            scrollView.ScrollBy(0, 30);
                        }
                   
                    break;
                    
            }
            return true;
        }

        public void OnScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
        {
            var scrollView = v as ScrollView;
            mScrollDistance = scrollView.ScrollY;
        }

如果我误解了你的需求,你可以随意修改逻辑。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62130708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档