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

我想使用活動。可能嗎?我想申請這個我的申請,它有活動。 –