Get scrolling direction in a scrollview?

后端 未结 2 2065
失恋的感觉
失恋的感觉 2020-12-10 09:38

This should be pretty straightforward but it seems that I missed something --- how do you figure out the direction (left/right/up/down) of a scrolling event for a ScrollView

相关标签:
2条回答
  • 2020-12-10 10:17

    Research is telling me that there is no scroll listener for that case.
    You could try your approch.
    Another thing I would try is to override onScrollChanged (which provides the scroll position before) and so creating your own ScrollView

    Update:
    This one workes for me. It scrolls to the right but not to the left.

    public class CustomScrollView extends HorizontalScrollView {
    
        public CustomScrollView(Context context) {
            super(context);
        }
    
        public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onScrollChanged(int w, int h, int ow, int oh) {
            if (w < ow) {
                scrollTo(ow, h);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 10:28

    use this

      scrollView.setOnTouchListener(new View.OnTouchListener() {
      float y1 = 0;
      float y0 = 0;
    
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
    
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
          y0 = motionEvent.getY();
          Log.i("Y", motionEvent.getAction() + " + " + (y0 - y1));
        } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
          y1 = motionEvent.getY();
          if (y0 - y1 > 0) {
            Log.i("Y", motionEvent.getAction() + " + " + (y0 - y1));
          } else if (y0 - y1 < 0) {
            Log.e("Y", motionEvent.getAction() + " - " + (y0 - y1));
          }
        }
    
        return false;
      }
    });
    
    0 讨论(0)
提交回复
热议问题