有我的解決方案,完美的作品,嘗試讀取代碼
public class CustomScrollView extends NestedScrollView
{
private boolean mIsScrolling;
private boolean mIsTouching;
private OnScrollListener mOnScrollListener;
private Runnable mScrollingRunnable;
public interface OnScrollListener
{
void onScrollChanged(CustomScrollView scrollView, int x, int y, int oldX, int oldY);
void onEndScroll(CustomScrollView scrollView);
void onGoUp();
void onGoDown();
}
public CustomScrollView(final Context context)
{
super(context);
}
public CustomScrollView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public CustomScrollView(final Context context, final AttributeSet attrs, final int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
private int y = 0;
@Override
public boolean dispatchTouchEvent(MotionEvent iEv)
{
if (isEnabled())
{
processEvent(iEv);
super.dispatchTouchEvent(iEv);
return true; //to keep receive event that follow down event
}
return super.dispatchTouchEvent(iEv);
}
private void processEvent(final MotionEvent iEv)
{
switch (iEv.getAction())
{
case MotionEvent.ACTION_DOWN:
y = (int) iEv.getY();
break;
case MotionEvent.ACTION_UP:
y = (int) iEv.getY();
if (mIsTouching && !mIsScrolling)
{
if (mOnScrollListener != null)
{
//If you need to know when end of scrolling just uncomment
mOnScrollListener.onEndScroll(this);
}
}
mIsTouching = false;
break;
case MotionEvent.ACTION_MOVE:
mIsTouching = true;
mIsScrolling = true;
int newY = (int) iEv.getY();
int difY = y - newY;
int MAX_VALUE = 200;
int MIN_VALUE = -200;
if (difY > MAX_VALUE)
{
AppUtils.printLog(Log.ERROR, "TAG", "difY down : " + difY + " , y : " + y + ", newY : " + newY);
if (mOnScrollListener != null)
{
mOnScrollListener.onGoDown();
}
y = newY;
}
else if (difY < MIN_VALUE)
{
AppUtils.printLog(Log.ERROR, "TAG", "difY up : " + difY + " , y : " + y + ", newY : " + newY);
if (mOnScrollListener != null)
{
mOnScrollListener.onGoUp();
}
y = newY;
}
break;
}
}
@Override
protected void onScrollChanged(int iX, int iY, int iOldX, int iOldY)
{
super.onScrollChanged(iX, iY, iOldX, iOldY);
if (Math.abs(iOldX - iX) > 0)
{
if (mScrollingRunnable != null)
{
removeCallbacks(mScrollingRunnable);
}
mScrollingRunnable = new Runnable()
{
public void run()
{
if (mIsScrolling && !mIsTouching)
{
if (mOnScrollListener != null)
{
mOnScrollListener.onEndScroll(CustomScrollView.this);
}
}
mIsScrolling = false;
mScrollingRunnable = null;
}
};
postDelayed(mScrollingRunnable, 200);
}
if (mOnScrollListener != null)
{
mOnScrollListener.onScrollChanged(this, iX, iY, iOldX, iOldY);
}
}
public OnScrollListener getOnScrollListener()
{
return mOnScrollListener;
}
public void setOnScrollListener(OnScrollListener mOnEndScrollListener)
{
this.mOnScrollListener = mOnEndScrollListener;
}
}
使用方法:只需加此自定義視圖中的XML
<ui.customWidgets.CustomScrollView
android:id="@+id/scroll_frag_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:scrollbars="none"
app:behavior_overlapTop="192dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Your code here-->
</LinearLayout>
</ui.customWidgets.CustomScrollView>
代碼
mScrollView.setOnScrollListener(new CustomScrollView.OnScrollListener()
{
@Override
public void onScrollChanged(final CustomScrollView scrollView, final int x, final int y, final int oldX, final int oldY)
{
}
@Override
public void onEndScroll(final CustomScrollView scrollView)
{
}
@Override
public void onGoUp()
{
}
@Override
public void onGoDown()
{
}
});
您已經採取了正確的方法,但所有這些變量是什麼? 'S','K','P','V','A','E','Z','X','Y'?你爲什麼需要它們?您只需計算x和y座標之間的差異即可獲得手指的移動量。 –