欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android解决viewpager嵌套滑动冲突并保留侧滑菜单功能

程序员文章站 2023-11-13 16:54:04
重写子pagerview的dispatchtouchevent方法,在返回前添加一句getparent().requestdisallowintercepttoucheve...

重写子pagerview的dispatchtouchevent方法,在返回前添加一句getparent().requestdisallowintercepttouchevent(true)中断掉事件的传递,类如下

public class supperviewpager extends viewpager {
 private int screenwidth;//屏幕宽度
 public supperviewpager(context context) {
  super(context);
 }
 public supperviewpager(context context, attributeset attrs) {
  super(context, attrs);
  // todo 自动生成的构造函数存根
 }
 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  screenwidth = measurespec.getsize(widthmeasurespec);//view测量时获取屏幕宽度
 }
 @override
 public boolean dispatchtouchevent(motionevent ev) {
  // screenwidth = getresources().getdisplaymetrics().widthpixels;
  system.out.println("屏幕宽度" + screenwidth);
  /*判断屏幕是否满足一定条件,满足则中断时间
  即,两边各留出一定宽度使靠边滑动时可以相应父pagerview 的事件,例如左边有侧滑菜单,右边靠边可以滑到另一个父viewpager的下一个*/
  if (ev.getrawx() > screenwidth / 8 && ev.getrawx() < screenwidth * 7 / 8) {
   getparent().requestdisallowintercepttouchevent(true);
  }
  return super.dispatchtouchevent(ev);
 }
}

以上所述是小编给大家介绍的android解决viewpager嵌套滑动冲突并保留侧滑菜单功能,希望对大家有所帮助