使用Android中的拖放进行scrollView自动滚动

时间:2011-09-08 17:37:42

标签: android android-scrollview

我搜遍了所有地方,但找不到解决方案。

我在scrollview中有一个视图(让我们称之为myView)。 myView比屏幕大。由于我能够在myView中获得相对 x,y手指的位置,因此当我的手指进入某个上/下阈值时,我想让scrollView自动滚动到顶部/底部。 我有一些想法,即将拖动位置转换为屏幕位置,但这并没有解决这个问题。

提前致谢

欢呼声

4 个答案:

答案 0 :(得分:9)

好吧,我自己想出来了。

首先,我必须扩展ScrollView类并添加一个OnScrollViewListener接口。

public class MyScrollView extends ScrollView {
    private OnScrollViewListener mListener;

    public MyScrollView(Context c, AttributeSet attrs) {
       super(c, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
       super.onScrollChanged(l, t, oldl, oldt);
       if (mListener != null) {
           mListener.onScrollChanged((OnScrollViewListener) this);
       }
    }


    public void setOnScrollViewListener(OnScrollViewListener listener) {
       mListener = listener;
    }


    public static interface OnScrollViewListener {
       public void onScrollChanged(OnScrollViewListener listener);
    }
}

接下来在我的Activity中插入了一个成员mScrollDistance,表示其数量 用户滚动的像素。

public class ScrollActivity extends Activity {
   private int mScrollDistance;

   @Override
   protected void OnCreate(...) {
     ...

     final MyScrollView myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
     myScrollView.setOnScrollViewListener(new MyScrollView.OnScrollViewListener() {

          public void onScrollChanged(OnScrollViewListener listener) {
             mScrollDistance = listener.getScrollY();
          }
     }

     // making an drag and drop in an view that is inside the MyScrollView
     final LinearLayout myLayout = (LinearLayout)findViewById(R.id.linear_layout);
     myLayout.setOnDragListener(new View.OnDragListener() {
       public boolean onDrag (View v, DragEvent event) {
         int action = event.getAction();
         switch(action) {
            case DragEvent.ACTION_DRAG_STARTED: {
            }
            case DragEvent.ACTION_DRAG_LOCATION: {

              int y = Math.round(event.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
                 myScrollView.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
                 myScrollView.scrollBy(0, 30);
              }
              // listen for more actions here
              // ...
            }
         }
       }
     }

现在,mScrollDistance始终为新值,拖动位置将转换为视图位置。 我对此进行了测试,它适用于大于屏幕尺寸的布局/视图。

希望有所帮助。

答案 1 :(得分:4)

我想出了一个不同的解决方案,我很高兴。

我希望能够在ScrollView中拖放视图。然后,当阴影到达滚动视图的边缘时,ScrollView需要自动向上和向下滚动。

我最终得到了一个解决方案,可以检测到滚动视图内部的拖放区域是否完全可见(具有100px边距),否则调整滚动视图。

@Override
public boolean onDrag(View view, DragEvent event) {

    MainWidget dropZoneView = (MainWidget) view;

    int action = event.getAction();
    switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
        //(... other stuff happens here)
        case DragEvent.ACTION_DRAG_LOCATION:

            ScrollView mainScrollView = (ScrollView) findViewById(R.id.main_scroll);

            int topOfDropZone = dropZoneView.getTop();
            int bottomOfDropZone = dropZoneView.getBottom();

            int scrollY = mainScrollView.getScrollY();
            int scrollViewHeight = mainScrollView.getMeasuredHeight();

            Log.d(LOG_TAG,"location: Scroll Y: "+ scrollY + " Scroll Y+Height: "+(scrollY + scrollViewHeight));
            Log.d(LOG_TAG," top: "+ topOfDropZone +" bottom: "+bottomOfDropZone);

            if (bottomOfDropZone > (scrollY + scrollViewHeight - 100))
                mainScrollView.smoothScrollBy(0, 30);

            if (topOfDropZone < (scrollY + 100))
                mainScrollView.smoothScrollBy(0, -30);

            break;
        default:
            break;
    }
    return true;
}

希望这有帮助!

答案 2 :(得分:1)

我在In C#中使用了计时器

ScrollCalendar ScrollCalendar = new ScrollCalendar (yourScrollView);

在拖动事件中

    public bool OnDrag (View v, DragEvent e)
    {
        var dragshadow = new EventDateDragShadow (v);
        switch (e.Action) {
        case DragAction.Started:
            return true;
        case DragAction.Entered:
            break;
        case Android.Views.DragAction.Location:

            if (e.GetY () < 90) {
                ScrollCalendar.StartScroll (-15);
            } else if (e.GetY () > yourScrollView.Height - 90) {
                ScrollCalendar.StartScroll (15);
            } else
                ScrollCalendar.StopScroll ();

            return (true);
        case DragAction.Exited:
            return true;
        case DragAction.Drop:
            return true;
        case DragAction.Ended:
            ScrollCalendar.StopScroll ();
            v.SetOnDragListener (null);
            return true;
        }

        return true;
    }

ScrollCalendar类

public class ScrollCalendar
{
    private ScrollView Calendar;
    private System.Timers.Timer Timer;
    private int ScrollDistance;

    public ScrollCalendar(ScrollView calendar)
    {
        Calendar = calendar;
        Timer = new System.Timers.Timer();
        Timer.Elapsed+=new ElapsedEventHandler(Scroll);
        Timer.Interval = 50;
    }

    public void StartScroll(int scrollDistance)
    {
        if (Timer.Enabled) {
            return;
        }
        ScrollDistance = scrollDistance;
        Timer.Enabled = true;
    }

    public void StopScroll()
    {
        Timer.Enabled = false;
    }

    private void Scroll(object source, ElapsedEventArgs e)
    {
        Calendar.SmoothScrollBy (0, ScrollDistance);
    }

}

更改StartScroll值和Timer.Interval以调整滚动的速度。

答案 3 :(得分:0)

我修改了Tiago A的答案。 我也遇到了同样的问题,Tiago A的解决方案虽然小巧易行,但存在一些局限性,因此如果其他人需要这样做可能会有所帮助。 感谢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;