我创建了一个包含导航抽屉的基本活动。
抽屉将在幻灯片上打开,但从不通过工具栏上的导航按钮。我已经坚持了很长一段时间,并且在创建这个基础课之前我已经开始工作了,我不认为我在逻辑上改变了什么。
我知道之前已经问过这个问题,但是我已经浏览过所有其他类似的帖子而且无法解决它。
提前致谢!
public class DrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
private Toolbar toolbar;
private NavigationView navigationView;
private boolean enableToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
enableToolbar = true;
}
@Override
public void setContentView(int layoutResID){
super.setContentView(R.layout.activity_drawer);
setToolbar();
setDrawerLayout();
setNavigationDrawer();
setToolbarOnClickListener();
if(!useToolbar())
toolbar.setVisibility(View.GONE);
inflateToContentFrame(layoutResID);
}
public void setUsesToolbar(boolean _enable){
enableToolbar = _enable;
}
private boolean useToolbar(){
return enableToolbar;
}
private void setToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void setDrawerLayout(){
drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
DrawerActivity.this.invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
DrawerActivity.this.invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
DrawerActivity.this.toolbar.setAlpha(1 - slideOffset / 2);
}
};
toggle.setDrawerIndicatorEnabled(true);
drawerLayout.addDrawerListener(toggle);
drawerLayout.post(new Runnable() {
@Override
public void run() {
toggle.syncState();
}
});
}
private void setNavigationDrawer(){
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setToolbarOnClickListener(){
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DrawerActivity.this.drawerLayout.openDrawer(GravityCompat.START);
}
});
}
private void inflateToContentFrame(int layoutResID){
FrameLayout content = (FrameLayout) findViewById(R.id.content_frame);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(layoutResID, content, true);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.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.drawer, 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) {
int id = item.getItemId();
Intent _intent;
if (id == R.id.nav_classes) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_appointments) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_clients) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_payments) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if(id == R.id.nav_Settings){
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_share) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_send) {
_intent = new Intent(this, AppointmentsActivity.class);
}else{
_intent = new Intent(this, AppointmentsActivity.class);
}
drawerLayout.closeDrawer(GravityCompat.START);
startActivity(_intent);
return true;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/activity_drawer_drawer"
/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
您正在设置切换的DrawerLayout
实例不是屏幕上的实例。在setDrawerLayout()
方法中,您正在为一个从未使用过的新布局进行充气。
使用findViewById()
获取已创建的DrawerLayout
实例,并将其添加到Activity
调用中的super.setContentView()
,而不是在那里充气。
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
此外,您可以省略toolbar.setNavigationOnClickListener()
来电,因为ActionBarDrawerToggle
会设置一个监听器,并在内部处理打开和关闭抽屉。