简体   繁体   English

如何在同一活动中为底部导航和导航抽屉创建侦听器?

[英]How to create a listener for bottom navigation and navigation drawer in the same activity?

//Here is my java code //这是我的java代码

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    BottomNavigationView bottomNavigationView;
    NavigationView navigationView;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    HomeFragment homeFragment=new HomeFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
                    return true;

                case R.id.navigation_stylist:
                    StylistFragment stylistsFragment=new StylistFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
                    return true;

                case R.id.navigation_apps:
                    MyapptsFragment myaaptsFragment=new MyapptsFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
                    return true;

                case R.id.navigation_tips:
                    HairtipsFragment hairtipsFragment=new HairtipsFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
                    return true;

                case R.id.navigation_account:
                    AccountFragment accountFragment=new AccountFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();

                    return true;
            }
            return false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //start onboarding when app is opening first time
        isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(HomeActivity.this, PREF_USER_FIRST_TIME, "true"));
        Intent introIntent = new Intent(HomeActivity.this, OnboardingActivity.class);
        introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);

        if (isUserFirstTime)
            startActivity(introIntent);

        setContentView(R.layout.activity_home);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        navigationView=findViewById(R.id.nav_drawer);


        //bottom navigationview listener
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

        //navigation drawer listener
        navigationView.setNavigationItemSelectedListener(this);


        //open home fragment on first launch
        HomeFragment homeFragment=new HomeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();

    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_home:
                HomeFragment homeFragment=new HomeFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
                return true;

            case R.id.nav_products:
                StylistFragment stylistsFragment=new StylistFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
                fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
                return true;

            case R.id.nav_promotions:
                MyapptsFragment myaaptsFragment=new MyapptsFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
                fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
                return true;

            case R.id.nav_purchases:
                HairtipsFragment hairtipsFragment=new HairtipsFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
                fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
                return true;

            case R.id.nav_settings:
                AccountFragment accountFragment=new AccountFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
                fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
                return true;
        }
        return false;
    }
}

// Here is my xml // 这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:theme="@style/menu_text_style"
        app:menu="@menu/navigation_drawer" />

    <!--app:headerLayout="@layout/nav_header_main"-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/shadow"
            android:animateLayoutChanges="true">

        </FrameLayout>

        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_1sdp"
            android:layout_above="@id/bottom_navigation"
            android:background="@color/shadow"/>


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:itemIconTint="@color/navigationitem"
            app:itemTextColor="@color/navigationitem"
            app:menu="@menu/navigation_item"/>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

I want to create both navigation drawer and bottom navigation in the same activity.我想在同一个活动中创建导航抽屉和底部导航。 I created XML design now I have to write java code for both.我创建了XML设计,现在我必须为两者编写 Java 代码。 Bottom navigation is working perfectly but navigation item clicklistener is not working.底部导航工作正常,但导航项clicklistener器不起作用。 I created fragments for navigation drawer and also for bottom navigation.我为导航抽屉和底部导航创建了片段。 When I click the items in navigation drawer it's not redirecting to respective fragment.当我单击导航抽屉中的项目时,它不会重定向到相应的片段。 The given clicklistener for navigation drawer is not at all working.导航抽屉的给定clicklistener器根本不起作用。

Solution:解决方案:

Try the following steps to get it working:尝试以下步骤以使其正常工作:

Step1: Implement onNavigationItemSelected as shown below: Step1:实现onNavigationItemSelected如下图:

.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

Step2: Use the method generated by the interface as: Step2:使用接口生成的方法为:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.your_drawer_item_id:
            ....
            ....
        case R.id.your_drawer_item_id:
            ....
            ....
    }
}

Step3: In your class declare a Global variable: Step3:在你的类中声明一个全局变量:

public NavigationView navigationView;

Step4: In your onCreate() initialize the variable as:步骤 4:在您的onCreate()中将变量初始化为:

navigationView = (NavigationView) findViewById(R.id.your_nav_view);

Finally: Setup the navigationView:最后:设置导航视图:

navigationView.setNavigationItemSelectedListener(this);

That's it.就是这样。

Hope it works.希望它有效。

DrawerLayout xml:抽屉布局xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_vendor_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_drawer_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/White"
        android:fitsSystemWindows="true"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@color/bottom_color"
        app:itemTextColor="@color/bottom_color"
        app:headerLayout="@layout/nav_header_vendor_drawer_layout"
        app:menu="@menu/vendor_drawer_menu" >

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

app_bar_vendor_drawer_layout xml app_bar_vendor_drawer_layout xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/MyMaterialTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/Project_Standard"
            app:popupTheme="@style/MyMaterialTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_above="@+id/bottom_navigation">

    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@drawable/toolbar_dropshadow_above"
        android:layout_above="@id/bottom_navigation"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/White"
        app:itemIconTint="@color/bottom_color"
        app:itemTextColor="@color/bottom_color"
        app:menu="@menu/vendor_bottom_navigation"/>

</RelativeLayout>

Try it.尝试一下。

You can use on the same activity using a method that recive a MenuItem parameter like this:您可以使用接收 MenuItem 参数的方法在同一活动上使用,如下所示:

private void myClickItem(MenuItem item){
    switch (item.getItemId()) {
        case R.id.bottom_home:
            // DO SOMETHING
            break;

        case R.id.drawer_main:
            // DO SOMETHING
            break;
      }
}

Then, call like this:然后,像这样调用:

BottomNavigationView bottomNavigationView =  findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> { // using lamda
     myClickItem(item); //call here
     return true;
});

NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(item -> {
    myClickItem(item); // call the same method here

    drawerLayout.closeDrawers(); // bonus: hide navigation after click
    return false;
});

You don't need to modify your xml res, it'll work fine.您不需要修改您的 xml res,它会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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