How to coordinate a Navigation Drawer with a Buttom Navigation View

倾然丶 夕夏残阳落幕 提交于 2020-08-05 08:06:17

问题


I'm fairly new to android dev. I'm checking the material design library and I've implemented a navigation drawer with a bottom navigation to navigate easily through fragments. Both components work just fine but I don't how to coordinate the navigation with both components. For example when a fragment is toggled on the navigation drawer it changed the layout but the button navigation selected item is not changed with it.

How can I solve this issue and link both components to work with each other and sync the changed selected item?

selected Item in bottom navigation drawer: notification

selected Item in navigation drawer: annonce

This is what my java file looks like and here's link for the whole project on GitHub: https://github.com/mreek/NavigationDrawer

Thank you in advance.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawer;
    private TextView appBarTV;
    private static final String TAG = "MyActivity";

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

        Intent intent= new Intent(this,IntroActivity.class);
        startActivity(intent);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        appBarTV = findViewById(R.id.appbar_text_view);

        ImageButton menuRight = findViewById(R.id.leftRight);
        menuRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }
            }
        });

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.action_item1:
                        selectedFragment = MainFragment.newInstance();
                        break;
                    case R.id.action_item2:
                        selectedFragment = AccountFragment.newInstance();
                        break;
                    case R.id.action_item3:
                        selectedFragment = SellFragment.newInstance();
                        break;
                    case R.id.action_item4:
                        selectedFragment = ChatFragment.newInstance();
                        break;
                    case R.id.action_item5:
                        selectedFragment = NotificationFragment.newInstance();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.f_container, selectedFragment);
                transaction.commit();
                return true;
            }
        });

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.f_container, MainFragment.newInstance());
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        drawer.closeDrawers();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            MainFragment fragment = new MainFragment();
            ft.replace(R.id.f_container, fragment);
            ft.commit();
        } else if (id == R.id.nav_gallery) {
            //appBarTV.setText("Fragment With Tabs");
            ChatFragment fragment = new ChatFragment();
            ft.replace(R.id.f_container, fragment);
            ft.commit();
        } else if (id == R.id.nav_slideshow) {
            AccountFragment fragmentTab = new AccountFragment();
            ft.replace(R.id.f_container, fragmentTab);
            ft.commit();
        } else if (id == R.id.nav_share) {
            Toast.makeText(this, "Partager", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_send) {
            Toast.makeText(this, "Rate 5 stars", Toast.LENGTH_SHORT).show();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
<?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">

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

        <include layout="@layout/app_bar_main" />

        <FrameLayout
            android:id="@+id/f_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="3dp"
            android:background="@color/white"
            app:itemIconTint="@color/blue"
            app:itemTextColor="@color/blue"
            app:menu="@menu/bottom_navigation_items" />

    </RelativeLayout>

    <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"
        app:itemTextColor="@color/black"/>
</android.support.v4.widget.DrawerLayout>

回答1:


try this code:

private void setupNavDrawer(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

             Fragment fragment = null;

             switch (item.getItemId()){
                case R.id.nav_gallery:
                   fragment = new GalleryFragment();
                   break;
                case R.id.nav_share:
                   fragment = new ShareFragment();
                   break;
                default:
                   fragment = new GalleryFragment();
                   break;

            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

           //if checked - set title menu
            item.setChecked(true);
            setTitle(item.getTitle());
            mDrawerLayout.closeDrawers();

            return true;
        }
    }); 
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ............//nav
    setupNavDrawer(navigationView);
}



回答2:


First of all you should avoid recreating fragments when user selected a tab from bottom navigation. You should keep your fragments as private variables:

private DrawerLayout drawer;
private BottomNavigationView bottomNavigationView;
private MainFragment mainFragment = MainFragment.newInstance();
private AccountFragment accountFragment = AccountFragment.newInstance();
private SellFragment sellFragment = SellFragment.newInstance();
private ChatFragment chatFragment = ChatFragment.newInstance();
private NotificationFragment notificationFragment = NotificationFragment.newInstance();

When user selects a tab just swich to that fragment:

bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
    Fragment selectedFragment = null;
    switch (item.getItemId()) {
        case R.id.action_announces:
            selectedFragment = mainFragment;
            break;
        case R.id.action_account:
            selectedFragment = accountFragment;
            break;
        case R.id.action_sell:
            selectedFragment = sellFragment;
            break;
        case R.id.action_chat:
            selectedFragment = chatFragment;
            break;
        case R.id.action_notifications:
            selectedFragment = notificationFragment;
            break;
    }
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (selectedFragment != null) {
        transaction.replace(R.id.f_container, selectedFragment);
        transaction.commit();
    }
    return true;
});

When user selects a tab using navigation menu swich to required fragment:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    drawer.closeDrawers();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    switch (item.getItemId()) {
        case R.id.nav_announces:
            bottomNavigationView.setSelectedItemId(R.id.action_announces);
            ft.replace(R.id.f_container, mainFragment);
            break;
        case R.id.nav_account:
            bottomNavigationView.setSelectedItemId(R.id.action_account);
            ft.replace(R.id.f_container, accountFragment);
            break;
        case R.id.nav_sell:
            bottomNavigationView.setSelectedItemId(R.id.action_sell);
            ft.replace(R.id.f_container, sellFragment);
            break;
        case R.id.nav_chat:
            bottomNavigationView.setSelectedItemId(R.id.action_chat);
            ft.replace(R.id.f_container, chatFragment);
            break;
        case R.id.nav_notifications:
            bottomNavigationView.setSelectedItemId(R.id.action_notifications);
            ft.replace(R.id.f_container, notificationFragment);
            break;
    }

    ft.commit();
    return true;
}

I have forked your repo and migrated it to AndroidX before fixing it on a new branch called androidx. I made a pull request to you. You can accept and try it.



来源:https://stackoverflow.com/questions/55667686/how-to-coordinate-a-navigation-drawer-with-a-buttom-navigation-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!