android ScrollView嵌套ListView自动滑动到底部,嵌套ViewPager出现的滑动冲突以及监听滑动停止

1、解决嵌套listview自动滑动到底部问题

2、解决ScrollView嵌套ViewPager出现的滑动冲突问题

3、监听滑动事件

4、监听滑动停止


import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {
	private boolean canScroll;
	private GestureDetector mGestureDetector;
	private OnScrollListener onScrollListener;
	private Runnable scrollerTask;
	private int initialPosition;
	private int newCheck = 100;
	public CustomScrollView(Context context) {
		super(context);
	}

	@SuppressWarnings("deprecation")
	public CustomScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		canScroll = true;
		mGestureDetector = new GestureDetector(new YScrollDetector());
		scrollerTask = new Runnable() {
			public void run() {
				int newPosition = getScrollY();
				if (initialPosition - newPosition == 0) {// has stopped
					if (onScrollListener != null) {
						onScrollListener.onScrollStopped();
					}
				} else {
					initialPosition = getScrollY();
					CustomScrollView.this.postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}

	public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	//**********监听滑动接口*********//
	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

	// **********解决嵌套listview自动滑动到底部问题*********//
	@Override
	protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
		return 0;
	}

	// **********解决ScrollView嵌套ViewPager出现的滑动冲突问题*********//
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_UP)
			canScroll = true;
		return super.onInterceptTouchEvent(ev)
				&& mGestureDetector.onTouchEvent(ev);
	}

	class YScrollDetector extends SimpleOnGestureListener {
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			if (canScroll)
				if (Math.abs(distanceY) >= Math.abs(distanceX))
					canScroll = true;
				else
					canScroll = false;
			return canScroll;
		}
	}


	public void startScrollerTask() {
		initialPosition = getScrollY();
		CustomScrollView.this.postDelayed(scrollerTask, newCheck);
	}
	
	@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);
		}
	}

	public interface OnScrollListener {
		void onScrollChanged(int x, int y, int oldx, int oldy);
		void onScrollStopped();
	}
	
}

监听滑动停止需要实现onTouch接口

scrollView.setOnTouchListener(this);

@Override
public boolean onTouch(View v, MotionEvent event) {
      <span style="white-space:pre">	</span>if (event.getAction() == MotionEvent.ACTION_UP) {
            scrollView.startScrollerTask();
        }
<span style="white-space:pre">	</span>return false;
}


参考:
http://stackoverflow.com/questions/8181828/android-detect-when-scrollview-stops-scrolling


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值