您的位置:首页 > 移动开发 > Android开发

Android DrawerLayout 侧滑界面的简单实现

2016-07-25 15:33 295 查看
首先 ,DrawerLayout 为外层布局,内层我们可以分为主视图以及侧滑部分,一般的xml布局部分如下:

<?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"
android:layout_marginTop="12dp"
android:background="@android:color/holo_red_light">

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

<ListView
android:id="@+id/lv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:scrollbars="none"
android:background="@android:color/holo_green_light"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />

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


FrameLayout中展示的是我们没有侧滑时的主视图,listview则是我们滑动之后的侧滑菜单了。这里,ListView通过设置layout_gravity来设置左滑还是右滑,分别对应start以及end,left/right并不推荐使用。之后需要做的就是往填充Framelayout以及listview了,如果想实现二者联动,即点击listview中的一个item让主视图中发生相应的变化,就可以设置监听。不能理解的是,如果listview以及gridview之类的setOnitemClickListener,那每个item只能实现相同形式的内容,那如果我想实现不同形式的东西,应该就必须自己去adapter中,用switch判断position,不同的位置我们实现不同的布局,这样是否可行?Drawerlayout有不少属性,官方文档可以多看看,理论上按照上边说的布局方式,我们就可以实现自己的侧滑菜单,有条件可以看看github上的SliddingMenu,它可以与toolbar连用,实现比较酷炫的效果,自己会尝试一下,注意点应该会记在笔记本上,还是不太习惯写blog,贼费脑子。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: