菜单按钮带有后退图标,但如何处理后退单击呢?

时间:2019-05-29 10:44:10

标签: android navigation

enter image description here

enter image description here

从导航菜单项加载片段时,我不会进入后退图标侦听器。如何识别用户单击2张图片中的后退图标。

我尝试了下面的代码,但是没有用。

我搜索了很多东西,但每个人都说您可以在选定的项目中获得点击事件,但它不起作用。

还有其他方法可以获取此点击事件。

    public class HomeConsumerActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, OnFragmentInteractionListener {

        public static final String TAG = HomeConsumerActivity.class.getSimpleName();

        @BindView(R.id.toolbar)
        Toolbar toolbar;
        @BindView(R.id.drawer_layout)
        DrawerLayout drawer;
        @BindView(R.id.nav_view)
        NavigationView navigationView;


        ActionBarDrawerToggle toggle;
        FragmentActivity activity;

        User user;
        boolean refresh;

        private Menu mMenu;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_consumer);
            ButterKnife.bind(this);      
            setSupportActionBar(toolbar);
            toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                }

                @Override
                public void onDrawerOpened(@NonNull View drawerView) {
                    UIElements();
                }

                @Override
                public void onDrawerClosed(@NonNull View drawerView) {
                }

                @Override
                public void onDrawerStateChanged(int newState) {
                }
            });
            toggle.syncState();
            navigationView.setNavigationItemSelectedListener(this);
            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.menu);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (drawer.isDrawerOpen(GravityCompat.START)) {
                        drawer.closeDrawer(GravityCompat.START);
                    } else {
                        drawer.openDrawer(GravityCompat.START);
                    }
                }
            });


            UIElements();
            ManageFragment.replace(activity, R.id.content_main, new HomeConsumerFragment(), HomeConsumerFragment.TAG);
            updateTitle(getString(R.string.home));
        }

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_sign_out) {
            readyBPrefs.logout(activity, false);
        }
        if (id == R.id.nav_password) {
            ManageFragment.replace(activity, R.id.content_main, new ChangePasswordFragment(), ChangePasswordFragment.TAG);
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }    

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            mMenu = menu;
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.home_consumer, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            //noinspection SimplifiableIfStatement
            Timber.i("Clicked Back");
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                int count = getSupportFragmentManager().getBackStackEntryCount();
                if (count <= 1) {
                    finish();
                } else {
                    ManageFragment.back(activity);
                    Fragment fragment = currentFragment();
                    setTitle(fragment);
                }
            }
        }

        public void setTitle(Fragment fragment) {
            if (fragment instanceof ChangePasswordFragment) {
                txtName.setText(getString(R.string.change_password));
            }
        }

        public Fragment currentFragment() {
            return getSupportFragmentManager().findFragmentById(R.id.content_main);
        }

    public void updateBack(Drawable drawable) {
            getSupportActionBar().setHomeAsUpIndicator(drawable);
        }

        public void showBack(boolean show) {
            if (show) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                getSupportActionBar().setDisplayShowHomeEnabled(false);
            }
        }

        public void showHideMenu(int item, boolean show) {
            MenuItem shareItem = mMenu.findItem(item);
            if (show)
                shareItem.setVisible(true);
            else
                shareItem.setVisible(false);
        }

0 个答案:

没有答案