Android中自定义ScrollView的滑动监听事件
来源:互联网 发布:淘宝店用喜宝动力效果 编辑:程序博客网 时间:2024/05/07 19:36
项目结构:
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(); }}
2activity_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="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="我是翟浩浩" android:gravity="center" /> </LinearLayout> </android.zhh.com.myapplicationscrollview.LazyScrollView> </RelativeLayout></LinearLayout>
3MainActivity(调用监听事件)
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..."); } }); }}
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9786448
0 0
- Android中自定义ScrollView的滑动监听事件
- Android中自定义ScrollView的滑动监听事件,并在滑动时渐变标题栏背景颜色
- Android中实现监听ScrollView滑动事件
- 【ScrollView】Android ScrollView监听滑动事件
- 自定义scrollview滑动到底部的监听事件
- Android监听ScrollView控件的滑动起始和结束事件
- Android 监听ScrollView的滑动
- Android 监听ScrollView的滑动
- Android 监听ScrollView的滑动
- Android 监听ScrollView的滑动
- Android ScrollView的滑动监听
- [Android] 监听ScrollView惯性滑动事件
- scrollview的滑动监听(自定义)
- 安卓下自定义监听滑动的ScrollView
- Android 自定义ScrollView嵌套滚动兼容,监听滑动状态:顶部、底部、停止、滑动中
- Android ScrollView滑动监听
- ScrollView滑动的监听
- ScrollView的滑动监听
- Struts 2 + websocket handshake unexpected response code 404问题
- 区块链学习日记———智能合约部署(二)
- STL set 用法
- 洛谷p2016战略游戏
- Android和Linux的关系
- Android中自定义ScrollView的滑动监听事件
- No.5 YUV420 格式图像旋转
- ARP缓存欺骗攻击
- Java基础之基本数据类型、封装类型
- king's trouble I
- js 的map方法
- hashset重写hashcode和Equals
- 1070 四舍五入型
- python在html中插入简单的代码并加上时间戳