DrawerLayout 和ActionBar的联动http://blog.csdn.net/Rodulf/article/details/50901481
在按照上面做的时候,DrawerLayout 和ActionBar的联动中遇到问题,ActionBar 不能控制DrawerLayout
后来发现,我重写的方法错了,
//TODO 注意了这里ActionBar和 DrawerLayout联动,
需要重写的方法是 onOptionsItemSelected而不是onContextItemSelected
做一个记录:
package tech.androidstudio.navigationviewdrawerlayoutdemo;import android.support.design.widget.NavigationView; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MenuItem; import android.widget.Toast;public class MainActivity extends AppCompatActivity{private DrawerLayout mDrawerLayout;private ActionBarDrawerToggle mToggle;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,0,0);getSupportActionBar().setDisplayHomeAsUpEnabled(true);mToggle.syncState();mDrawerLayout.setDrawerListener(mToggle);//看 源码 ActionBarDrawerToggle有 实现 Drawerlistener所以 可以添加 }@Override public boolean onOptionsItemSelected(MenuItem item) {if(mToggle.onOptionsItemSelected(item)){return true;}return super.onOptionsItemSelected(item);}//TODO 注意了这里ActionBar和 DrawerLayout联动,需要重写的方法是 onOptionsItemSelected而不是onContextItemSelected /** @Override public boolean onContextItemSelected(MenuItem item) { // return super.onContextItemSelected(item); Toast.makeText(this,"Toggle Clicked",Toast.LENGTH_LONG).show(); if(mToggle.onOptionsItemSelected(item)){ return true; } return super.onContextItemSelected(item); } */ }