2014-06-26 139 views
0

我有4個選項卡實現的ActionBar。每個標籤加載一個片段。在每個片段中,我都有一個帶有藍牙標誌的imageView,當藍牙連接時,此標誌應該閃爍。我有這樣的方法對所有的碎片以這種方式實現:知道選擇了哪個選項卡

public void makeBluetoothBlink() { 
    btIcon.setImageResource(R.drawable.bluetooth_blink); 
    AnimationDrawable frameAnimation = (AnimationDrawable) btIcon.getDrawable(); 
    frameAnimation.start(); 
} 

現在,在活動中,我有藍牙狀態,當它連接子監聽器,它應該檢測哪些片段是可見的。其他可能性是檢測選擇哪個選項卡。展位選擇應該完成這項工作。

我的方法是爲每個片段定義一個標籤,然後檢查哪一個是可見的,並調用該方法使藍牙標誌閃爍。但它不能識別片段中的方法。所以這就是爲什麼我想知道檢測哪個選項卡可能更容易,但我不確定,我不知道該怎麼做。

public void onConnecting() { 
    //Call fragment blink method 
    Fragment current_fragment = getSupportFragmentManager().findFragmentByTag(tab1); 
    if (current_fragment.isVisible()) { 
     current_fragment.makeBluetoothBlink(); 
    } 
} 

回答

0

Android Development docs

public abstract ActionBar.Tab getSelectedTab() 

Added in API level 11 
Returns the currently selected tab if in tabbed navigation mode and there is at least one tab present. 

Returns 
The currently selected tab or null 
相關問題