2017-05-04 98 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