2017-05-04 101 views
1

我試圖在我的儀表板Activity上實施Navigation Drawer Activity。在我的應用程序中,我創建了一個主題,刪除了「工具欄/ ActionBar」,並在custom layout上爲我的漢堡菜單添加了ImageView,並將其包含在我的儀表板佈局中。我想要的是當點擊這個ImageView時,會顯示滑動抽屜。單擊ImageView時如何打開滑動導航抽屜?

我試圖做的是通過com.project.projectname - > New - > activity - >'Navigation Drawer Activity'添加一個Navigation Drawer Activity並自動添加NavigationActivity class , activity_navigation.xml app_bar_navigation.xml, etc

但我想知道如何打開抽屜? 在我Dashboard Activity我加入這個

public class DashboardActivity extends AppCompatActivity { 

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

    View side_tab = findViewById(R.id.side_tab); 
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu); 

    expanded_menu.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      // open drawer here 
     } 
    }); 

此外,在NavigationActivity它使用ActionBarDrawerToggle,這是因爲我不適用的ActionBar我不能使用。但是我可以使用什麼替代方法?

這裏是我的NavigationActivity

public class NavigationActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

    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.navigation, 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(); 
    Intent goToSreen; 

    if (id == R.id.nav_dashboard) { 
     goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class); 
     startActivity(goToSreen); 
    } else if (id == R.id.nav_others) { 

    } 

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

感謝您的熱心幫助。


更新

所以在我的儀表盤活動添加以下

NavigationActivity navigationActivity; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.dashboard); 

    View side_tab = findViewById(R.id.side_tab); 
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu); 

expanded_menu.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      navigationActivity = new NavigationActivity() ; 
      navigationActivity.drawer.openDrawer(GravityCompat.START); 
     } 
    }); 

而在我的航海活動我添加了一個全局變量

public class NavigationActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

DrawerLayout drawer; 


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

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

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

我設法打電話我的儀表板活動中的drawer但滑動菜單仍不顯示當我點擊我的漢堡菜單。應用程序崩潰,並顯示java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference

+0

加入DrawerLayout創建導航視圖的對象和onclick方法應用 drawer.openDrawer(GravityCompat.START); –

+0

如果你想要從佈局中刪除默認的操作欄 –

+0

@AashutoshKumar我應該把drawer.openDrawer(GravityCompat.START);在我的儀表板活動?我已經從佈局中刪除了默認的操作欄。 – natsumiyu

回答

1

我致以NavigationActivityDashboardActivity像下面

public class DashboardActivity extends NavigationActivity { 

    side_tab = findViewById(R.id.side_tab); 
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu); 


    expanded_menu.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      side_tab.setVisibility(View.INVISIBLE); 
      drawer.openDrawer(GravityCompat.START); 
     } 
    }); 

,然後按照在此之後,通過說烏斯曼@Hassan和@tahsinRupam

回答

我在dashboard.xml

1

添加將下面的代碼NavigationActivity

DrawerLayout mDrawerLayout; // Declare globally 

onCreate()

mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer); 
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu); 
expanded_menu.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      // open drawer here 
      mDrawerLayout.openDrawer(Gravity.LEFT); 
     } 
    }); 

如果抽屜是打開,關閉它onBackPressed()

@Override 
public void onBackPressed() { 
    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { 
     mDrawerLayout.closeDrawer(Gravity.LEFT); 
     return; 
    } 
} 

希望這有助於。

2

NavigationActivity

public class NavigationActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

//Add this line 
    public static DrawerLayout drawer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_navigation); 

    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(); 
....... 

添加今明改變這個

expanded_menu.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v) { 
       // open drawer here 
       NavigationActivity. drawer.openDrawer(GravityCompat.START); 
     } 
    }); 

試試這個希望它會解決你的problrm。

+0

我有一個錯誤'java.lang.NullPointerException:嘗試調用空對象引用虛擬方法'void android.support.v4.widget.DrawerLayout.openDrawer(int)' – natsumiyu

+0

您的抽屜對象爲空,檢查它適當地爲什麼它爲空? – HassanUsman

+0

@mori現在檢查我的答案。 – HassanUsman