2016-10-01 189 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