检测用户何时从页面边缘滑动Viewpager [英] detect when user swipes the viewpager from edge of a page

查看:66
本文介绍了检测用户何时从页面边缘滑动Viewpager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定有关于它的帖子,我以前浏览过它们,但现在找不到了.

I am sure that there are some posts about it, and I skimmed through them before, but now I cannot find them.

我想检测用户从页面边缘(例如左边缘)滑动Viewpager时的操作.我想对这种滑动进行一些特殊的处理,例如显示菜单.

I want to detect the action when the user swipes the viewpager from edge of a page (such as left edge). I want to do some special handling for this kind of swipe, such as showing menu.

是否有ViewPager内置(?)支持?我隐约记得它是.否则,我必须实现自己的逻辑来检测这些动作.

Is there any ViewPager built-in(?) support for it? I vaguely remembered it is. Otherwise, I have to implement my own logic to detect those action.

有人可以给我一些信息吗?

Can anyone point me some info?

推荐答案

我认为,您应该重写ViewPager容器的几种方法.例如,如果是LinearLayout

I think, you should override few methods of the container of your ViewPager. For example if it is 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 结合使用(将其检测器放在onTouchEvent()内),您可能会得到不错的结果.但是使用DrawerLayout的最佳方法.

If you combine with TeRRo anrwer (put his detector inside onTouchEvent()) you may have good result. But best way to use DrawerLayout.

祝你好运!

这篇关于检测用户何时从页面边缘滑动Viewpager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆