2017-06-16 28 views
0

我有一個Bottombar。當你從那裏點擊一個物品時,一個碎片將填充Bottombar和屏幕頂部之間的空間。當你點擊一個物品時,這個代碼將被運行:做一個完美的導航與Bottombar和片段

case R.id.navigation_search: 
        Fragment_search fragment_search = new Fragment_search(); 
        fragmentTransaction = fragmentManager.beginTransaction(); 
        fragmentTransaction.replace(R.id.fragment_container, fragment_search); 
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit(); 
        return true; 

你看到我添加碎片到BackStack。一切工作正常,但是當用戶點擊Android的後退按鈕時,只有先前的片段纔會加載,Bottombar將選擇片段所具有的項目。那麼如何做到這一點,當一個片段被稱爲適當的項目將被選中?有沒有像onFragmentChange?以及如何從Bottombar中選擇物品?

在此先感謝。

回答

1

如果您想要後退按鈕更改底部欄,您可以攔截它並告訴它要選擇哪個項目。

@Override 
public void onBackPressed() 
{ 
    super.onBackPressed(); 

    // Figure out which fragment is now showing and select the item in the bottom bar. 
} 

如果您有一個actibar /工具欄,您可能還需要攔截後退箭頭。

+0

是的,我也有過這個想法,但是如何獲得當前的碎片? – user7938448

+0

你應該能夠做這樣的事情: '''片段currentFragment = getFragmentManager()findFragmentById(R.id.fragment_container);''' 或 '''片段currentFragment = getSupportFragmentManager( ).findFragmentById(R.id.fragment_container);''' 然後查看哪些片段經由的instanceof示出,以確定選擇哪個項目, '''如果(currentFragment的instanceof YourFragment){ Log.v。 (TAG,「你的片段是可見的」); }''' –

+0

但現在是不同的部分。如何從Bottombar中選擇物品? – user7938448