ExpandableListView 安卓二级菜单

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView)。ExpandableListView允许有两个层次:一级列表中有二级列表。
比如在手机设置中,对于分类,有很好的效果。手机版QQ也是这样的效果。

使用ExpandableListView的整体思路

(1)给ExpandableListView设置适配器,那么必须先设置数据源。

(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter,它是EXpandableListView的一个子类。需要重写里面的10个方法。
数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。
getChildView()和getGroupView()方法设置自定义布局。

(3)数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。

ExpandableListView的完整代码实现

(1)activity_main.xml:在里面放置一个ExpandableListView控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="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"
    tools:context="com.smyhvae.expandablelistviewdemo.MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

(2)item_group.xml:一级列表的item的布局

<?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="wrap_content"
              android:background="#cccccc"
              android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="group text"
        />

</LinearLayout>

(3)item_child.xml:二级列表的item的布局

<?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="wrap_content"
              android:orientation="horizontal"
              android:gravity="center" >

    <ImageView
        android:id="@+id/iv_child"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/ic_launcher" />
    <TextView
        android:id="@+id/tv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="item text" />

</LinearLayout>

(4)MainActivity.java:

package com.smyhvae.expandablelistviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    //View
    private ExpandableListView expandableListView;

    //Model:定义的数据
    private String[] groups = {"A", "B", "C"};
    //注意,字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}*/
        private String[][] childs={{"A1","A2","A3","A4"},{"A1","A2","A3", "B4"},{"A1","A2","A3","C4"}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

        expandableListView.setAdapter(new MyExpandableListView());

    }

    //为ExpandableListView自定义适配器
        class MyExpandableListView extends BaseExpandableListAdapter {

        //返回一级列表的个数
        @Override
        public int getGroupCount() {
            return groups.length;
        }

        //返回每个二级列表的个数
        @Override
        public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
            Log.d("smyhvae", "-->" + groupPosition);
            return childs[groupPosition].length;
        }

        //返回一级列表的单个item(返回的是对象)
        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        //返回二级列表中的单个item(返回的是对象)
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childs[groupPosition][childPosition];  //不要误写成groups[groupPosition][childPosition]
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //每个item的id是否是固定?一般为true
        @Override
        public boolean hasStableIds() {
            return true;
        }

        //【重要】填充一级列表
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_group, null);
            }
            TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
            tv_group.setText(groups[groupPosition]);
            return convertView;
        }

        //【重要】填充二级列表
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_child, null);
            }

            ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);
            TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);

            //iv_child.setImageResource(resId);
            tv_child.setText(childs[groupPosition][childPosition]);

            return convertView;
        }

        //二级列表中的item是否能够被选中?可以改为true
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}

注:请自行完成ConvertView和ViewHolder的优化。

时间: 2024-10-30 07:38:43

ExpandableListView 安卓二级菜单的相关文章

动态生成二级菜单

现在越来越多的用到二级甚至多级菜单,前台菜单的显示,手动指定也越来越不能满足要求,所以,动态生成菜单是必须的 思路 + 示例代码(以二级菜单为例) 先取出一级菜单内容值,接下来遍历一级菜单,将其id当做本次检索的parentid,将与之对应的二级菜单值获取到, 并加入到当前数组中(后台) 二层循环,当获取一个值时,检查其对于的二级菜单项是否有数据,有的话,则输出来,没有则跳过(前台) 以PHP后台为例 $res = mysql_query('*** where parentid = 0');  

jquery实现后台系统左侧菜单的点击展开/收缩二级菜单效果

html: <div class="col-sm-3 col-md-2 sidebar"> <div class="totalt"><a>系统管理系统</a></div> <ul class="menu"> <li class="title"> <a class="item item1"><span cla

html+css二级菜单制作!

二级菜单!!<!DOCTYPE html<html lang="e<head> <meta charset="UTF-8"> <title>Title</title> <style> *{ padding: 0; margin: 0; } .er{ width: auto; background-color: antiquewhite; } a{ text-decoration: none; } .er

一级菜单 二级菜单的联动

1.页面代码:单个集合循环生成一级和二级菜单 /* <c:forEach var="m" items="${list}" >                              <c:if test="${m.mb.father eq '-1' }">                                   <input type="checkbox" name="me

菜单(二级菜单)

一级菜单,div id是mian(i) 注意:i是数字: 二级菜单,div id是child(i)注意:i是数字 target属性是对应<iframe>标签的属性name,这表示在./zy-dzsw.html页面在<iframe>容器显示. <a href="#"><div id="main1" style="color:blue" onclick="document.all.child1.st

Javascript实现简单的下拉二级菜单

在线演示;http://jsfiddle.net/Web_Code/ThhbG/embedded/result/ <span style="font-size:14px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <

二级菜单通过一个ID查找父级ID

select `id`,`name`,`pid` AS `q_pid`,(select id From qiyu_gift_category where id = q_pid ) as root_id from qiyu_gift_category WHERE id = 1SELECT `id` FROM `qiyu_gift_category` WHERE id = (SELECT pid FROM qiyu_gift_category WHERE id = 1) LIMIT 1 二级菜单通过

菜单(三)二级菜单和标题栏菜单

package com.example.menu2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { @Overri

纯css+div写二级菜单

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>二级菜单</title> <style> * { margin:0; padding:0;}ul, li { list-style:none;}a { text-decoration:none;}.nav { border:2px solid #cc