【support包学习】DrawerLayout的使用

为了让开发者能更快更好的进行安卓app开发(或者是为了填坑),谷歌推出了一系列的support package给广大开发者使用。从最早的v4到现在的v21,里面包括了很多好用的UI控件例如ViewPager以及工具类。本系列仅用于记录support包各个常用模块的学习过程。

曾几何时,对于我这种UI生手来说为app实现侧边栏有着相当的工作量,现如今谷人希在v4包里面为我们带来了利器DrawerLayout。侧边栏就是弹指一挥间的事情了,下面直接上demo。

首先,将主Activity的layout改为DrawerLayout,可以看出实际上DrawerLayout由两部分组成,一部分是作为主界面的FrameLayout,一部分是作为侧边栏的ListView。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent">

    <FrameLayout  android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" />

    <ListView  android:id="@+id/left_drawer" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/holo_blue_light" android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

然后在主Activity的onCreate()中设置侧边栏菜单

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawerlayout);

        mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];

        // 三个菜单选项
        drawerItem[0] = new ObjectDrawerItem(R.drawable.reset, "Create");
        drawerItem[1] = new ObjectDrawerItem(R.drawable.security, "Read");
        drawerItem[2] = new ObjectDrawerItem(R.drawable.touch, "Help");

        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // 初始化状态
        selectItem(0);
    }

菜单部分很简单,就是普通的ListView,设置好adapter以及listener就完事儿了。至于主界面,可以使用fragment来填充,当用户点击不同的菜单的时候切换不同的fragment即可。

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

private void selectItem(int position) {

        CreateFragment fragment = null;

        switch (position) {
            case 0:
                fragment = new CreateFragment();
                fragment.setTitle("Create");
                fragment.setIcon(R.drawable.reset);
                break;
            case 1:
                fragment = new CreateFragment();
                fragment.setTitle("Read");
                fragment.setIcon(R.drawable.security);
                break;
            case 2:
                fragment = new CreateFragment();
                fragment.setTitle("Help");
                fragment.setIcon(R.drawable.touch);
                break;

            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            ActionBar ab = getActionBar();
            if (ab != null) {
                ab.setTitle(mNavigationDrawerItemTitles[position]);
            }
            mDrawerLayout.closeDrawer(mDrawerList);

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

可从这里下载demo源码。

你可能感兴趣的:(support包学习)