项目结构:

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(布局文件中引用)
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.zhh.com.myapplicationscrollview.LazyScrollView
android:id="@+id/myScrollView"
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="30dp"
android:text="我是zhujiabin"
android:gravity="center"
/>         .......
</LinearLayout>
</android.zhh.com.myapplicationscrollview.LazyScrollView> </RelativeLayout> </LinearLayout>
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...");
}
});
} }

Android 自定义ScrollView的滑动监听事件的更多相关文章

  1. Android开发入门——Button绑定监听事件三种方式

    import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...

  2. swiper 使用参考 禁止手动滑动 监听事件

    最外层容器加类名  swiper-no-swiping 监听切换事件 onTransitionEnd: function(swiper){ console.log('过渡结束'); }

  3. js监听事件 上滑消失下滑出现的效果 触摸与手势事件

    https://www.w3cmm.com/javascript/touch.html //触摸与手势事件连接tinyscrollbar //方法1var _this = $('#fabu');var ...

  4. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

      分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动     转载请标明出处:http: ...

  5. Android——监听事件总结

    各种监听事件 1.按钮 Button(1)点击监听 btn_1.setOnClickListener(new View.OnClickListener() { (2)长按监听 btn_1.setOnL ...

  6. 横向滑动的listview和其中用到的触摸监听事件详解

    一.首先把横向的listview的代码放上来 HorizontalListView: package com.common.cklibrary.utils.myview; import java.ut ...

  7. UI设计篇·入门篇·简单动画的实现,透明动画/旋转动画/移动动画/缩放动画,混合动画效果的实现,为动画设置监听事件,自定义动画的方法

    基本的动画构成共有四种:透明动画/旋转动画/移动动画/缩放动画. 配置动画的方式有两种,一种是直接使用代码来配置动画效果,另一种是使用xml文档配置动画效果 相比而言,用xml文档写出来的动画效果,写 ...

  8. [Android]Fragment自定义动画、动画监听以及兼容性包使用

    Fragment是Android在API 11之后加入的一个组件,对提高Android开发中的布局合理性和布局效率都有很大作用,尤其是在Android平板等大屏幕设备的开发中,Fragment的引入能 ...

  9. Android中Button的五种监听事件

    简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...

随机推荐

  1. C++基础 (10) 第十天 C++中类型转换 异常 栈解旋 io操作

    1之前内容的回顾 C语言中的类型转换(int)a  强转可读性太差了 C++把()拆分成了四种转换方式 static_cast static_cast在编译器编译阶段就进行转换了 2.dynamic_ ...

  2. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)D. Peculiar apple-tree

    In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity ...

  3. Node-Blog整套前后端学习记录

    Node-Blog 后端使用node写的一个一整套的博客系统 #### 主要功能 登录 注册 发表文章 编辑/删除文章 添加/删除/编辑文章分类 账号的管理 评论功能 ... 所用技术 node ex ...

  4. Problem 13

    Problem 13 # Problem_13 """ Work out the first ten digits of the sum of the following ...

  5. CF789C. Functions again

    /* CF789C. Functions again http://codeforces.com/contest/789/problem/C 水题 题意:求数组中的连续和的最大值 */ #includ ...

  6. hdu 1385 floyd记录路径

    可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...

  7. POJ 2137

    水,dp[i][j][k],设为当前为i个牛,在它喜欢的j个位置,而第一个牛在k个位置,很明显了,其实不过是枚举. #include <iostream> #include <cst ...

  8. [HTML 5] Styling with ARIA

    See if you can do a better job styling this button using ARIA states. One huge benefit to styling wi ...

  9. ClientProtocolException

    用httpclient 去訪问页面是会出现org.apache.http.client.ClientProtocolException 我这里出现的原因是在http请求时.请求头缺少user-agen ...

  10. ubuntu中写一个shell脚本的过程

    gedit hello.sh ,然后输入 #!/bin/bash echo "Hello world!" chmod +x hello.sh ./hello.sh