话说今天刚是本人失业第二天。已经感觉有点无聊了。对于这次失业挺突然的。不过想通之后也没啥可惋惜的。毕竟身怀技术走到哪都不怕找不到饭碗。作为技术人员要做的首要就是不断提高自身技术水平,以适应这个瞬息万变的社会。
喜欢这行也是兴趣所向。喜欢那种解决问题后的成就感,完成一个功能后的喜悦感。但是觉得自己又比较矛盾,自己也确实很喜欢编程,但是却对上班编程感觉不是那么特别喜欢,原因是:虽然也是同样的写程序,上班是要根据公司领导的安排而去写一些有时候自己不是很喜欢的代码或者是没有技术含量的代码。而自己写的话肯定是写一些自己喜欢或者自己感兴趣的方向去深究和钻研。希望自己有一天实现经济自由,可以随意学习自己喜欢的技术吧。
好了上面叨叨了很多,下来看今天的正题:
一个ExpandableListView+Dialog实现的弹出二级下拉菜单选项的功能。直接老规矩先看效果图:
ExpandableListView相信大家从单词上都能看出来,里面有listview。没错,这里给大家大概说一下有兴趣的可以继续查阅资料了解,ExpandableListView是继承自ListView。这样一说估计心里都有个大概了。
下来看看具体实现:
这里我们是在Activity里面实现的
1.首先在 activity_main中添加基本的一些布局,用于后面触发事件和显示结果数据
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="diaog.com.expandablelistviewdialog.MainActivity">
<EditText
android:id="@+id/editText_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:textSize="22sp" />
<EditText
android:id="@+id/editText_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:textSize="22sp" />
<Button
android:id="@+id/btnShowDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show"
android:textSize="22sp" />
</LinearLayout>
这个布局没什么可说的。
2. expandablelistview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/ew_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/white" />
</LinearLayout>
这里说下它跟listview不同的是要自己独立写一个layout。下来是两个xml布局文件,一个显示一级目录一个显示对应的二级目录
group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
&l