DrawerLayout 侧边栏写法

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    ArrayList list = new ArrayList();
    private FrameLayout fl_content;
    private DrawerLayout drawerLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        fl_content = (FrameLayout) findViewById(R.id.fl);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        initData();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fl,MFragment.getInstace(list.get(i))).commit();
                drawerLayout.closeDrawer(lv);
            }
        });
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.e("wzq","onDrawerSlide");


            }


            @Override
            public void onDrawerOpened(View drawerView) {
                Log.e("wzq","onDrawerOpened");


            }


            @Override
            public void onDrawerClosed(View drawerView) {
                Log.e("wzq","onDrawerClosed");


            }


            @Override
            public void onDrawerStateChanged(int newState) {
                Log.e("wzq","onDrawerStateChanged");


            }
        });
    }


    private void initData() {
        for (int i = 0; i < 10; i++) {
            list.add("DrawerLayout" + i);
        }
 ArrayAdapter mAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
        lv.setAdapter(mAdapter);
    }




}

你可能感兴趣的:(DrawerLayout 侧边栏写法)