如果单击一个位于片段中的图像按钮,我想根据条件控制viewpager滚动。我使用了一个单独的监听器,并从我的MainActivity中调用了该监听器:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<Response>
<Gather action="phonemenu.cshtml?node=default" numDigits="1">
<Say>Hello and welcome to the Initech Phone Menu</Say>
<Say>For business hours, press 1</Say>
<Say>For directions, press 2</Say>
<Say>To hear a duck quack, press 3</Say>
<Say>To speak to a receptionist, press 0</Say>
</Gather>
</Response>
</body>
</html>
我的方法是否正确?如何检查图像按钮是否被触摸然后解锁了viewpager滚动?
答案 0 :(得分:0)
如果你想&#34;控制&#34;在ViewPager的滑动中,你必须写你的 拥有类,扩展ViewPager并覆盖onTouchEvent方法。
@Override
public boolean onTouchEvent(MotionEvent ev) {
return !isEnabled() || super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return !isEnabled() && super.onInterceptTouchEvent(ev);
}
通过这种方式,您可以简单地将viewPager.setEnable(boolean)设置为 恩或残疾。
如果你想跟踪viewPager本身的点击,它会变得有点棘手。
这样,您必须在viewPager中添加手势检测器 实现。像这样举例如:
public class YourViewPager extends ViewPager {
private GestureDetectorCompat gestureDetector;
private View.OnClickListener listener;
public YourViewPager (Context context) {
super(context);
init(context);
}
public YourViewPager (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
gestureDetector = new GestureDetectorCompat(context, new TapGestureListener()); ;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isEnabled()) {
gestureDetector.onTouchEvent(ev);
}
return !isEnabled() || super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return !isEnabled() && super.onInterceptTouchEvent(ev);
}
@Override
public void setCurrentItem(int item) {
if (isEnabled()) {
super.setCurrentItem(item);
}
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
if (isEnabled()) {
super.setCurrentItem(item, smoothScroll);
}
}
@Override
public void setOnClickListener(OnClickListener l) {
this.listener = l;
}
/**
* Delegates onClick events to the listeners and invokes a smooth swipe.
*/
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (listener != null) {
listener.onClick(OnBoardingViewPager.this);
}
return super.onSingleTapUp(e);
}
}
}