滚动屏幕后执行操作

时间:2019-04-12 02:10:26

标签: android scrollview

我试图在屏幕水平滚动后显示祝酒词。滚动可以在屏幕上的任何地方进行,但是我希望仅在屏幕从右向左滚动时才显示吐司。我已经尝试过了:

HorizontalScrollView headerScrollView = new HorizontalScrollView(this);

       headerScrollView.setOnTouchListener(new View.OnTouchListener() {

           @Override
           public boolean onTouch(View v, MotionEvent event) {
               Toast.makeText(Verbs.this, "The screen has been scrolled", Toast.LENGTH_LONG).show();
               return true;
           }
       });

2 个答案:

答案 0 :(得分:2)

您必须重写setOnScrollChangeListener方法以检测从右向左滚动。

HorizontalScrollView headerScrollView = new HorizontalScrollView(this);
    headerScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            if (scrollX > oldScrollX) {
                Toast.makeText(Verbs.this, "moved right to left", Toast.LENGTH_LONG).show();
            }
        }
    });

答案 1 :(得分:0)

我相信您正在寻找的是scrollListener

HorizontalScrollView headerScrollView = new HorizontalScrollView(this);
headerScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            int scrollY = rootScrollView.getScrollY(); // For ScrollView
            int scrollX = rootScrollView.getScrollX(); // For HorizontalScrollView
            // DO SOMETHING WITH THE SCROLL COORDINATES
        }
    });