Android将导航汉堡图标切换到后退图标

时间:2018-08-13 12:06:32

标签: android navigation-drawer

我已经从本教程中实现了导航抽屉:https://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

这里的问题是如何更改片段中的ActionBar,因此当我转到片段时,它将向我显示后退箭头而不是ActionBar上的汉堡图标,并且单击“后退箭头”图标时,它将离开片段并且ActionBar应该回到第一个状态。

1 个答案:

答案 0 :(得分:3)

打开片段时需要更改Drawer Toggle的图标。当用户单击片段中的“后退”按钮时,弹出堆栈并将支持操作栏指示器重置为汉堡包。您可能需要覆盖

  

onSupportNavigateUp()

如果“后退”按钮不起作用,则为您的活动方法

 @Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

祝你好运。

编辑

这是实现所需行为的示例代码。

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private String[] mNavigationDrawerItemTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    Toolbar toolbar;
    private CharSequence mTitle;
    ActionBarDrawerToggle mDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = getTitle();
        mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mDrawerList = findViewById(R.id.left_drawer);

        setupToolbar();

        DataModel[] drawerItem = new DataModel[2];

        drawerItem[0] = new DataModel(R.drawable.ic_menu_camera, "Connect");
        drawerItem[1] = new DataModel(R.drawable.ic_menu_gallery, "Fixtures");

        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mDrawerLayout =  findViewById(R.id.drawer_layout);
        setupDrawerToggle();
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }

    }

    private void selectItem(int position) {

        Fragment fragment = null;

        switch (position) {
            case 0:
                fragment = new TestFragment();
                break;
            case 1:
                fragment = new TestFragment2();
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(fragment.getTag()).commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavigationDrawerItemTitles[position]);
            showBackButton(true);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }

    void setupToolbar(){
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    void setupDrawerToggle(){
        mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name, R.string.app_name);
        //This is necessary to change the icon of the Drawer Toggle upon state change.
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mDrawerToggle.isDrawerIndicatorEnabled()){
                   mDrawerLayout.openDrawer(GravityCompat.START);
                }else{
                    onBackPressed();
                }
            }
        });
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
    }

    public void showBackButton(boolean isBack){
        mDrawerToggle.setDrawerIndicatorEnabled(!isBack);
        getSupportActionBar().setDisplayHomeAsUpEnabled(isBack);
        mDrawerToggle.syncState();
    }

    @Override
    public void onBackPressed() {
        if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }else{
            FragmentManager fm = getSupportFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                fm.popBackStack();
            } else {
                super.onBackPressed();
            }
            showBackButton(false);
            setTitle(getTitle());
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}

检查onBackPressed()showBackButton()setupDrawerToggle方法。