您的位置:首页 > 移动开发 > Android开发

Android ScrollView 滚动监听

2016-09-12 15:15 435 查看
ScrollView在工作中是经常用到的一个控件,来看下官方给我们的说明:

/**
* Layout container for a view hierarchy that can be scrolled by the user,
* allowing it to be larger than the physical display.
*/


它是一个可以由用户进行滚动,可以显示比物理屏幕大的视图层次容器,它继承于FramLayout。今天这里不是要分析它的源码,而是要对于其中的一个接口进行说明。

在很多时候,我们在使用ScrollView的时候需要监听它的滚动状态,但是它不像 ListView 那样给我们做好了 onScroll() 监听借口。那么我们要怎么做。

我们可以发现其实ScrollView 里面是有关于滚动的 方法的,是叫 onScrollChanged(),但是为什么我们不能直接引用呢?点进去看下,原来他是 protected 方法,我们不可以直接引用它:

/**
* This is called in response to an internal scroll in this view (i.e., the
* view scrolled its own contents). This is typically as a result of
* {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
* called.
*
* @param l Current horizontal scroll origin.
* @param t Current vertical scroll origin.
* @param oldl Previous horizontal scroll origin.
* @param oldt Previous vertical scroll origin.
*/
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
notifySubtreeAccessibilityStateChangedIfNeeded();

if (AccessibilityManager.getInstance(mContext).isEnabled()) {
postSendViewScrolledAccessibilityEventCallback();
}

mBackgroundSizeChanged = true;
if (mForegroundInfo != null) {
mForegroundInfo.mBoundsChanged = true;
}

final AttachInfo ai = mAttachInfo;
if (ai != null) {
ai.mViewScrollChanged = true;
}

if (mListenerInfo != null && mListenerInfo.mOnScrollChangeListener != null) {
mListenerInfo.mOnScrollChangeListener.onScrollChange(this, l, t, oldl, oldt);
}
}


那我们如果要调用它的话,就需要多做一步工作,自己把它曝露出来,让它变成 公共的,就好比,你爸爸有个厂,生产某件商品,他和你说这个商品你可以随便拿,不要钱,但是你不能直接拿出去卖
4000
。如果你足够聪明的话,你肯定会想个办法绕过去,比如说自己拿了这个商品后自己再换个包装,拿出去卖,这个卖的就是你自己的商品,不是你爸爸生产的商品,成功的挣到钱。
那么我们需要做的是就是拿到它,自己给它再包装下:

public class MyScrollView extends ScrollView {

private ScrollViewListener scrollViewListener;

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);

if (scrollViewListener != null) {
scrollViewListener.onMyScroll(l, t, oldl, oldt);
}
}

public interface ScrollViewListener {
void onMyScroll(int x, int y, int oldx, int oldy);
}
}


在xml中调用:

<view.MyScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>

</view.MyScrollView>


在Activity中:

public class MainActivity extends AppCompatActivity {

private MyScrollView myScrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myScrollView = (MyScrollView) findViewById(R.id.myscrollview);
myScrollView.setScrollViewListener(new MyScrollView.ScrollViewListener() {
@Override
public void onMyScroll(int x, int y, int oldx, int oldy) {
Log.i("MyScrollView","-----------x--------------------- "+x);
Log.i("MyScrollView","-----------y--------------------- "+y);
Log.i("MyScrollView","-----------oldx--------------------- "+oldx);
Log.i("MyScrollView","-----------oldy--------------------- "+oldy);
}
});

}
}


其中的四个参数,x 是 现在的水平位置 ;y 是目前的竖直位置; oldx 是之前的水平位置;oldy是之前竖直位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐