切换活动时,侧边栏菜单消失

时间:2020-07-02 18:43:02

标签: java android android-studio

我知道这个问题经常被问到,但是没有一个答案对我有用。

我有两个活动,MainActivityShopActivity。在MainActivity中,有一个侧边栏菜单。

当我切换活动时,菜单就会消失。

那么如何在每个活动中固定侧栏菜单?这不可能吗?

onCreate方法上的主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.drawer);
    mNavigationView = findViewById(R.id.navigationView);
    mNavigationView.setNavigationItemSelectedListener(this);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    CreateExampleList();
    BuildRecyclerView();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
}

在这里我要切换活动:

public void BuildRecyclerView() {
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    adapter = new ShopAdapter(shops);
    adapter.setOnShopClickListener(new ShopAdapter.OnShopClickListener() {
        @Override
        public void onItemClick(int position) {
            ShopItem selectedShop = shops.get(position);
            Intent k = new Intent(MainActivity.this, ShopActivity.class);
            startActivity(k);
            Toast.makeText(MainActivity.this, "Selected Shop: " + selectedShop.getShopName(), Toast.LENGTH_SHORT).show();
        }
    });
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

第二活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);
    mDrawer = findViewById(R.id.drawer);
    mToggle = new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close);
    mDrawer.addDrawerListener(mToggle);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.activity_shop, null, false);
    mDrawer.addView(contentView, 0);
}

有人知道出什么问题吗?

1 个答案:

答案 0 :(得分:2)

解决方案很简单,就是像在彼此MainActivityActivity一样初始化菜单栏。

因此,只需在第二个活动中编写MainActivity的代码即可。

第二活动onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.drawer);
    mNavigationView = findViewById(R.id.navigationView);
    mNavigationView.setNavigationItemSelectedListener(this);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    CreateExampleList();
    BuildRecyclerView();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
}