做一个自动滚屏滚动视图通过拖放在Android中 [英] Make a scrollView autoscroll with drag and drop in Android

查看:172
本文介绍了做一个自动滚屏滚动视图通过拖放在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找遍了,也没有找到一个解决方案。

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

我有一个观点(让称之为MyView的)一个滚动视图中。 MyView的大于屏幕。因为我能够得到的相对的X,我的手指里面的MyView的y位置,我想使滚动视图自动滚动到顶部/底部的时候,我的手指进入一定的顶部/底部的门槛。 我有一些想法,即转换拖动位置在屏幕上的位置,但是这并没有解决这个问题。

I have a view (lets call it myView) inside a scrollview. myView is bigger than the screen. Since I'm able to get the relative x,y position of my finger inside myView, I would like to make the scrollView autoscroll to the top/bottom when my finger enters a certain top/bottom threshold. I have some ideas, namely translating the drag location to the screen position but this did not solve this problem.

在此先感谢

欢呼声

推荐答案

好吧,我想通了由我自己。

All right I figured it out by myself.

首先,我不得不延长滚动型类,并增加了一个接口OnScrollViewListener。

First I had to extend the ScrollView class and added an interface 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);
    }
}

接着,在我的活动我插入一个构件mScrollDistance指示的量 像素用户滚动。

Next in my Activity I inserted a member mScrollDistance that indicates the amount of pixels the user scrolls.

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总是得到一个新值和拖动位置将被翻译到视图位置。 我测试了这一点,它适用于布局/视图均大于屏幕尺寸。

Now, mScrollDistance gets always a new value and the drag location will be translated to the view location. I tested this and it works on layouts/views that are bigger than the screen size.

希望有所帮助。

这篇关于做一个自动滚屏滚动视图通过拖放在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆