1.不要忘记调用id 因为可以吧DrawerLayout 看做一个布局 drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private LinearLayout menu;
private List<String> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
menu = (LinearLayout) findViewById(R.id.menu);
ListView listView= (ListView) findViewById(R.id.lv);
datas = new ArrayList<String>();
for(int i=0;i<10;i++){
datas.add("菜单"+i);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//打开菜单
//drawerLayout.openDrawer(menu);
//关闭菜单
drawerLayout.closeDrawer(menu);
Bundle bundle=new Bundle();
bundle.putString("key", datas.get(position));
//添加fragment到主内内容中
ContentFragment cf=new ContentFragment();
cf.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frcontent,cf).commit();
}
});
}
}
//点击侧拉 仿 QQ 右侧进行推移 侧滑菜单滑动 推动主屏幕跟着滑动 drawer_Layout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { View content = drawer_Layout.getChildAt(0); View menu1 = drawerView; float scale = 1 - slideOffset;//1~0 content.setTranslationX(menu1.getMeasuredWidth() * (1 - scale));//0~width } @Override public void onDrawerOpened(View drawerView) { } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } });