处理和显示工具栏上的向后显示图标,并打开了片段

时间:2019-05-03 09:13:58

标签: android android-fragments navigation-drawer onbackpressed

我正在使用带有片段的导航抽屉,并且在我的主页中以这种方式进行导航==> maincategory ====> subcategory ===> subcategoryDe​​tail

maincategory 移至 subcategory 时,它完美替代了汉堡包图标并显示了后退箭头图标,并且后按功能正常运行

但是当我按 subcategoryDe​​tail 的后退箭头时,从 subcategory 移到 subcategoryDe​​tail 时,按下时工作正常 但是它用汉堡包图标替换了 subcategory.java 页面的后退箭头,我无法返回主页

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    Toolbar toolbar;
    DrawerLayout drawer;
    ActionBarDrawerToggle toggle;

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

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

        displaySelectedScreen(R.id.nav_home);

    }


    public ActionBarDrawerToggle getToggle() {
        return toggle;
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            int fragments = getSupportFragmentManager().getBackStackEntryCount();
            if (fragments == 1) {
                finish();
            } else if (getFragmentManager().getBackStackEntryCount() > 1) {
                getFragmentManager().popBackStack();

            } else {
                super.onBackPressed();
                toggle.setDrawerIndicatorEnabled(true);
            }
        }
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        displaySelectedScreen(item.getItemId());

        return true;
    }

    private void displaySelectedScreen(int itemId) {
        Bundle bundle = new Bundle();
        //creating fragment object

        Fragment fragment = null;
        //initializing the fragment object which is selected
        switch (itemId) {
            case R.id.nav_home:
                fragment = new HomeFragment();
                break;
            case R.id.nav_mens_clothing:
                bundle.putString("MAINCATEGORYID", String.valueOf(1));
                bundle.putString("MAINCATEGORYNAME","Carpentry");
                fragment = new SubCategoryFragment();
                fragment.setArguments(bundle);
                break;
            case R.id.nav_womens_clothing:
                bundle.putString("MAINCATEGORYID", String.valueOf(2));
                bundle.putString("MAINCATEGORYNAME","Electrical");
                fragment = new SubCategoryFragment();
                fragment.setArguments(bundle);
                break;
            case R.id.nav_boys_clothing:
                fragment = new SubCategoryFragment();
                break;
        }

        //replacing the fragment
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }

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

}

SubCategoryFragment.java

public class SubCategoryFragment extends Fragment {
    protected Toolbar toolbar;
    protected ActionBarDrawerToggle toggle;

    View view;

    public SubCategoryFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_sub_category, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();
            shimmerContainer = view.findViewById(R.id.shimmer_view_container);
            recyclerView_subcategory = view.findViewById(R.id.recycler_view_category);

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });



        }
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        toolbar.setTitle("Cooper");
        toggle.setDrawerIndicatorEnabled(true);

    }

SubCategoryDe​​tailFragment.java

public class SubCategoryDetailFragment extends Fragment {
    protected Toolbar toolbar;
    protected ActionBarDrawerToggle toggle;
    View view;

    public SubCategoryDetailFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_sub_category_detail, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });

        }
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        toolbar.setTitle("zzz");
        toggle.setDrawerIndicatorEnabled(true);

    }}
}

2 个答案:

答案 0 :(得分:0)

使用XML设计两个不同的工具栏,并根据片段和活动设置其可见性。当“活动”加载将“工具栏”设置为“导航”抽屉时,但是当片段加载到该活动中时,隐藏当前工具栏,并替换为带有后退图标的另一个工具栏XML

不要忘记在片段的onResume()方法中添加工具栏代码。

答案 1 :(得分:0)

请在onresume中添加这两行,它将起作用。

 toggle.setDrawerIndicatorEnabled(false);
 toggle.setHomeAsUpIndicator(R.drawable.back);