Android重寫ViewPager修改滑動靈敏度
阿新 • • 發佈:2019-02-08
使用ViewPager作為一個頁面進行切換,裡面可以存放很多View,但有時在操作View時不小心滑動一下就有可能跳到下一頁,這並不是我們想要的,這裡就需要重寫ViewPager修改它的滑動條件
效果圖
程式目錄結構
BTViewPager.java
- package com.example.viewpagerdemo;
- import android.content.Context;
- import android.support.v4.view.ViewPager;
- import android.util.AttributeSet;
-
import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.Scroller;
- /**
- * 解決ViewPager滑動過於靈敏,只有滑動距離大於100才滑到另一頁
- *
- * @author Administrator
- *
- */
- publicclass BTViewPager extends ViewPager {
- privatestaticfinal String TAG = "dzt_pager";
-
privatestaticfinalint MOVE_LIMITATION = 100;// 觸發移動的畫素距離
- privatefloat mLastMotionX; // 手指觸碰螢幕的最後一次x座標
- privateint mCurScreen;
- private Scroller mScroller; // 滑動控制元件
- public BTViewPager(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- init(context);
- }
-
public BTViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- init(context);
- }
- privatevoid init(Context context) {
- mScroller = new Scroller(context);
- mCurScreen = 0;// 預設設定顯示第一個VIEW
- }
- @Override
- publicboolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- finalint action = event.getAction();
- finalfloat x = event.getX();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- Log.d(TAG, "[BTViewPager->]onTouchEvent ACTION_DOWN");
- mLastMotionX = x;
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d(TAG, "[BTViewPager->]onTouchEvent ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d(TAG, "Item = " + getCurrentItem() + " count = "
- + getChildCount());
- if (Math.abs(x - mLastMotionX) < MOVE_LIMITATION) {
- // snapToDestination(); // 跳到指定頁
- snapToScreen(getCurrentItem());
- returntrue;
- }
- break;
- default:
- break;
- }
- Log.d(TAG, "[BTViewPager->]onTouchEvent--end");
- returnsuper.onTouchEvent(event);
- }
- @Override
- publicvoid computeScroll() {
- // TODO Auto-generated method stub
- Log.d(TAG, "[BTViewPager->]computeScroll");
- super.computeScroll();
- if (mScroller.computeScrollOffset()) {
- Log.d(TAG,
- "[BTViewPager->]computeScroll x = " + mScroller.getCurrX());
- scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
- invalidate();
- }
- }
- /**
- * 根據滑動的距離判斷移動到第幾個檢視
- */
- publicvoid snapToDestination() {
- finalint screenWidth = getWidth();
- finalint destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
- Log.d(TAG, "[BTViewPager->]snapToDestination screenWidth = "
- + screenWidth + " destScreen = " + destScreen);
- snapToScreen(destScreen);
- }
- /**
- * 滾動到制定的檢視
- *
- * @param whichScreen
- * 檢視下標
- */
- publicvoid snapToScreen(int whichScreen) {
- // whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() -
- // 1));
- if (getScrollX() != (whichScreen * getWidth())) {
- finalint delta = whichScreen * getWidth() - getScrollX();
- Log.d(TAG, "[BTViewPager->]snapToScreen-whichScreen = "
- + whichScreen + " delta = " + delta + " scrollX = "
- + getScrollX());
- mScroller.startScroll(getScrollX(), 0, delta, 0,
- Math.abs(delta) * 2);
- mCurScreen = whichScreen;
- invalidate();
- }
- }
- /**
- * 用於攔截手勢事件的,每個手勢事件都會先呼叫這個方法。Layout裡的onInterceptTouchEvent預設返回值是false,
- * 這樣touch事件會傳遞到childview控制元件 ,如果返回false子控制元件可以響應,否則了控制元件不響應,這裡主要是攔截子控制元件的響應,
- * 對ViewGroup不管返回值是什麼都會執行onTouchEvent
- */
- @Override
- publicboolean onInterceptTouchEvent(MotionEvent arg0) {
- // TODO Auto-generated method stub
- Log.d(TAG, "[BTViewPager->]onInterceptTouchEvent");
- finalint action = arg0.getAction();
- finalfloat x = arg0.getX();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- Log.d(TAG, "onInterceptTouchEvent---ACTION_DOWN ");
- mLastMotionX = x;
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d(TAG, "onInterceptTouchEvent---ACTION_MOVE ");
- break;
- case MotionEvent.ACTION_UP:
- Log.d(TAG, "onInterceptTouchEvent---ACTION_UP ");
- break;
- default:
- break;
- }
- returnsuper.onInterceptTouchEvent(arg0);
- }
- }
- if (Math.abs(x - mLastMotionX) < MOVE_LIMITATION) {
- // snapToDestination(); // 跳到指定頁
- snapToScreen(getCurrentItem());
- returntrue;
- }
完整Demo:http://download.csdn.net/detail/deng0zhaotai/7384637