How to Have Onscrolllistener for a Scrollview

Can I have onScrollListener for a ScrollView?

Every instance of View calls getViewTreeObserver(). Now when holding an instance of ViewTreeObserver, you can add an OnScrollChangedListener() to it using the method addOnScrollChangedListener().

You can see more information about this class here.

It lets you be aware of every scrolling event - but without the coordinates. You can get them by using getScrollY() or getScrollX() from within the listener though.

scrollView.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
}
});

Android: ScrollView 'setOnScrollListener' (like ListView)

You can try extending ScrollView and overriding View#onScrollChanged and the doing your checks there. You have to extend ScrollView since onScrollChanged is a protected method.

Android scrollview onScrollChanged

No.

ScrollView doesn't provide a listener for scroll events or even a way to check how far down the user has scrolled, so you have to do what is suggested by the link.

Capture onScroll event in Android ScrollView

Unfortunately, there is no pre-built way for getting these events. However, there are two workarounds:

  • Use a ListView instead

  • Build your own custom ScrollView with ScrollListener. This is not that hard to achieve and has been done before.

listening to scroll events horizontalscrollview android

You may want to try creating your own custom class that extends HorizontalScrollView and overriding the onScrollChanged() function as such

public class TestHorizontalScrollView extends HorizontalScrollView {

public TestHorizontalScrollView(Context context) {
super(context);
}


@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
Log.i("Scrolling", "X from ["+oldl+"] to ["+l+"]");
super.onScrollChanged(l, t, oldl, oldt);
}

}

This overriden function will catch all changes to the scroll position even when the view is not being touched. This should keep your scroll views in sync.

onKeyListener with onScrollListener

I was able to find a solution to my problem by using dispatchKeyEvent(KeyEvent event). I ended up with something like

 public boolean dispatchKeyEvent(KeyEvent event) {
mcols= mcols + 1;
if(event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
gridLayoutE.getFocusedChild().setBackgroundColor(Color.BLACK);
gridLayoutE.findViewById(mcol).setBackgroundColor(Color.GREEN);
}
return super.dispatchKeyEvent(event);
}

This along with my scrolllistener, allowed my to scroll the grid and change each cell as I did.

Get Scroll in Recyclerview inside Nested Scrollview

I just handled issue by using gesture motion



Related Topics



Leave a reply



Submit