我有底部导航和导航抽屉,并且将主屏幕片段设置为默认屏幕

时间:2019-04-25 17:14:48

标签: java android xml bottomnavigationview drawerlayout

我有底部导航和导航抽屉,并且将主屏幕片段设置为默认屏幕

现在的问题是,当我在模拟器上运行我的应用程序时,汉堡包图标和底部导航显示,并且默认情况下显示首页片段

当我单击汉堡包图标时,它的不起作用

当我删除默认主页片段时,导航抽屉正在工作

如何解决此问题因此NavigationDrawer和底部导航都将起作用

HomeScreenActivity.java

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

ActivityHomeScreen.xml

public class HomeScreenActivity extends AppCompatActivity {

    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mToggle;
    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);



        bottomNavigationView = findViewById(R.id.homeScreenBottomNavigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(bottomNavListener);
     //   getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, new HomeFragment()).commit();


        mDrawerLayout = findViewById(R.id.HomeScreenDrawerLayout);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mToggle.onOptionsItemSelected(item)) {
            return true;

        }

        return super.onOptionsItemSelected(item);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener bottomNavListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            Fragment selectedFragment = null;

            switch (menuItem.getItemId()) {
                case R.id.bottomNavHome:
                    selectedFragment = new HomeFragment();
                    break;

                case R.id.bottomNavAdd:
                    selectedFragment = new AddFragment();
                    break;

                case R.id.bottomNavSearch:
                    selectedFragment = new SearchFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, selectedFragment).commit();
            return true;


        }
    };
}

1 个答案:

答案 0 :(得分:0)

问题出在您的ActivityHomeScreen.xml上。您必须使DrawerLayout作为父视图。在其中必须放置BottomNavigationDrawer和其余代码。

您还没有为导航抽屉创建自定义应用栏(它现在带有导航抽屉模板) 您的ActivityHomeScreen.xml应该如下所示:

ActivityHomeScree.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_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

现在创建一个布局文件app_bar_main.xml。在其中创建一个工具栏,并将您的frameLayout放在此处。

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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=".MainActivity">

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

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

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

    <!--
    Place your frameLayout here
    -->
    
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        app:menu="@menu/navigation" />

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

现在Navdrawer和BottomNavigationDrawer都可以使用。