==========================================================================================================
public class MyHScrollView extends HorizontalScrollView
{
ScrollViewObserver mScrollViewObserver = new ScrollViewObserver();
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
/*
* 当滚动条移动后,引发滚动事件。通知给观察者,观察者会传达给其他的。
*/
if (mScrollViewObserver != null /*&& (l != oldl || t != oldt)*/)
{
mScrollViewObserver.NotifyOnScrollChanged(l, t, oldl, oldt);
}
super.onScrollChanged(l, t, oldl, oldt);
}
}
/*
* 当发生了滚动事件时
*/
public static interface OnScrollChangedListener
{
public void onScrollChanged(int l, int t, int oldl, int oldt);
}
/*
* 观察者
*/
public static class ScrollViewObserver
{
List<OnScrollChangedListener> mList;
public ScrollViewObserver()
{
super();
mList = new ArrayList<OnScrollChangedListener>();
}
public void NotifyOnScrollChanged(int l, int t, int oldl, int oldt)
{
if (mList == null || mList.size() == 0)
{
return;
}
for (int i = 0; i < mList.size(); i++)
{
if (mList.get(i) != null)
{
mList.get(i).onScrollChanged(l, t, oldl, oldt);
}
}
}
}
class OnScrollChangedListenerImp implements OnScrollChangedListener
{
MyHScrollView mScrollViewArg;
//构造参数一个滚动轴,构造时赋给自己的属性
public OnScrollChangedListenerImp(MyHScrollView scrollViewar)
{
mScrollViewArg = scrollViewar;
}
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt)
{
mScrollViewArg.smoothScrollTo(l, t);
}
};
//实例化列头的水平滚动轴并给其订阅数据行的滚动变化。
MyHScrollView headSrcrollView = (MyHScrollView) mHead.findViewById(R.id.horizontalScrollView1);
headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(scrollView1));