1

I am developing a News Reader type android app where I want to clear my custom header and footer bars when user start scrolling the news list. Again those bar should appear when user stop scrolling the news. This way I can provide the user more space for reading the news. But the problem is, I can not find a way to fire an event like onScrollStop or onScrollStart. Please help.

orchidrudra
  • 1,172
  • 1
  • 12
  • 30

2 Answers2

1

In my project I had to intercept the end of the scroll so I implemented my own ScrollListener:

public class VerticalScrollLayout extends ScrollView {
    private GestureDetector gdScrolling;
    private boolean isScrolling;

    private OnScrollListener lOnScroll;

    public VerticalScrollLayout(Context context) {
        super(context);
        setVerticalScrollBarEnabled(false);

        gdScrolling = new GestureDetector(new SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                isScrolling = true;
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
        });

        setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gdScrolling.onTouchEvent(event)) {
                    return false;
                }

                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(isScrolling ) {
                        isScrolling  = false;
                        if(lOnScroll != null) {
                            lOnScroll.onScrollEnded();
                        }
                    }
                }

                return false;
            }
        });
    }

    public void setOnScrollListener(OnScrollListener l) {
        lOnScroll = l;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if(lOnScroll != null) {
            lOnScroll.onScrollChanged(x, y, oldx, oldy);
        }
    }

    public interface OnScrollListener {
        void onScrollChanged(int x, int y, int oldx, int oldy);
        void onScrollEnded();
    }
}

It might not be best practice, but it works well.

sebataz
  • 1,025
  • 2
  • 11
  • 35
  • by the way if there is a better way to achieve this I'd be interested too. – sebataz Feb 01 '12 at 14:30
  • This option works, but I think there must be a simpler way to do it. Hope any one wants to share it with us. 1 up for your effort. :) – orchidrudra Feb 02 '12 at 09:22
0

Check out the List13 Android API Demo.

Essentially your Activity should implement ListView.OnScrollListener. Then you have the callbacks:

public void onScrollStateChanged(AbsListView view, int scrollState) {
    switch (scrollState) {
    case OnScrollListener.SCROLL_STATE_IDLE:

        break;
    case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:

        break;
    case OnScrollListener.SCROLL_STATE_FLING:

        break;
    }
}

Dont forget to call setOnScrollListener(this) on your ListView.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • Thanks Paul, but these solutions are applicable to a ListView, I don't know how to implement those to a ScrollView. Any help please? – orchidrudra Feb 01 '12 at 14:23
  • Oh wow, sorry about that (I somehow ignored the title!). This answer was marked as working: http://stackoverflow.com/questions/3513594/android-scrollview-setonscrolllistener-like-listview – Paul Burke Feb 01 '12 at 15:03