Remove selected item bottombar (Lib Roughike)

Asked

Viewed 60 times

1

I have an application that uses Navigationview and Bottombar, I’m having a hard time removing the bootombar item selection when selecting an item from navigationView, I’m using the Roughike Library https://github.com/roughike/BottomBar.

I tried to use:

// Remove item selected BottomBar *** Problem here ***
    public void rmSelectBottom() {
        bottomBar.getCurrentTab().clearFocus();
        bottomBar.getCurrentTab().setSelected(false);
    }

But I didn’t succeed

Complete code

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        bottomBar = (BottomBar) findViewById(R.id.bottom_navigation_view);

        toolbar.setTitle("Title");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        navigationView.setNavigationItemSelectedListener(this);

        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        appBarLayout.setExpanded(false, true);


        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            }
        });

//BottomBar
        bottomBar.setDefaultTabPosition(0);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {

                Fragment fragment = null;
                Class fragmentClass = null;

                rmSelectNavigation();

                switch (tabId) {
                    case R.id.tab_home:
                        fragmentClass = TabHomeFragment.class;
                        break;
                    case R.id.tab_notas:
                        fragmentClass = TabGradesFragment.class;
                        break;
                    case R.id.tab_aval:
                        fragmentClass = TabAvaliationsFragment.class;
                        break;
                    case R.id.tab_faltas:
                        fragmentClass = TabAbsencesFragment.class;
                        break;
                }

                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            }
        });

    }

// NavigationView
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment = null;
        Class fragmentClass = null;

        rmSelectBottom();

        if (id == R.id.nav_notification) {
            fragmentClass = NavIncidentsFragment.class;
        }
        if (id == R.id.nav_task) {
            fragmentClass = NavTasksFragment.class;
        }
        if (id == R.id.nav_content) {
            fragmentClass = NavContentFragment.class;
        }
        if (id == R.id.nav_financial) {
            fragmentClass = NavFinancialFragment.class;
        }
        if (id == R.id.nav_promotion) {
            fragmentClass = NavPromotionsFragment.class;
        }
        if (id == R.id.nav_config) {
            Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(intent);
        }

        if (id != R.id.nav_config) {
            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

// Remove item selected NavigationView
    public void rmSelectNavigation() {
        int size = navigationView.getMenu().size();
        for (int i = 0; i < size; i++) {
            navigationView.getMenu().getItem(i).setChecked(false);
        }
    }
// Remove item selected BottomBar *** Problem here ***
    public void rmSelectBottom() {
        bottomBar.getCurrentTab().clearFocus();
        bottomBar.getCurrentTab().setSelected(false);
    }

Is there any other specific method to remove the selected item?

Follow the image with the example of the problem:

Na imagem eu seleciono o item **TAREFAS** do NavigationView e o item **NOTAS** no BottomBar manteve selecionado.

  • Kowal, I’m pretty sure it’s not possible to remove the selection of a tab using that lib, I don’t think it’s a use case if you think about it. Following the specs of Material Design, there is no mention of Bottom Bar without the element not selected. Might not be better to take Bottom Bar out of Navigationview screens that have no items in Bottombar?

  • In the Google+ App there is a good example, if you click on the Navigationview Events item it will keep the bottombar and all items will be unchecked.

  • Yeah, you’re really right, I saw it here. My suggestion is to try the BottomNavigationView which is built into the Design Support Library, I think from version 25. As I’ve never tried, I’m not sure if it provides an API for this.

1 answer

1


After much searching without finding a solution to the problem with the Library of the Roughike, I decided to follow @Wakim’s suggestion and used Bottomnavigationview, which is in Design Support Library 25.

Solution:
When I click on a Navigationview item I call a method that traverses the Bottombarnavigationview and a setChecked(false)

int size = bottomBar.getMenu().size();
for (int i = 0; i < size; i++) {
    bottomBar.getMenu().getItem(i).setChecked(false);
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.