LoginSignup
0
0

More than 5 years have passed since last update.

getVisibility() より isShown() が良さそうやな!つこたろ!

Last updated at Posted at 2016-09-07

前置き

Viewの表示状態を見て処理を振り分けたいケースがあると思います。
以前はこうしてました↓

java
if(view.getVisibility() == View.VISIBLE){
  // 見えとるやんけ!
}

修正してみた

でもほら、isShown()ってそれっぽい関数があるじゃないですか。
で、そっち使うように修正するじゃないですか。

java
if(view.isShown()){
  // 見えとるのに、ここ通らねぇw ワロリッシュw
}

動かねぇ

ふーむ。isShown()って関数名はそれっぽいんだけど。
ソースコードはこんな感じで動いてくれても良さそうなんだけどなぁ。
parentで先祖遡ってるうちに、どっかでfalse返してるのかな?

java
    /**
     * Returns the visibility of this view and all of its ancestors
     *
     * @return True if this view and all of its ancestors are {@link #VISIBLE}
     */
    public boolean isShown() {
        View current = this;
        //noinspection ConstantConditions
        do {
            if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
                return false;
            }
            ViewParent parent = current.mParent;
            if (parent == null) {
                return false; // We are not attached to the view root
            }
            if (!(parent instanceof View)) {
                return true;
            }
            current = (View) parent;
        } while (current != null);

        return false;
    }

結局

onResume() のタイミングで isShown() するとfalse返すみたいなのよね。
ダメ、絶対。

どうしてもonResume()で判断したい場合は?

getVisivility()でがんばろう

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0