ExpandableListView+Dialog实现弹出二级下拉列表选项。可自由用于Activity和Fragment中

本文介绍了如何在Android中使用ExpandableListView和Dialog组件创建二级下拉菜单,适用于Activity和Fragment。通过自定义布局和Adapter,详细阐述了实现过程,并提供了源码示例。
摘要由CSDN通过智能技术生成

话说今天刚是本人失业第二天。已经感觉有点无聊了。对于这次失业挺突然的。不过想通之后也没啥可惋惜的。毕竟身怀技术走到哪都不怕找不到饭碗。作为技术人员要做的首要就是不断提高自身技术水平,以适应这个瞬息万变的社会。
喜欢这行也是兴趣所向。喜欢那种解决问题后的成就感,完成一个功能后的喜悦感。但是觉得自己又比较矛盾,自己也确实很喜欢编程,但是却对上班编程感觉不是那么特别喜欢,原因是:虽然也是同样的写程序,上班是要根据公司领导的安排而去写一些有时候自己不是很喜欢的代码或者是没有技术含量的代码。而自己写的话肯定是写一些自己喜欢或者自己感兴趣的方向去深究和钻研。希望自己有一天实现经济自由,可以随意学习自己喜欢的技术吧。

 好了上面叨叨了很多,下来看今天的正题:
 一个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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值