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

2024-04-28

//这是我的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;
    }
}

// 这是我的 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>

我想在同一个活动中创建导航抽屉和底部导航。我创建XML现在设计我必须为两者编写java代码。底部导航工作正常,但导航项clicklistener不管用。我为导航抽屉和底部导航创建了片段。当我单击导航抽屉中的项目时,它不会重定向到相应的片段。给定的clicklistener导航抽屉根本不起作用。


解决方案:

请尝试执行以下步骤以使其正常工作:

Step1:实施onNavigationItemSelected如下所示:

.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

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:在你的类中声明一个全局变量:

public NavigationView navigationView;

Step4:在你的onCreate()将变量初始化为:

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

Finally:设置导航视图:

navigationView.setNavigationItemSelectedListener(this);

就是这样。

希望它有效。

抽屉布局 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

<?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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • Scala(或 Java)中泛型函数的特化

    是否可以在 Scala 中专门化泛型函数 或类 例如 我想编写一个将数据写入 ByteBuffer 的通用函数 def writeData T buffer ByteBuffer data T buffer put data 但由于 put
  • 如何将txt文件添加到你的android项目中? [复制]

    这个问题在这里已经有答案了 我的Android studio版本是1 5 1 显然这个 never 版本没有 txt 文件的 asset 文件夹 您打算如何将这些文件包含到您的项目中 以及如何进一步使用您内部的应用程序 谢谢你的建议 Pro
  • Java 中 static 关键字如何工作?

    我正在阅读Java教程 http docs oracle com javase tutorial index html从一开始我就有一个问题static字段或变量上的关键字 作为Java said here http docs oracle
  • 尝试在空对象引用上调用虚拟方法“java.lang.String org.jsoup.nodes.Element.ownText()”

    我正在使用下面的代码来获取版本名称 from 应用商店通过使用 jsoup 我正在获取详细信息 但它引发了一些异常 我的代码是 public class ForceUpdateAsync extends AsyncTask
  • 更改 RowLayout SWT Java 中元素的顺序

    有没有办法更改在行布局中创建的元素的顺序 我想将其显示在元素中 首先显示 例如 如果我创建 element1 则 element2 element3 element4 我想看到的布局为 元素4 元素3 元素2 元素1 这意味着最后创建的元素
  • Java 类:匿名类、嵌套类、私有类

    有人能解释一下Java中匿名类 嵌套类和私有类之间的区别吗 我想知道与每个相关的运行时成本以及每个编译器的方法 这样我就可以掌握哪个最适合用于例如性能 编译器优化的潜力 内存使用以及其他 Java 编码人员的普遍可接受性 我所说的匿名类是指
  • NoSuchMethodError:将 Firebase 与应用程序引擎应用程序集成时

    我试图将 firebase 实时数据库与谷歌应用程序引擎应用程序集成 我在调用时收到此错误 gt DatabaseReference ref FirebaseDatabase gt getInstance gt getReference t
  • 删除 ArrayList 对象问题

    我在处理作业时遇到从 ArrayList 中删除对象的问题 如果我使用 正常 for 循环 它的工作原理如下 public void returnBook String isbn for int i 0 i lt booksBorrowed
  • 膨胀类片段 InflateException 二进制 XML 文件时出错

    我正在使用 Material Design 和 NavigationDrawer 布局等设计我的第一个应用程序 但我遇到了一个问题 该应用程序非常简单 它只显示文本 并且基于 Android Studio 中提供的模板 尝试启动我的应用程序
  • Hibernate @OneToMany 注释到底是如何工作的?

    我对 Hibernate 还很陌生 我正在通过教程学习它 我在理解到底如何一对多注释作品 所以我有这两个实体类 Student代表一个学生并且Guide代表指导学生的人 因此 每个学生都与一名向导相关联 但一名向导可以跟随多个学生 我想要一
  • 三星 tab2 平板电脑的 Android 开发

    是否可以使用 Eclipse 在 Samsung Tab 2 平板电脑中开发 Android 应用程序 需要安装什么驱动吗 当然可以 你必须安装标准的android开发工具 SDK ADT ad eclipse Juno最新版本 从这里下载
  • Android 和 iPhone 应用程序可以使用同一个 Facebook 应用程序 ID 吗?

    我有两个具有相同名称和相同功能的应用程序 一款在安卓市场 一款在应用商店 目前仅通过 iPhone 应用程序 您可以使用我创建的 Facebook 应用程序将您的分数发布到 Facebook 墙上 我的问题是我可以使用相同的 Android
  • Listview里面只有一个Element

    您好 我正在尝试将列表视图放入列表视图中的列表视图中 唯一的问题是只有第一个列表视图正确显示所有元素 此后的每个列表视图仅包含一个元素 UPDATE 创建我自己的不可滚动列表视图解决了这个问题 https stackoverflow com
  • 在运行时用Dagger添加Retrofit RequestInterceptor

    我正在使用匕首和改装 我用 Dagger 注入我的 Retrofit 服务 现在我想做一个授权请求来获取 accessToken 之后 我想使用请求拦截器来增强我的 api 模块 以便将此访问令牌用于将来的请求 我的想法是在收到访问令牌后使
  • JPA - 非主键字段上的 @OneToOne 关系不起作用

    我有一个 Spring Data JPA 后端 使用 Hibernate 作为 ORM 实现 这是模型 Person MailConfig id PK uid PK FK Person uid uid Entity
  • 将视图放置在 ConstraintLayout 之外

    我想将视图放置在ConstraintLayout用滑动动画来制作它们的动画 我尝试过设置像这样的约束constraintBottom toTopOf parent 但是View留在容器内 请注意 我希望通过使用内置动画的约束来实现此目的 而
  • 条件查询:按计数排序

    我正在尝试执行一个标准查询 该查询返回 stackoverflow 中回答最多的问题 例如常见问题解答 一个问题包含多个答案 我正在尝试使用标准查询返回按每个问题的答案数排序的回答最多的问题 任何人都知道我应该在 hibernate cri
  • 编译时在代码中替换Java静态最终值?

    在java中 假设我有以下内容 fileA java class A public static final int SIZE 100 然后在另一个文件中我使用这个值 fileB java import A class b Object t
  • 有什么方法可以禁用/覆盖 Galaxy Tab 4 上的多任务按钮吗? [复制]

    这个问题在这里已经有答案了 我们编写了一个工业控制应用程序 并随我们的制造产品预装在三星 Galaxy 平板电脑上 我们使用的平板电脑是运行 Honeycomb 的 Tab 10 但我们无法再获得足够的 OEM 平板电脑 因此我们改用运行
  • 首选项和操作栏中的开/关切换按钮 - 冰淇淋三明治风格

    我指的是 ICS 手机上默认 Android 设置应用程序中看到的蓝色开 关样式 也可以在这里看到 http android developers blogspot com 2012 02 android design v2 now wit

随机推荐