2016-08-14 40 views
1

我自己試圖開發基於this tutorial底部欄的示例安卓應用程序。安卓底部欄設置默認選項卡上創建的活動

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.thirdactivity); 

     BottomBar bottomBar = BottomBar.attach(this, savedInstanceState); 
     bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() { 
      @Override 
      public void onMenuItemSelected(int itemId) { 
       Intent myAct = new Intent(); 
      switch (itemId) { 
       case R.id.item1: 
        myAct = new Intent(findViewById(itemId).getContext(), mainactivity.class); 
        break; 
       case R.id.item2: 
        myAct = new Intent(findViewById(itemId).getContext(), secondactivity.class); 
        break; 
       case R.id.item3: 
        myAct = new Intent(findViewById(itemId).getContext(), thirdactivity.class); 
        break; 
      } 
       startActivity(myAct); 
      } 
     }); 
    } 

但是,我如何設置第三個選項卡作爲默認oncreate活動。上面的代碼突出顯示了第一個選項卡爲選中狀態,並且在第一個選項卡上單擊時沒有收聽。稍後的選項卡也會打開相應的活動,但不會將其作爲當前選項卡高亮顯示

編輯:我自己可以使用bottomBar.setDefaultTabPosition(desiredTabId);和它的工作,但它使用高內存。什麼是修復?

回答

0

它是由具有作爲startActivity()解決,

BottomBar bottomBar = BottomBar.attach(this, savedInstanceState); 

     bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() { 
       @Override 
       public void onMenuItemSelected(int itemId) { 
        Intent act = new Intent(); 
        if (R.id.item1 == itemId) { 
         act = new Intent(findViewById(itemId).getContext(), act1.class); 
         startActivity(act); 
         overridePendingTransition(R.anim.open_translate, R.anim.close_scale); 
        } 
        if (R.id.item2 == itemId) { 
         act = new Intent(findViewById(itemId).getContext(), act2.class); 
        } 
        if (R.id.item3 == itemId) { 
         act = new Intent(findViewById(itemId).getContext(), act3.class); 
         startActivity(act); 
         overridePendingTransition(R.anim.open_translate, R.anim.close_scale); 
        } 

       } 

      }); 
      bottomBar.setDefaultTabPosition(2); 

      bottomBar.setActiveTabColor("#F3C030");