SlidingMenu和Listview冲突解决方案

来源:互联网 发布:华为 5g 知乎 编辑:程序博客网 时间:2024/06/14 07:49

SlidingMenu和Listview冲突解决方案

前些日子做了一个项目用到了仿QQ侧滑效果,然后就在我的男神博客里找到了这篇文章。用上之后效果确实不错,但是bug也随之出现,一但主页面中有Listview就会和项目中的HorizontalScrollView发生冲突,鸿洋大神并没有给出解决方案,下方大家的评论也都没给出,没办法只能自己研究了。最后终于让我找到了解决方案。

解决方案:重写HorizontalScrollView,然后替换掉重写HorizontalScrollView,然后滑起来就比较顺畅了。代码如下:

/** * @author 作者 YYD * @version 创建时间:2016年7月24日 上午7:57:05 * @function 未添加 */public class CustomHorizontalScrollView extends HorizontalScrollView {      float lastX, lastY;      private GestureDetector mGestureDetector;      View.OnTouchListener mGestureListener;      public CustomHorizontalScrollView(Context context) {          super(context);          mGestureDetector = new GestureDetector(context, new YScrollDetector());          setFadingEdgeLength(0);      }      public CustomHorizontalScrollView(Context context, AttributeSet attrs) {          super(context, attrs);          mGestureDetector = new GestureDetector(context, new YScrollDetector());          setFadingEdgeLength(0);      }      public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {          super(context, attrs, defStyleAttr);          mGestureDetector = new GestureDetector(context, new YScrollDetector());          setFadingEdgeLength(0);      }      @Override      public boolean onInterceptTouchEvent(MotionEvent ev) {          return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);      }      /**       * 如果竖向滑动距离<横向距离,执行横向滑动,否则竖向。如果是ScrollView,则'<'换成'>'       */      class YScrollDetector extends GestureDetector.SimpleOnGestureListener {          @Override          public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {              if (Math.abs(distanceY) < Math.abs(distanceX)) {                  return true;              }              return false;          }      }  }  

最后 奉送上代码,希望对大家有所帮助,兄弟手头紧张,小收1分资源费,如果大家不想用分,完全可以按照上面我说的方式自己弄,不用下载,请见谅!

2 1
原创粉丝点击