0
我有一個MainActivity
,它包含10個片段。我想禁止在特定頁面中從右向左滑動,並顯示一個彈出窗口,取消和確認按鈕,點擊確認會將您帶到下一個片段。我在MainActivity
的OnTouch實現中實現了這一點。但片段在生成彈出之前正在移動。我在Stackoverflow的其他答案中嘗試了很多,但沒有運氣。 dispatchTouchEvent()
,OnTouchIntercept()
,OnTouchEvent()
有幫助嗎?在ViewPager中從左向右滑動時顯示彈出框,並且在彈出頁面不應該移動
這是爲viewpager實現的OnTouch方法。
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
float finalX;
float finalY;
switch (action) {
case MotionEvent.ACTION_DOWN:
initialX = event.getX();
initialY = event.getY();
//Log.i(TAG, "Action was DOWN");
return true;
/*case MotionEvent.ACTION_MOVE:
//Log.i(TAG, "Action was MOVE");
finalX = event.getX();
finalY = event.getY();
if (initialX > finalX) {
//Log.i(TAG, "Left to Right swipe performed");
return true;
}
else return false;*/
case MotionEvent.ACTION_UP:
finalX = event.getX();
finalY = event.getY();
//Log.i(TAG, "Action was UP");
if (initialX < finalX) {
Log.i(TAG, "Left to Right swipe performed");
return false;
//mViewPager2.setCurrentItem(mViewPager2.getCurrentItem()-1);
}
if (initialX > finalX) {
Log.i(TAG, "Right to Left swipe performed");
int a = mViewPager2.getCurrentItem();
if(a==0||a==1||a==2||a==4||a==6){
displayPopup();
return true;
}
else if(a==7 && screenPreferences.getInt("user_login",0) == 0){
displayPopup();
return true;
}
else return false;
}
if (initialY < finalY) {
//Log.i(TAG, "Up to Down swipe performed");
return true;
}
if (initialY > finalY) {
//Log.i(TAG, "Down to Up swipe performed");
return true;
}
break;
case MotionEvent.ACTION_CANCEL:
//Log.i(TAG,"Action was CANCEL");
break;
case MotionEvent.ACTION_OUTSIDE:
//Log.i(TAG, "Movement occurred outside bounds of current screen element");
break;
}
return false;
}
謝謝。它終於奏效了。用你給出的代碼和線索,我已經嘗試了最後一次,它的工作。我的天啊 –