2016-10-01 192 views
0

我正在開發一個應用程序,其中包含底部導航抽屜,直到現在,每件事情都很好,但是當我添加點擊偵聽器時,我正在執行的操作不起作用。如何從底部導航抽屜啓動活動?

bottombar_tabs.xml

<?xml version="1.0" encoding="utf-8"?> 
<tabs xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 

    <tab 
     android:layout_height="50dp" 
     android:id="@+id/tab_favorites" 
     android:icon="@mipmap/ic_map_black_24dp" 
     android:title="Land" /> 

    <tab 
     android:layout_height="50dp" 
     android:id="@+id/tab_nearby" 
     android:icon="@mipmap/ic_directions" 
     android:title="Vehicle" /> 

    <tab 
     android:layout_height="50dp" 
     android:id="@+id/tab_friends" 
     android:icon="@mipmap/ic_inbox_black_24dp" 
     android:title="Equipment" /> 

</tabs> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_marginTop="?attr/actionBarSize" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/fishPriceList" 
     android:layout_marginBottom="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="10dp" 
     android:layout_weight="1"/> 

    <android.support.design.widget.FloatingActionButton 

     android:id="@+id/fab" 
     android:background="#003300" 
     app:backgroundTint="@color/green" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="70dp" 
     android:layout_gravity="bottom|end" 
     android:layout_marginRight="@dimen/fab_margin" 
     android:src="@mipmap/ic_filter_list" /> 


    <com.roughike.bottombar.BottomBar 
     android:id="@+id/bottomBar" 
      android:layout_width="match_parent" 
      android:layout_height="65dp" 
      android:layout_alignParentBottom="true" 
      app:bb_tabXmlResource="@layout/bottombar_tabs" /> 


</RelativeLayout> 

在Java類

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar); 
     bottomBar.setOnTabReselectListener(new OnTabReselectListener() { 
      @Override 
      public void onTabReSelected(@IdRes int tabId) { 

     if (tabId == R.id.tab_nearby){ 
       Toast.makeText(getApplicationContext(),"Working",Toast.LENGTH_LONG).show(); 
      } 

      } 
     }); 
    } 
} 
+0

那麼你實際面臨什麼問題? –

+0

我沒有得到烤麪包,當我在關閉庫時出現 –

+0

試圖使用android原生viewpager標籤親愛的它給了更多的定製 –

回答

-1

嘗試這樣

bottomBar.setOnTabselectListener(new OnTabselectListener() { 
     @Override 
     public void onTabSelected(@IdRes int tabId) { 
      Toast.makeText(this, "Hello How are you?", Toast.LENGTH_LONG).show(); 
     } 
    }); 
+0

請告訴我什麼是TabMessage? –

+0

它的自定義類來顯示消息 –

+0

你只是使用靜態字符串檢查 –

1

我接受的答案不同意,我調試的代碼,並發現了問題:

tabId總是等於一個錯誤值到達1

顯然,問題已經得到identified

enter image description here

SOLUTION:

1 - 刪除您bottombar_tabs.xml

2 - 創建資源XML文件的文件夾的文件夾,外佈局文件夾 enter image description here

3 - 創建一個新的bottombar_tabs.xml WITHOUTandroid:命名空間

<?xml version="1.0" encoding="utf-8"?> 
<tabs> 
    <tab 
     layout_height="50dp" 
     id="@+id/tab_favorites" 
     icon="@android:drawable/ic_menu_save" 
     title="Land" /> 

    <tab 
     layout_height="50dp" 
     id="@+id/tab_nearby" 
     icon="@android:drawable/ic_menu_save" 
     title="Vehicle" /> 

    <tab 
     layout_height="50dp" 
     id="@+id/tab_friends" 
     icon="@android:drawable/ic_menu_save" 
     title="Equipment" /> 
</tabs> 

4 - 在您的activity_main.xml佈局中使用新的XML

<com.roughike.bottombar.BottomBar 
     android:id="@+id/bottomBar" 
     android:layout_width="match_parent" 
     android:layout_height="65dp" 
     android:layout_alignParentBottom="true" 
     app:bb_tabXmlResource="@xml/tabs_final" /> 

瞧,這是完成=)

+0

@Kriti Jain你能解決你的問題嗎?我相信這個答案應該被標記爲正確的,讓我知道這是否有幫助=) – HenriqueMS