项目结构:
1.LazyScrollView类(自定义ScrollView)
package android.zhh.com.myapplicationscrollview;/** * Created by sky on 2017/3/19. */import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.ScrollView;/** * Created by sky on 2017/3/17. */public class LazyScrollView extends ScrollView { private static final long DELAY = 100; private int currentScroll; private Runnable scrollCheckTask; /** * @param context */ public LazyScrollView(Context context) { super(context); init(); } /** * @param context * @param attrs */ public LazyScrollView(Context context, AttributeSet attrs) { super(context, attrs); init(); } /** * @param context * @param attrs * @param defStyle */ public LazyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { scrollCheckTask = new Runnable() { @Override public void run() { int newScroll = getScrollY(); if (currentScroll == newScroll) { if (onScrollListener != null) { onScrollListener.onScrollStopped(); } } else { if (onScrollListener != null) { onScrollListener.onScrolling(); } currentScroll = getScrollY(); postDelayed(scrollCheckTask, DELAY); } } }; setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { currentScroll = getScrollY(); postDelayed(scrollCheckTask, DELAY); } return false; } }); } public interface OnScrollListener { public void onScrollChanged(int x, int y, int oldX, int oldY); public void onScrollStopped(); public void onScrolling(); } private OnScrollListener onScrollListener; /** * @param onScrollListener */ public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } @Override protected void onScrollChanged(int x, int y, int oldX, int oldY) { super.onScrollChanged(x, y, oldX, oldY); if (onScrollListener != null) { onScrollListener.onScrollChanged(x, y, oldX, oldY); } } /** * @param child * @return */ public boolean isChildVisible(View child) { if (child == null) { return false; } Rect scrollBounds = new Rect(); getHitRect(scrollBounds); return child.getLocalVisibleRect(scrollBounds); } /** * @return */ public boolean isAtTop() { return getScrollY() <= 0; } /** * @return */ public boolean isAtBottom() { return getChildAt(getChildCount() - 1).getBottom() + getPaddingBottom() == getHeight() + getScrollY(); }}
2.activity_main.xml(布局文件中引用)
.......
3.MainActivity(调用监听事件)
package android.zhh.com.myapplicationscrollview;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {// 初始化自定义的ScrollView private LazyScrollView myScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myScrollView = (LazyScrollView)findViewById(R.id.myScrollView);// 自定义的ScrollView的滑动监听事件 myScrollView.setOnScrollListener(new LazyScrollView.OnScrollListener() { @Override public void onScrollChanged(int x, int y, int oldX, int oldY) { Log.e("@", "x:" + oldX + "->" + x + ", y:" + oldY + "->" + y); } @Override public void onScrollStopped() { if (myScrollView.isAtTop()) { Toast.makeText(MainActivity.this, "Stopped at top", Toast.LENGTH_SHORT).show(); } else if (myScrollView.isAtBottom()) { Toast.makeText(MainActivity.this, "Stopped at bottom", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Stopped", Toast.LENGTH_SHORT).show(); } } @Override public void onScrolling() { Log.e("@", "scrolling..."); } }); }}