(背景:本人进入开发领域10个月,直接学的android,java随着android的学习而同步进行的,技术比较菜,各位看官勿喷)
以前看到各位大牛的博客,一直很羡慕,也很想参与,可终究是各种理由!没想到还是突破了,加油~~~
作为我第一篇博客,要从今天看到的一个效果说起,误打误撞,今天在qq群里说起微博,就去关注了下群里一个朋友的微博,在里面发现了转载的一个效果~(柳暗花明)
两个scrollview同步滚动,使我对接口的理解再次升华,以前一直知道interface,但自己不会用。
自己的实现,感觉方法有点低级~ 首先对项目分析,知道要用scrollview,但查阅api没发现onScrollChangedListener,看源码发现是个protected方法,我第一个念头是不能用,重写scrollview比较麻烦,就在onTouchListener里面想办法,最后用的开启thread、hanler方法监听ScrollVIew的滚动getScrollY(),总之不理想
博客上大牛的实现: http://blog.csdn.net/weidi1989/article/details/8621769
1.ScrollView里面有个protected void onChanged(),因此需要定义个接口interface实现 滑动监听方法,
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView,int w, int h, int oldw, int oldh );
}
2.我们需要重写ScrollView才能实现该接口
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, w, h, oldw, oldh);
}
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
}
对于接口的使用有待进一步了解~