Toolbar hamburger on click listener?

2020-06-30 03:02发布

I'm implementing persistent search view like google play in my android application.here is my code.

public class NavigationDrawerActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private EditText searchView;
    private ActionBarDrawerToggle toggle;
    private DrawerLayout drawer;
    private TextView appName;
    private boolean isNavigationDrawerOpened;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        CoordinatorLayout coordinatorLayout = (CoordinatorLayout)findViewById(R.id.mainLayout);
        coordinatorLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(isNavigationDrawerOpened){
                    hideSearchAndEnableAppName();
                }
                return false;
            }
        });
        searchView = (EditText) findViewById(R.id.searchView);
        appName = (TextView) findViewById(R.id.appName);
        searchView.setVisibility(View.GONE);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(getApplicationContext(),"asdfasdfasdf",Toast.LENGTH_LONG).show();
                return false;
            }
        });
        drawer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "asdfasfasdf", Toast.LENGTH_SHORT).show();
            }
        });
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);


        drawer.setDrawerListener(toggle);
        toggle.syncState();
        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(NavigationDrawerActivity.this,"yayyy mani got it",Toast.LENGTH_LONG).show();
            }
        });

        appName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!isNavigationDrawerOpened) {

                    searchView.setVisibility(View.VISIBLE);
                    searchView.requestFocus();
                    appName.setVisibility(View.GONE);
                    isNavigationDrawerOpened = true;
                    animateDrawerIndicator(true);
                    InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

                }
            }
        });

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

    private void hideSearchAndEnableAppName() {
        animateDrawerIndicator(false);
        isNavigationDrawerOpened = false;
        searchView.setVisibility(View.GONE);
        appName.setVisibility(View.VISIBLE);
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    public void onBackPressed() {

        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }
        else if(isNavigationDrawerOpened){
            hideSearchAndEnableAppName();
        }else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                drawer.openDrawer(GravityCompat.START);
                Toast.makeText(getApplicationContext(),"asdfasdf",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    public void animateDrawerIndicator(boolean shouldAnimate) {
        ValueAnimator anim;
        if(shouldAnimate) {
            anim = ValueAnimator.ofFloat(0, 1);
        } else {
            anim = ValueAnimator.ofFloat(1, 0);
        }
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float slideOffset = (Float) valueAnimator.getAnimatedValue();
                // You should get the drawer layout and
                // toggler from your fragment to make the animation
                toggle
                        .onDrawerSlide(drawer,
                                slideOffset);
            }
        });
        anim.setInterpolator(new DecelerateInterpolator());
        anim.setDuration(500);
        anim.start();
    }
}

Here everything is working fine, but when user has clicked on toolbar edittext(as per my code) i'm changing the hamburger icon to back button. Now if user clicks on back button, it should animate back to hamburger and edittext should be hidden(as per my requirement)

But, for that to happen i need to handle hamburger click. I have tried onClickListners on toggle,actionbartoggle etc.But in vain please help.

2条回答
再贱就再见
2楼-- · 2020-06-30 03:23

Directly calling an ActionBarDrawerToggle's onDrawerSlide() method to animate the toggle is messy. A better option is to set a DrawerArrowDrawable as the toggle's Up indicator, and animate that ourselves instead, enabling and disabling the toggle's drawer indicator as necessary.

First, we declare a DrawerArrowDrawable field in the Activity.

private DrawerArrowDrawable searchToggle;

We then initialize it in the onCreate() method, set it as the Up indicator, and define the navigation OnClickListener.

searchToggle = new DrawerArrowDrawable(this);
toggle.setHomeAsUpIndicator(searchToggle);
toggle.setToolbarNavigationClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            hideSearchAndEnableAppName();
        }
    }
);

In the animateDrawerIndicator() method, we first disable the drawer indicator if we're showing the search EditText. This causes the toggle to revert to the Up indicator, which we'll then animate as necessary, switching back to the drawer indicator once the hiding animation is finished.

public void animateDrawerIndicator(final boolean shouldAnimate) {
    ValueAnimator anim;
    if (shouldAnimate) {
        anim = ValueAnimator.ofFloat(0, 1);
        // Show the Up indicator instead
        // of the drawer indicator
        toggle.setDrawerIndicatorEnabled(false);
    }
    else {
        anim = ValueAnimator.ofFloat(1, 0);
    }
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float slideOffset = (Float) valueAnimator.getAnimatedValue();
                // Animate our Drawable instead of the drawer's
                searchToggle.setProgress(slideOffset);
                // If we're hiding and have finished,
                // re-enable the drawer indicator
                if(!shouldAnimate && slideOffset == 0) {
                    toggle.setDrawerIndicatorEnabled(true);
                }
            }
        });
    anim.setInterpolator(new DecelerateInterpolator());
    anim.setDuration(500);
    anim.start();
}
查看更多
三岁会撩人
3楼-- · 2020-06-30 03:34

When both toolbar and drawer were present.

toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
        mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                }else {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }

            }
        });

This is I used in my app when Change the hamburger icon with other when set icon than on click of that is not working because we need to use:

 mDrawerToggle.setDrawerIndicatorEnabled(false);

But when only toolbar(No Navigation Drawer):

toolbar = (Toolbar) findViewById(R.id.toolbar);

toggle = new ActionBarDrawerToggle(
                this, null, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
        mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                }else {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }

            }
        });
    

Then change the hamburger icon to any other as:

toolbar.setNavigationIcon(R.drawable.ic_dehaze_black_24dp); 


    
查看更多
登录 后发表回答