监听ListView是否滚动到顶部

package com.example.propertyanimation;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * @类名:MyListView.java 
* @功能描述: 监听ListView滚动到顶部
* @作者:XuanKe'Huang
* @时间:2015-6-5 下午8:25:02
* @Copyright @2015
*/
public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context) { super(context); } private int startY = 0; private int distanceY = 0; @Override public boolean onTouchEvent(MotionEvent ev) { if (listener != null) switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: startY = (int) ev.getRawY(); distanceY = 0; break; case MotionEvent.ACTION_MOVE: distanceY = (int) (ev.getRawY() - startY); break; case MotionEvent.ACTION_UP: if (getTop() == getChildAt(0).getTop() && distanceY > 50) { listener.onScrollBottomListener(distanceY); } startY = 0; distanceY = 0; break; } return super.onTouchEvent(ev); } /** * 方法名: setOnScrollToHeadLintener * * 功能描述:设置监听 * * @param listener * @param layout * ScrollView包含的Layout * @return void * * throws */ public void setOnScrollToHeadLintener(OnScrollToBottomListener listener) { this.listener = listener; } public interface OnScrollToBottomListener { // 当前滑动的距离 public void onScrollBottomListener(int top); } private OnScrollToBottomListener listener; }

你可能感兴趣的:(Android)