2bab
V2EX  ›  问与答

关于 Android 监听某个 View 之外的触摸事件关闭该 View

  •  
  •   2bab · Sep 18, 2014 · 4744 views
    This topic created in 4279 days ago, the information mentioned may be changed or developed.
    问题背景:一个Fragment,分三部分,头部为一个RelativeLayout,底部是一个LinearLayout。中间是listView。现,listItem上有一Button,点击则覆盖一个新layout到该item上(即显示更多操作的layout,该layout的宽高就是listItem的宽高)。

    问题:如何做到触摸页面上除了这个新的layout的其他地方,都把新layout关闭。

    目前的办法和bug:用popupWindow实现这个覆盖的layout,但是当某个listItem没有完全显示时,比如在与头部交接的地方,点击按钮,弹出的popupWindow会把头部的一部分盖住。

    尝试过的:直接在listItem里添加View,但是在Root ViewGroup 监听touch事件不知道怎么捕捉当前view以外的touch事件(比如用位置判断,感觉要对Event的判断异常麻烦)。也尝试过FocusChange的判断,但是新的layout requestFocus后点击其他地方都没有获得focus,虽然设了focusable和focusableInTouchMode,而且成本太高。最后还打算尝试在整个Fragment上盖一层alpha touch layer,然后用位置判断,不知道可不可行。

    希望朋友们支点招。
    1 replies    2015-09-07 15:30:15 +08:00
    veightz
        1
    veightz  
       Sep 7, 2015
    iOS 倒是蛮好做的+_=
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   867 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:37 · PVG 05:37 · LAX 14:37 · JFK 17:37
    ♥ Do have faith in what you're doing.