我确信有一些关于它的帖子,我以前浏览过,但是现在我找不到了。
当用户从页面的边缘(如左边沿)滑动查看器时,我希望检测到该操作。我想为这种滑动做一些特殊的处理,例如显示菜单。
有没有内置的ViewPager (?)支持它?我隐约记得是的。否则,我必须实现我自己的逻辑来检测这些行为。
有人能告诉我一些信息吗?

发布于 2014-12-07 20:13:13
我认为,您应该重写ViewPager容器的几个方法。例如,如果是LinearLayout
public class Liner extends LinearLayout {
public Liner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
//Remember start points
float mInitX;
float mInitY;
// TouchSlop value
float mSomeValue=20F;
//
boolean mCatched=false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitX=ev.getX();
mInitY=ev.getY();
break;
default:
break;
}
mCatched=mInitX<10 && Math.abs(ev.getX()-mInitX)>mSomeValue;
return mCatched;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(mCatched){
//Do all you want here
}
return true;
}}
如果你和TeRRo anrwer (把他的探测器放在onTouchEvent()里)结合,你可能会有很好的效果。但使用DrawerLayout的最佳方式。
祝好运!
发布于 2014-12-07 21:06:18
只需检查边缘掩模
public void onTouchEvent(MotionEvent event){
if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
}
} 如果将代码片段放入onInterceptTouchEvent(MotionEvent event)覆盖中,同样的逻辑也会起作用。
发布于 2014-12-07 18:50:10
通过这个例子,您可以控制ViewPager中的所有类型的手势,现在只需要添加一些条件来处理onSwipeLeft()和onSwipeRight()中的边缘。
public class OnSwipeTouchListener implements OnTouchListener {
@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onTouch(e);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// onTouch(e);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onTouch(MotionEvent e) {
}
public void onSwipeRight() {
//call this only if your condition was set
}
public void onSwipeLeft() {
//nothing, this means,swipes to left will be ignored
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}在viewpager对象中设置自定义OnTouchListener:
viewPager.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
if(your conditionIsMet){
// move your pager view to the right
}
}
});我希望这对你有帮助。
https://stackoverflow.com/questions/27346030
复制相似问题