0

你好我想在Android Studio中用导航抽屉活动创建一个点击事件。当我点击左侧菜单中的图库时,我将其重定向到带有意图的图库活动。它可以工作,但画廊活动中没有左侧菜单。我希望所有重定向的活动都离开了菜单。我怎样才能做到这一点?导航抽屉 - 重定向活动没有左菜单

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
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.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

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

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

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     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) { 
      Intent intent = new Intent(MainActivity.this, GalleryActivity.class); 
      startActivity(intent); 
     } else if (id == R.id.nav_slideshow) { 
Intent intent = new Intent(MainActivity.this, Slideshow.class); 
      startActivity(intent); 
     } else if (id == R.id.nav_manage) { 

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

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

     } 

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

我只是改变了这种与添加意图line.Everything否则就是默认的代码时,我选择了NavigationDrawerActivity。

public class GalleryActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gallery); 
} 
} 

画廊布局

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.gamze.leftmenu05.GalleryActivity"> 


<EditText 
    android:id="@+id/editText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:text="Name" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

</LinearLayout> 

回答

1

的是你可以用活动也做到这一点。 由于在加载视图和凝视屏幕的情况下,活动有点困难或沉重。这就是为什么片段大多是导航抽屉的首选方式。

你可以做

  • 创建具有抽屉和扩大活动休息这一活动基地活动。你可以检查执行herehere
  • 第二个选项可以创建与导航抽屉的主要活动,并在内容的一部分,加载菜单中的所有片段。为此,您可以检查here
+0

通过这种方式,每当新的抽屉菜单膨胀时,这是非常低效的恕我直言。 – Fabio

+0

@Fabio不会发生这种情况,因为基本活动会保留在内存中,因此它不会再次加载,并且不会重新创建视图。 – androidnoobdev

+0

嗯,完美。我总是在这种情况下使用片段,但很高兴知道:) – Fabio

0

因为你GalleryActivity有不同的布局。

如果你想保留一切,但主窗口中的内容,我建议你使用一个片段的画廊,而不是一个活动

+0

我想使用活动。可能吗?我想申请这个我的申请,它有活动。 –