ExpandableListView视图树简单应用

首先我们要自定义一个adapter来继承BaseExpandableListAdapter;

package com.example.tree;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class TreeAdapter extends BaseExpandableListAdapter {
    private String[] groups;
    private String[][] chlids;
    private Context context;

    public TreeAdapter(String[] groups, String[][] chlids, Context context) {
        this.groups = groups;
        this.chlids = chlids;
        this.context = context;
    }
    //父项长度
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groups.length;
    }
    //子项长度
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return chlids[groupPosition].length;
    }
    //返回父项对象
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groups[groupPosition];
    }
    //返回子项对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return chlids[groupPosition][childPosition];
    }
    //返回父项id
    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }
    //返回子项id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }
    //创建textview方法
    private TextView buildTextView() {
        AbsListView.LayoutParams p = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv = new TextView(context);
        tv.setLayoutParams(p);
        tv.setTextSize(20);
        tv.setPadding(60, 5, 10, 5);
        return tv;
    }
    //返回父项视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView tv = buildTextView();
        tv.setText(groups[groupPosition].toString());
        return tv;
    }
    //返回子项视图
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView tv = buildTextView();
        tv.setText(chlids[groupPosition][childPosition].toString());
        return tv;
    }
    //设置子项是否可以点击
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

布局文件

<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=".MainActivity" >

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

</RelativeLayout>

activity

package com.example.tree;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

public class MainActivity extends Activity {
    private ExpandableListView ex;
    private TreeAdapter adapter;
    private String[] groups = { "吉林", "辽宁", "黑龙江", "湖南" };
    private String[][] childs = { { "长春", "四平", "通话" }, { "大连", "沈阳" },
            { "绥芬河", "哈尔冰" }, { "长沙" } };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ex = (ExpandableListView) findViewById(R.id.ex);
        adapter = new TreeAdapter(groups, childs, this);
        //注册上下文菜单
        //super.registerForContextMenu(ex);
        ex.setAdapter(adapter);
        // 子项的点击事件
        ex.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,
                        childs[groupPosition][childPosition], 1000).show();
                return false;
            }
        });
        //父项点击事件
        ex.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this,
                        groups[groupPosition], 1000).show();
                return false;
            }
        });
        //父项关闭事件
        ex.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this,
                        "关闭", 1000).show();
            }
        });
        //父项打开事件
        ex.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this,
                        "打开", 1000).show();
            }
        });
    }

}
时间: 2024-10-09 15:44:25

ExpandableListView视图树简单应用的相关文章

自定义实现ExpandableListView收缩的简单动画效果

以下是 ExpandableListView 收缩的简单动画效果 1 /* 2 * Copyright (C) 2014 Gary Guo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the L

监听视图树 OnGlobalLayoutListener

背景 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们写一个控件 public class MyImageView extends ImageView {     public MyImageView(Context context, AttributeSet attrs) {         super(context, attrs);     }     public MyImageView(Context context) {       

NSViewAnimation视图的简单动画

NSViewAnimation和NSAnimation提供了视图的简单动画效果.NSViewAnimation是从NSAnimation继承下来的.这个类提供了一个简便的方式去给多个视图或窗口做动画效果.动画的效果可以改变视图的位置,大小,淡入淡出. - (id)initWithViewAnimations:(NSArray*)viewAnimations 初始化方法需要参数是一个包含字典对象的数组对象.这个字典对象信息包含4个键值对.如下 NSString *NSViewAnimationTa

HDU 3016 Man Down 线段树+简单DP

囧,一开始看错题意,后来才发现人是垂直下落的,被附带链接里的Man Down游戏误导了. 那就变成了一个简单的DAG模型动态规划,随意搞就ok了 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queu

数据库视图及其简单创建

视图是一个虚拟的表,该表中的数据是由一个查询语句执行后所得到的查询结果所构成的. 创建视图: CREATE VIEW VIEW_Name AS SQL 语句 例子: create view pr_例四asselect 产品.产品ID,产品.产品名称,产品.供应商,类别.类别名称,类别.说明from 产品 inner join类别 on 产品.类别ID=类别.类别ID 查看视图: select * from pr_例四 结果 数据库视图及其简单创建

字典树简单运用--》去亿万条城市三字码数据中Check是否存在某个城市三字码

前言:不知不觉来C***P半年了,过得也算是不好不坏.遇到过出问题只会推给开发,一直抱怨却提不出方案的业务:也遇到过大V开头(可以问度娘)的水的冒泡的开发. 有两件小事和大家分享一下: 1)我们组的接口调用某个外部接口,有些条件下该外部接口会直接把exception(堆栈信息,代码物理路径也有..)抛给我们,我邮件给相关的测试和开发, 不出意外,没有回复:后来不知道他们搞什么鬼,想让我们把这个exception也Log下来... 2)说起这个大V开头的,也是我们要调用的某个外部接口的开发.简单描

UINavigationController切换视图的简单使用

UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈. 下面通过简单的示例说明 AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen

敌兵布阵 (线段树简单模板题)

https://vjudge.net/contest/318019#problem C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视. 中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向

iOS设计之 多视图导航栏UINavigationController切换视图的简单设计

在iOS平台上创建有个工程,之后在工程中创建两个类视图 操作步骤如下 1.在分别在两个类视图中对主视图设置背景色 FirstViewController.m #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {    [super viewDidLoad];        //设置主视