当前位置: 移动技术网 > IT编程>移动开发>Android > Android listview的滑动冲突解决方法

Android listview的滑动冲突解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

长柄滤头,51小游戏,中国建设银行重庆分行

android listview的滑动冲突解决方法

在android开发的过程中,有时候会遇到子控件和父控件都要滑动的情况,尤其是当子控件为listview的时候。就比如在一个scrollview里有一个listview,这种情况较常见,就会出现这种滑动冲突的情况。这种情况也比较常见,有时候就是这样,没法,但是,了解事件分发的我们知道应该怎么处理这样的事情

有两点需要注意:

一般来说,view的ontouchevent返回true,即消耗点击事件,viewgroup的onintercepttouchevent返回false,即不拦截点击事件,这一点从android源码中可以看出来。但是listview的父类abslistview重写了onintercepttouchevent,返回了true,注意这里不是一定返回true,但是我觉得这一点可以先忽略。

ontouchevent和onintercepttouchevent的调用顺序。点击事件从父控件向子控件传递,如果父控件不拦截,则交由子控件拦截,如果父控件拦截了,则交由父控件的ontouchevent处理,如果最终处理点击事件的控件的ontouchevent返回了false,则将会直接调用其父控件的ontouchevent,如此向上类推

其实解决方法也很简单:重写父控件的onintercepttouchevent函数,在move的时候根据需要返回true,比如左右滑动返回true,其他情况均返回false。这样,当左右滑动的时候,由于onintercepttouchevent返回了true,父控件就能处理,其他情况,事件将传递到listview中,listview自身可以处理上下滑动。

@override 
public boolean onintercepttouchevent(motionevent ev)  
{ 
  log.d(tag, "onintercepttouchevent-slop:"+mtouchslop); 
 
  final int action = ev.getaction(); 
  if ((action == motionevent.action_move) && (mtouchstate != touch_state_rest)) 
  { 
    return true; 
  } 
 
  final float x = ev.getx(); 
  final float y = ev.gety(); 
 
  switch (action) 
  { 
  case motionevent.action_move: 
    final int xdiff = (int)math.abs(mlastmotionx-x); 
    if (xdiff>mtouchslop) 
    { 
      mtouchstate = touch_state_scrolling; 
    } 
    break; 
 
  case motionevent.action_down: 
    mlastmotionx = x; 
    mlastmotiony = y; 
    mtouchstate = mscroller.isfinished()? touch_state_rest : touch_state_scrolling; 
    break; 
 
  case motionevent.action_cancel: 
  case motionevent.action_up: 
    mtouchstate = touch_state_rest; 
    break; 
  } 
 
  return mtouchstate != touch_state_rest; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网