【问题标题】:how to make header of listview not to consume the touch event如何使列表视图的标题不消耗触摸事件
【发布时间】:2016-08-03 06:28:59
【问题描述】:

我想要什么

我希望 listView 的 header 不消耗触摸事件

问题

我有一个 FrameLayout。其中有一个 mapview 和一个 listview。列表视图有一个透明的标题视图,以便显示更多的地图视图。现在标题视图响应触摸事件,但我想将这些事件分派到地图视图。怎么办?

我试过了

  • headerView.setEnabled(false)
  • headerView.setClickable(false)
  • mListView.addHeaderView(headerView, null, false)

都失败了,有什么建议吗?

【问题讨论】:

  • 我能想到的防止标题消耗触摸事件的唯一方法是将其可见性设置为不可见。
  • 想你@Anders。我尝试了您的解决方案,但不起作用...

标签: android listview touch-event


【解决方案1】:

我在SO中尝试了很多解决方案,但都不起作用。最后,我重写了dispatchTouchEvent()函数,它起作用了。完整版本结束了gist ,关键代码在这里。

@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
    if(mHeaderView == null) return super.dispatchTouchEvent(motionEvent);
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
        //if touch header not to consume the event
        Rect rect = new Rect((int) mHeaderView.getX(), (int) mHeaderView.getY(), mHeaderView.getRight(), mHeaderView.getBottom());
        if(rect.contains((int)motionEvent.getX(), (int)motionEvent.getY())){
            isDownEventConsumed = false;
            return isDownEventConsumed;
        }else {
            isDownEventConsumed = true;
            return super.dispatchTouchEvent(motionEvent);
        }
    }else{
        //if touch event not consumed, then move/up event should be the same
        if(!isDownEventConsumed)return isDownEventConsumed;
        return super.dispatchTouchEvent(motionEvent);
    }
}

【讨论】:

    【解决方案2】:

    您是否尝试在扩展标题视图或 this 时将 onClickListener 设置为 null?

    【讨论】:

    • 我试过了,但是不行。最后我重写了dispatchTouchEvent()函数,它可以工作了~
    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    相关资源
    最近更新 更多