在业务需求中,有时会遇到需要监听ScrollView实现页面的一些业务效果
主要思路:创建Handler,在ScrollView滑动的时候,先清空所有消息,然后发送延时消息,如果能接收到消息,说明滑动停止,下面是具体实现的代码
public class ObservableScrollView extends NestedScrollView {
private OnScrollStatusListener onScrollStatusListener;
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);
}
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollStatusListener != null) {
onScrollStatusListener.onScrolling();
mHandler.removeCallbacksAndMessages(null);
mHandler.sendEmptyMessageDelayed(0x01, 200);
}
}
public void setOnScrollStatusListener(OnScrollStatusListener onScrollStatusListener) {
this.onScrollStatusListener = onScrollStatusListener;
}
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0x01:
if (onScrollStatusListener != null) {
onScrollStatusListener.onScrollStop();
}
break;
}
}
};
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacksAndMessages(null);
}
public interface OnScrollStatusListener {
void onScrollStop();
void onScrolling();
}
}
然后就可以引用自定义的ObservableScrollView了,下面是布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yuntu.scrollview.MainActivity"
>
<com.yuntu.scrollview.ObservableScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FF352E"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FF6738"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FF352E"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#4E65FE"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="测试"
android:textColor="#3D4552"
/>
</LinearLayout>
</com.yuntu.scrollview.ObservableScrollView>
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="停止滑动"
android:textColor="#3D4552"
android:textSize="20sp"
/>
</FrameLayout>
然后在代码中
ObservableScrollView scrollView = findViewById(R.id.scrollView);
final TextView tvTest = findViewById(R.id.tv_test);
scrollView.setOnScrollStatusListener(new ObservableScrollView.OnScrollStatusListener() {
@Override public void onScrollStop() {
tvTest.setText("停止滑动");
}
@Override public void onScrolling() {
tvTest.setText("滑动中");
}
});
可以监听到ScrollView的滑动跟静止状态变化。
网友评论