我有一个MainActivity
,持有10个片段。我想禁用在特定页面中从右向左滑动并显示弹出窗口,而使用取消和确认按钮,单击确认将转到下一个片段。我通过MainActivity
中的OnTouch实现实现了这一目标。但是片段在生成弹出窗口之前正在移动。我在Stackoverflow的其他答案中尝试了很多,但没有运气。 dispatchTouchEvent()
,OnTouchIntercept()
,OnTouchEvent()
有帮助吗?
这是为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;
}
答案 0 :(得分:0)
我想在特定页面中禁用从右到左的滑动
您可以使用自定义ViewPager
执行此操作,使用方法setPagingEnabled
禁用/启用滑动
public class CustomViewPager extends ViewPager {
private boolean enabled;
public AHBottomNavigationViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
/**
* Enable or disable the swipe navigation
* @param enabled
*/
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
由于您成功检测到了滑动,因此您可以应用代码在状态下显示弹出窗口禁用刷取viewpager以显示弹出窗口,然后您可以控制单击确认/取消弹出窗口以设置下一个片段。