Listening to Scroll Events Horizontalscrollview Android

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.

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: how to listen for scrolling events?

You can only create a class that extends ScrollView and override the onScrollChanged() method.

How to pass scroll events to another view, but keep the tap/click events and react to them

For the HorizontalScrollView do this:

private float xCoordinateOfScreenTouch;
private float xCoordinateOfDragRelease;
private static final int MIN_DISTANCE = 40;

horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
xCoordinateOfScreenTouch = event.getX();
break;
case MotionEvent.ACTION_UP:
xCoordinateOfDragRelease = event.getX();
float deltaX = xCoordinateOfDragRelease - xCoordinateOfScreenTouch;

if (Math.abs(deltaX) > MIN_DISTANCE) {

if (xCoordinateOfDragRelease > xCoordinateOfScreenTouch) {
// left to right
// do whatever (pass the event)
} else {
// right to left
// do whatever (pass the event)
}
}
break;
}
return true;
}
});

For it's children, whom the ACTION_DOWN event gets passed to by default, do this:

    textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
xCoordinateOfScreenTouch = event.getRawX();
}
return false;
}
});

I want to detect the end of scroll event

Thank to all of you.
after some testes;i see that the values int i, int i1, int i2 take the same value egual to 0 at the real time so it solve my problem.
I will not use your suggestions now but i learn some important notion from them only by reading.

Android How to scroll HorizontalScrollView with scroll of another parellel HorizontalScrollView

You can set the scrollViews to onTouchListener and let the other scrollViews scroll with it. I had the same a long time ago. You can do the trick for example:

 scrollOne = (HorizontalScrollView)findViewById(R.id.horizontal_one);
scrollTwo = (HorizontalScrollView)findViewById(R.id.horizontal_two);

scrollTwo.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub

int scrollX = view.getScrollX();
int scrollY = view.getScrollY();

scrollOne.scrollTo(scrollX, scrollY);
return false;
}

});

That was my solution in that question:listening to scroll events horizontalscrollview android

For me it worked fine...



Related Topics



Leave a reply



Submit