当前位置: 移动技术网 > IT编程>移动开发>Android > Android ListView的item中嵌套ScrollView的解决办法

Android ListView的item中嵌套ScrollView的解决办法

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

千广网,ca119航班,邓秋婷

 前沿:有时候,listview 的item要显示的字段比较多,考虑到显示问题,item外面不得不嵌套scrollview来实现,于是问题来了,当listview需要做点击事件时,由于scrollview的嵌套使用,拦截了listvew点击事件:只好重写listview来实现了。

/**
* 
* @author 作者:易皇星
* 
* @da2016年10月24日 时间:
* 
* @totodo 类描述: 解决 listview中嵌套scrollview,scrollview拦截listview的item点击事件的解决办法
* 
* 
* 在listview中嵌套scrollview,发现横滑竖滑都正常,但是无法单击listview的item。查询android分发机制后解决,继承listview重写listview的onintercepttouchevent。
* 
* onintercepttouchevent中总是调用listview的ontouchevent保证listview的事件都执行,
* super.onintercepttouchevent(ev)不会拦截需要传递给scrollview的横滑。
*/
public class mylistview extends listview {
private int flag = 0;
private float startx;
private float starty;
public mylistview(context context) {
super(context);
// todo auto-generated constructor stub
}
public mylistview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
// todo auto-generated constructor stub
}
public mylistview(context context, attributeset attrs) {
super(context, attrs);
// todo auto-generated constructor stub
}
@override
public boolean onintercepttouchevent(motionevent ev) {
// 总是调用listview的touch事件处理
ontouchevent(ev);
if (ev.getaction() == motionevent.action_down) {
startx = ev.getx();
starty = ev.gety();
return false;
}
if (ev.getaction() == motionevent.action_move) {
float scollx = ev.getx() - startx;
float scolly = ev.gety() - starty;
// 判断是横滑还是竖滑,竖滑的话拦截move事件和up事件(不拦截会由于listview和scrollview同时执行滑动卡顿)
if (math.abs(scollx) < math.abs(scolly)) {
flag = 1;
return true;
}
return false;
}
if (ev.getaction() == motionevent.action_up) {
if (flag == 1) {
return true;
}
return false;
}
return super.onintercepttouchevent(ev);
}
}

以上所述是小编给大家介绍的android listview的item中嵌套scrollview的解决办法,希望对大家有所帮助

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

相关文章:

验证码:
移动技术网