Vo7ice
V2EX  ›  Android

listview 与 gridview 嵌套使用时响应事件冲突

  •  
  •   Vo7ice · Oct 27, 2015 · 13694 views
    This topic created in 3855 days ago, the information mentioned may be changed or developed.
    ListView 与 GridView 嵌套使用的时候 gridview 放置几张图片 要求用户点按图片的时候响应的为 gridview 的 ItemLongClick 事件 而不是 listview 的 ItemLongClick 事件。请问,应该如何解决这两个事件冲突的问题?
    8 replies    2015-10-29 18:30:10 +08:00
    gengrui
        1
    gengrui  
       Oct 27, 2015
    其实我没太明白问题,只是试着回答一下。如果想屏蔽其中一个 callback ,把 listview 的 itemLongClick 给 set 成 null 可以么?
    yishanxin
        2
    yishanxin  
       Oct 27, 2015 via Android
    子控件焦点问题!
    Vo7ice
        3
    Vo7ice  
    OP
       Oct 27, 2015
    @yishanxin 我已经通过事件改变传递的方式分开了 itemclick 事件了,但在 itemLongClick 事件中点击 gridview 中的图片时先响应的是整个 listview 的 itemlongclick 事件。不知道为什么
    Vo7ice
        4
    Vo7ice  
    OP
       Oct 27, 2015
    @gengrui 应该不行 设置监听就是个回调的过程
    yishanxin
        5
    yishanxin  
       Oct 27, 2015
    @Vo7ice 就按照正常事件监听,焦点问题 android:descendantFocusability
    Vo7ice
        6
    Vo7ice  
    OP
       Oct 27, 2015
    @yishanxin 我解决了 按事件传递的逻辑 然后把子控件的 gridview 的 itemLongClick 事件的返回值设置为 true 。让 gridview 消耗掉该次事件 不传递到 listview 上
    zhoulujue
        7
    zhoulujue  
       Oct 28, 2015
    这个正好不久前我也遇到过,不过 我是 GridView 的 Click 事件收不到。其实是嵌套的 ViewGroup 情况下, Touch Event 这个责任链 Android 似乎处理的不太好。

    我的解决方法是,重载 GridView Item 的 onTouch 事件,使用 GestureDetector 处理这个 onTouch event ,然后直接 return true , consume 掉这个事件;在 GestureDetector 里的 onSingleTapUp 里再作 onClick 应该做的事情,同时不影响 GridView 以外的其他控件 touch event 事件,也不会影响 ListView 的 OnItemClick 。
    kaedea
        8
    kaedea  
       Oct 29, 2015
    不要把两个 ScrollView 嵌套使用……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3113 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 13:06 · PVG 21:06 · LAX 06:06 · JFK 09:06
    ♥ Do have faith in what you're doing.