Cannot catch toolbar home button click event

前端 未结 11 1954
悲&欢浪女
悲&欢浪女 2020-11-27 02:56

I\'ve implemented the newest appcompat library and using the Toolbar as action bar. But the problem is I cannot catch the home button / hamburger icon click eve

相关标签:
11条回答
  • 2020-11-27 03:21

    For anyone looking for a Xamarin implementation (since events are done differently in C#), I simply created this NavClickHandler class as follows:

    public class NavClickHandler : Java.Lang.Object, View.IOnClickListener
    {
        private Activity mActivity;
        public NavClickHandler(Activity activity)
        {
            this.mActivity = activity;
        }
        public void OnClick(View v)
        {
            DrawerLayout drawer = (DrawerLayout)mActivity.FindViewById(Resource.Id.drawer_layout);
            if (drawer.IsDrawerOpen(GravityCompat.Start))
            {
                drawer.CloseDrawer(GravityCompat.Start);
            }
            else
            {
                drawer.OpenDrawer(GravityCompat.Start);
            }
        }
    }
    

    Then, assigned a custom hamburger menu button like this:

            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDefaultDisplayHomeAsUpEnabled(false);
            this.drawerToggle.DrawerIndicatorEnabled = false;
            this.drawerToggle.SetHomeAsUpIndicator(Resource.Drawable.MenuButton);
    

    And finally, assigned the drawer menu toggler a ToolbarNavigationClickListener of the class type I created earlier:

            this.drawerToggle.ToolbarNavigationClickListener = new NavClickHandler(this);
    

    And then you've got a custom menu button, with click events handled.

    0 讨论(0)
  • 2020-11-27 03:25

    I have handled back and Home button in Navigation Drawer like

    public class HomeActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
        private ActionBarDrawerToggle drawerToggle;
        private DrawerLayout drawerLayout;
        NavigationView navigationView;
        private Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_home);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            resetActionBar();
    
            navigationView = (NavigationView) findViewById(R.id.navigation_view);
            navigationView.setNavigationItemSelectedListener(this);
    
            //showing first fragment on Start
            getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).replace(R.id.content_fragment, new FirstFragment()).commit();
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            //listener for home
            if(id==android.R.id.home)
            {  
                if (getSupportFragmentManager().getBackStackEntryCount() > 0)
                    onBackPressed();
                else
                    drawerLayout.openDrawer(navigationView);
                return  true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onBackPressed() {
           if (drawerLayout.isDrawerOpen(GravityCompat.START)) 
                drawerLayout.closeDrawer(GravityCompat.START);
           else 
                super.onBackPressed();
        }
    
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Begin the transaction
    
            Fragment fragment = null;
            // Handle navigation view item clicks here.
            int id = item.getItemId();
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (id == R.id.nav_companies_list) {
                fragment = new FirstFragment();
                // Handle the action
            } 
    
    
            // Begin the transaction
            if(fragment!=null){
    
                if(item.isChecked()){
                    if(getSupportFragmentManager().getBackStackEntryCount()==0){
                        drawer.closeDrawers();
                }else{
                        removeAllFragments();
                        getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).replace(R.id.WikiCompany, fragment).commit();
                        drawer.closeDrawer(GravityCompat.START);
                    }
    
                }else{
                    removeAllFragments();
                    getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).replace(R.id.WikiCompany, fragment).commit();
                    drawer.closeDrawer(GravityCompat.START);
                }
            }
    
            return true;
        }
    
        public void removeAllFragments(){
            getSupportFragmentManager().popBackStackImmediate(null,
                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    
        public void replaceFragment(final Fragment fragment) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.WikiCompany, fragment).addToBackStack("")
                    .commit();
        }
    
    
        public void updateDrawerIcon() {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        Log.i("", "BackStackCount: " + getSupportFragmentManager().getBackStackEntryCount());
                        if (getSupportFragmentManager().getBackStackEntryCount() > 0)
                            drawerToggle.setDrawerIndicatorEnabled(false);
                        else
                            drawerToggle.setDrawerIndicatorEnabled(true);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }, 50);
        }
    
        public void resetActionBar()
        {
            //display home
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    
        public void setActionBarTitle(String title) {
            getSupportActionBar().setTitle(title);
        }
    }
    

    and In each onViewCreated I call

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((HomeActivity)getActivity()).updateDrawerIcon();
        ((HomeActivity) getActivity()).setActionBarTitle("List");
    }
    
    0 讨论(0)
  • 2020-11-27 03:30

    I think the correct solution with support library 21 is the following

    // action_bar is def resource of appcompat; 
    // if you have not provided your own toolbar I mean  
    Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
    if (toolbar != null) {
        // change home icon if you wish
        toolbar.setLogo(this.getResValues().homeIconDrawable());
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //catch here title and home icon click                          
            }                       
        });                 
    }
    
    0 讨论(0)
  • 2020-11-27 03:31

    Try this code

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == android.R.id.home){
            //You can get 
        }
        return super.onOptionsItemSelected(item);
    }
    

    Add below code to your onCreate() metod

    ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 2020-11-27 03:36

    This is how I implemented it pre-material design and it seems to still work now I've switched to the new Toolbar. In my case I want to log the user in if they attempt to open the side nav while logged out, (and catch the event so the side nav won't open). In your case you could not return true;.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (!isLoggedIn() && item.getItemId() == android.R.id.home) {
            login();
            return true;
        }
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
提交回复
热议问题