János János - 2 months ago 17
Android Question

Decide wether scroll was user initiated or programmatically triggered?

Some logic need to be executed in a

HorizontalScrollView
listener only if
HorizontalScrollView
was scrolled by user, and not me was who called
horizontalScrollView.scrollBy(dx, 0);
method at on other code part.

With flag - programmatic call was started, finished - can not really fix problem, because there is a small delay between
scrollBy
method call and triggering event handler. But in this time frame even user could touch and scroll
HorizontalScrollView
.

What approach would you suggest?

horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

@Override
public void onScrollChanged() {

if (horizontalState == 0) {
horizontalState = 2;
}

if (horizontalState == 2) {

int d = ...;

if (d != 0) {
userSettingRecycleView.scrollBy(d , 0);

} else {
horizontalState = 0;
}
}
}
});





userSettingRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy){

if (horizontalState == 1) {

super.onScrolled(recyclerView, dx, dy);
horizontalScrollView.scrollBy(dx, 0);
}
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);

if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {

ItemUserSettingRatingActivity.horizontalState = 1;
}

if (newState == RecyclerView.SCROLL_STATE_IDLE || newState == RecyclerView.SCROLL_STATE_SETTLING) {
ItemUserSettingRatingActivity.horizontalState = 0;
}
}
});

Answer

I would suggest to disable user touches in range between your programm scrool events. That is, use those classes to create your custom HorisontalScrollView and to enable/disable taps' catch in a programm way.