简体   繁体   中英

How to Start Activity from Bottom Navigation Drawer?

I am developing an application which contains bottom navigation drawer, Till now every thing is working fine but when i am adding click listener, the action which i am performing is not working.

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>

In java class

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();
           }

            }
        });
    }
}

I disagree with the accepted answer, I debugged the code and found the problem:

tabId always arrives with a corrupt value equal to 1.

Apparently the issue has already been identified

在此处输入图片说明

SOLUTION:

1 - Delete your bottombar_tabs.xml

2 - Create a folder folder for XML files in resources, outside of layouts folder 在此处输入图片说明

3 - create a new bottombar_tabs.xml WITHOUT the android: namespace

<?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 - In your activity_main.xml layout use the new 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" />

Voila, it's done =)

Try like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM