Reputation: 1119
I have created two ScrollView in same layout. You can say a parallel scrollview. I want to scroll one scrollview manually and in response another scrollview should scroll exactly same way. Scroll length for both view is same. This should happen same for both the scrollviews. I tried a code for this.
horizontalScrollViewB.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
horizontalScrollViewD.scrollTo(horizontalScrollViewB.getScrollX(), horizontalScrollViewB.getScrollY());
}
});
horizontalScrollViewD.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
horizontalScrollViewB.scrollTo(horizontalScrollViewD.getScrollX(), horizontalScrollViewD.getScrollY());
}
});
Here what happened is for the first "B" scrollview it works fine but for "D" scrollview it creates problem in scrolling. I understand the problem but couldnot get the solution. So what should I do to avoid one's call when another is calling "onScrollChangeListener()".
Upvotes: 1
Views: 63
Reputation: 597
Override HorizontalView class
package com.example.shaby.payshare;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;
/**
* Created by shaby on 3/7/2017.
*/
public class HorizontalScroll extends HorizontalScrollView {
private ScrollViewListener scrollViewListener = null;
public interface ScrollViewListener {
void onScrollChanged(HorizontalScroll scrollView, int x, int y, int oldx, int oldy);
}
public HorizontalScroll(Context context) {
super(context);
}
public HorizontalScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HorizontalScroll(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
Implement the interface in your required class
Upvotes: 1