接着上一篇文章说,再Tv开发的过程中,我需要控制Scrollview的移动距离。这个又怎么搞?在Scrollview里边有一个onScrollChanged方法,但是这个方法不能直接使用。所以自定义一个Scrollview。
代码如下:
public class ObservableScrollView extends HorizontalScrollView { private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) { super(context); } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ObservableScrollView(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); } } }
然后我需要写一个接口去调用onScrollChanged方法,代码如下:
public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);}
然后是在MainActivity里边的使用了!
上来新鲜的代码:
if (rightbuttonnumber == 1 || rightbuttonnumber == 2||rightbuttonnumber == 3) {
myHorizontalScrollView.scrollTo(0, 0);
} else if (rightbuttonnumber == 4||rightbuttonnumber == 5) {
myHorizontalScrollView.scrollTo(690,690);
} else if (rightbuttonnumber == 6||rightbuttonnumber == 7) {
myHorizontalScrollView.scrollTo(1350, 1350);
} else if (rightbuttonnumber == 8||rightbuttonnumber == 9) {
myHorizontalScrollView.scrollTo(2030, 2030);
} else if (rightbuttonnumber == 10||rightbuttonnumber == 11) {
myHorizontalScrollView.scrollTo(2700, 2700);
} else {
myHorizontalScrollView.scrollTo(3380, 3380);
}