2013-01-15 12 views
3

有了新的面貌的Android ADT(Android開發者工具)有一些新的項目嚮導進一步走了很多比我用來和老Hello World模板我試圖理解他們,但發現真的很難。的Java方法使用的界面相關的Android ADT全屏項目嚮導

我正在查看FullScreen項目嚮導。

enter code here使用此嚮導創建項目時,它將創建一個包含SystemUIHider.java文件的util庫。

這是問題。

我看到:

protected OnVisibilityChangeListener mOnVisibilityChangeListener = sDummyListener; 

其中要求在類文件的底部發現了一個接口:

public interface OnVisibilityChangeListener { 

     public void onVisibilityChange(boolean visible); 
    } 

我的問題是:

1)我以爲接口必須是擴展了這個代碼有什麼實際用途?

2)如果OnVisibilityChangeListener是一個接口而不是嚴格意義上的類,這怎麼可能?

private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() { 
     @Override 
     public void onVisibilityChange(boolean visible) { 
     } 
    }; 

謝謝!

package com.example.test2.util; 

import android.app.Activity; 
import android.os.Build; 
import android.view.View; 

public abstract class SystemUiHider { 
    /** 
    * When this flag is set, the 
    * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN} 
    * flag will be set on older devices, making the status bar "float" on top 
    * of the activity layout. This is most useful when there are no controls at 
    * the top of the activity layout. 
    * <p> 
    * This flag isn't used on newer devices because the <a 
    * href="http://developer.android.com/design/patterns/actionbar.html">action 
    * bar</a>, the most important structural element of an Android app, should 
    * be visible and not obscured by the system UI. 
    */ 
    public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1; 

    /** 
    * When this flag is set, {@link #show()} and {@link #hide()} will toggle 
    * the visibility of the status bar. If there is a navigation bar, show and 
    * hide will toggle low profile mode. 
    */ 
    public static final int FLAG_FULLSCREEN = 0x2; 

    /** 
    * When this flag is set, {@link #show()} and {@link #hide()} will toggle 
    * the visibility of the navigation bar, if it's present on the device and 
    * the device allows hiding it. In cases where the navigation bar is present 
    * but cannot be hidden, show and hide will toggle low profile mode. 
    */ 
    public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4; 

    /** 
    * The activity associated with this UI hider object. 
    */ 
    protected Activity mActivity; 

    /** 
    * The view on which {@link View#setSystemUiVisibility(int)} will be called. 
    */ 
    protected View mAnchorView; 

    /** 
    * The current UI hider flags. 
    * 
    * @see #FLAG_FULLSCREEN 
    * @see #FLAG_HIDE_NAVIGATION 
    * @see #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES 
    */ 
    protected int mFlags; 

    /** 
    * The current visibility callback. 
    */ 
    protected OnVisibilityChangeListener mOnVisibilityChangeListener = sDummyListener; 

    /** 
    * Creates and returns an instance of {@link SystemUiHider} that is 
    * appropriate for this device. The object will be either a 
    * {@link SystemUiHiderBase} or {@link SystemUiHiderHoneycomb} depending on 
    * the device. 
    * 
    * @param activity The activity whose window's system UI should be 
    *   controlled by this class. 
    * @param anchorView The view on which 
    *   {@link View#setSystemUiVisibility(int)} will be called. 
    * @param flags Either 0 or any combination of {@link #FLAG_FULLSCREEN}, 
    *   {@link #FLAG_HIDE_NAVIGATION}, and 
    *   {@link #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES}. 
    */ 
    public static SystemUiHider getInstance(Activity activity, View anchorView, int flags) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      return new SystemUiHiderHoneycomb(activity, anchorView, flags); 
     } else { 
      return new SystemUiHiderBase(activity, anchorView, flags); 
     } 
    } 

    protected SystemUiHider(Activity activity, View anchorView, int flags) { 
     mActivity = activity; 
     mAnchorView = anchorView; 
     mFlags = flags; 
    } 

    /** 
    * Sets up the system UI hider. Should be called from 
    * {@link Activity#onCreate}. 
    */ 
    public abstract void setup(); 

    /** 
    * Returns whether or not the system UI is visible. 
    */ 
    public abstract boolean isVisible(); 

    /** 
    * Hide the system UI. 
    */ 
    public abstract void hide(); 

    /** 
    * Show the system UI. 
    */ 
    public abstract void show(); 

    /** 
    * Toggle the visibility of the system UI. 
    */ 
    public void toggle() { 
     if (isVisible()) { 
      hide(); 
     } else { 
      show(); 
     } 
    } 

    /** 
    * Registers a callback, to be triggered when the system UI visibility 
    * changes. 
    */ 
    public void setOnVisibilityChangeListener(OnVisibilityChangeListener listener) { 
     if (listener == null) { 
      listener = sDummyListener; 
     } 

     mOnVisibilityChangeListener = listener; 
    } 

    /** 
    * A dummy no-op callback for use when there is no other listener set. 
    */ 
    private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() { 
     @Override 
     public void onVisibilityChange(boolean visible) { 
     } 
    }; 

    /** 
    * A callback interface used to listen for system UI visibility changes. 
    */ 
    public interface OnVisibilityChangeListener { 
     /** 
     * Called when the system UI visibility has changed. 
     * 
     * @param visible True if the system UI is visible. 
     */ 
     public void onVisibilityChange(boolean visible); 
    } 
} 

回答

0
private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() { 
     @Override 
     public void onVisibilityChange(boolean visible) { 
     // Implementation here. 
     } 
}; 

上述片斷創建和實例化,通過提供在接口中定義的功能的實現實現OnVisibilityChangeListener匿名類。

匿名類旨在用於需要不會被重用的特定實現的地方。也就是說,如果僅在一個地方實例化上述類,則沒有理由給這個類命名,只需使用匿名類即可。通常情況下,匿名類內嵌實例化都用到哪裏,例如:

protected OnVisibilityChangeListener mOnVisibilityChangeListener = new OnVisibilityChangeListener() { 
     @Override 
     public void onVisibilityChange(boolean visible) { } 
}; 

或者作爲參數傳遞給函數,如下面的代碼片段:

something.setOnVisibilityChangeListener(new OnVisibilityChangeListener() { 
     @Override 
     public void onVisibilityChange(boolean visible) { } 
}); 
+0

謝謝你,讓我的東西看成!是否有可能解釋在哪種情況下或爲什麼這樣設計?例如,如果OnVisibilityChangeListener只是一個內部類,它不會達到同樣的目的嗎? – drlobo

+0

更新了我的答案。是的,它會達到同樣的目的,基本上是匿名類所允許的簡潔。 –