Android 抽屉导航

  1. 云栖社区>
  2. 博客>
  3. 正文

Android 抽屉导航

科技小先锋 2017-11-15 22:34:00 浏览863
展开阅读全文

效果

012717480.png

012719736.png

使用最新的android-support-v4.jar, 见附件.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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">
    <!-- The main content view -->
     ...
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#EEA9B8"/>
</android.support.v4.widget.DrawerLayout>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    /** 抽屉导航变量 */
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
                           
...
                           
        /** 抽屉导航相关 */
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerShadow(R.drawable.draw, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
                           
        mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),
                R.layout.drawer_item, new String[] { "drawer_img",
                        "drawer_title" }, new int[] { R.id.drawer_img,
                        R.id.drawer_title }));
                           
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /** host Activity */
                mDrawerLayout,         /** DrawerLayout object */
                R.drawable.draw,  /** nav drawer image to replace 'Up' caret */
                R.string.search_label,  /** "open drawer" description for accessibility */
                R.string.defalut_text  /** "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("onDrawerClosed");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
                           
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("onDrawerOpened");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
                           
...
                           
    /**
     * 监听抽屉导航item点击事件
     */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
                           
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(view, position);
        }
    }
                           
    private boolean flag = false;
    private void selectItem(View view, int position) {
        if (position == 0) {
            if (!flag) {
                mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(true),
                        R.layout.drawer_item, new String[] { "drawer_img",
                                "drawer_title" }, new int[] { R.id.drawer_img,
                                R.id.drawer_title }));
                flag = true;
            else {
                mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),
                        R.layout.drawer_item, new String[] { "drawer_img",
                                "drawer_title" }, new int[] { R.id.drawer_img,
                                R.id.drawer_title }));
                flag = false;
            }
        else {
            TextView title = (TextView) view.findViewById(R.id.drawer_title);
            String cityName = title.getText().toString();
            this.cityInput.setText(cityName);
        }
    }



本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/1212912,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
科技小先锋
+ 关注