ToolBar加DrawerLayout加Fragment实现侧滑菜单

遇到需要侧滑菜单这种情况,以前总是用slidingmenu,今天我们用drawerLayout实现,比前者代码简单多了.

本例效果:


先看布局文件:

   
    
    
    
    
        
        
        
        
    
布局文件很简单,我们把actionbar替换成了toolbar,哦,对了,主题用:Theme.AppCompat.Light.NoActionBar

主要代码:

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] items = {"List1", "List2", "List3", "List04"};
    private ActionBarDrawerToggle mDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        listView = (ListView) findViewById(R.id.lv);
        //设置标题
        toolbar.setTitle(items[0]);
        setSupportActionBar(toolbar);
        //下面2行决定是否显示左上方3个横杠的图标
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //这个open和close在string分别写的开,关
        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        //决定是否切换三个横杠的图标和箭头之间切换
        mDrawerToggle.syncState();
        drawerLayout.setDrawerListener(mDrawerToggle);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl, ItemFragment.getInstance(items[0])).commit();
        listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, items));
       //条目点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                //将数据传到Fragment里面
                Fragment fragment = ItemFragment.getInstance(items[position]);
                getSupportFragmentManager().beginTransaction().replace(R.id.fl, fragment).commit();
                toolbar.setTitle(items[position]);
                //关闭左边的抽屉
                drawerLayout.closeDrawer(listView);
            }
        });
    }
    //点击图标开关抽屉
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
ItemFragment代码:

public class ItemFragment extends Fragment {
    private String name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        savedInstanceState = getArguments();
        if (savedInstanceState != null) {
            name = savedInstanceState.getString("name");
        }
    }
    public static Fragment getInstance(String name) {
        Fragment fragment = new ItemFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView tv = new TextView(getActivity());
        tv.setText(name);
        return tv;
    }
}
是不是很简单啊!

你可能感兴趣的:(ToolBar加DrawerLayout加Fragment实现侧滑菜单)