android学习笔记13——ExpandableListView

时间:2022-09-02 09:39:35

ExpandableListView==>可展开的列表组件

==》

ExpandableListView是ListView的子类,对其进行了扩展,其将应用中的列表项分为几组,每组中又包含多个列表项;

ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供;

ExpandableListView支持的额外属性:

android:childDivider 指定各组内各子列表项之间的分隔条
android:childIndicator 显示在子列表项旁的Drawable对象
android:groupIndicator 显示在组列表项旁的Drawable对象

实例:

布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:listSelector="#00000000" >
</ExpandableListView> </LinearLayout> 实现代码==》
package com.example.myexpandablelistview; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity
{
// 设置组视图的图片
private int[] logos = new int[]
{ R.drawable.one, R.drawable.two, R.drawable.three };
// 设置组视图的显示文字
private String[] generalsTypes = new String[]
{ "One", "Two", "Three" };
// 子视图显示文字
private String[][] generals = new String[][]
{
{ "西瓜", "樱桃", "草莓" },
{ "葡萄", "梨子", "青苹果" },
{ "香蕉", "橙子", "芒果" } }; // 子视图图片
public int[][] generallogos = new int[][]
{
{ R.drawable.one, R.drawable.one, R.drawable.one },
{ R.drawable.two, R.drawable.two, R.drawable.two, },
{ R.drawable.three, R.drawable.three, R.drawable.three } }; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
final ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
// 自己定义一个获得文字信息的方法
TextView getTextView()
{
@SuppressWarnings("deprecation")
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
} // 重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount()
{
return generalsTypes.length;
} @Override
public Object getGroup(int groupPosition)
{
return generalsTypes[groupPosition];
} @Override
public long getGroupId(int groupPosition)
{
return groupPosition;
} @Override
public int getChildrenCount(int groupPosition)
{
return generals[groupPosition].length;
} @Override
public Object getChild(int groupPosition, int childPosition)
{
return generals[groupPosition][childPosition];
} @Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
} @Override
public boolean hasStableIds()
{
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(50, 0, 0, 0);
ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView); return ll;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView generallogo = new ImageView(MainActivity.this);
generallogo.setImageResource(generallogos[groupPosition][childPosition]);
ll.addView(generallogo);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setAdapter(adapter); // 设置item点击的监听器
expandableListView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id)
{ Toast.makeText(MainActivity.this,
"你点击了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT)
.show(); return false;
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

实现效果:

android学习笔记13——ExpandableListView

android学习笔记13——ExpandableListView的更多相关文章

  1. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

  2. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  3. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  4. 【转】Pro Android学习笔记(四):了解Android资源(下)

    处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...

  5. Android 学习笔记之Volley&lpar;七&rpar;实现Json数据加载和解析&period;&period;&period;

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  6. Android学习笔记进阶之在图片上涂鸦&lpar;能清屏&rpar;

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  7. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  8. Ext&period;Net学习笔记13:Ext&period;Net GridPanel Sorter用法

    Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...

  9. udacity android 学习笔记&colon; lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

随机推荐

  1. NV SDK 10 (1) Clipmaps

    Clipmaps sample: Abstract Clipmaps are a feature first implemented on SGI workstations that allow ma ...

  2. Android Service与Activity之间通信

    主要分为: 通过Binder对象 通过broadcast(广播)的形式 Activity调用bindService (Intent service, ServiceConnection conn, i ...

  3. python4delphi Python could not be properly initialized&period; We must quit&period;

    要用32位的DLL,不要用64位的dll Unable to load Python 2.7 dll with Delphi 2010 #6  Closed GoogleCodeExporter op ...

  4. 技术随笔 查找速度最快的Google IP

    转:http://www.xiumu.org/technology/the-find-the-fastest-in-the-google-ip.shtml 体验秒开GOOGLE的感觉! 在http:/ ...

  5. 【转】android开发工具Eclipse,androidStudio,adt网盘下载--不错

    原文网址:http://tools.android-studio.org/index.php/85-tools/109-android-tools-download

  6. Document原来可以这样来获取DOM

    images集合(页面中的图象) a)通过集合引用 代码 document.images              //对应页面上的<img>标签 document.images.leng ...

  7. 2017-2018-1 20155306 《信息安全系统设计基础》Mybash的实现

    2017-2018-1 20155306 <信息安全系统设计基础>Mybash的实现 要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解 ...

  8. 在Web界面中实现Excel数据大量导入的处理方式

    在早期Bootstrap框架介绍中,我的随笔<结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传.预览.提交的导入Excel数据操作流程> ...

  9. Dubbo原理和源码解析之服务暴露

    github新增仓库 "dubbo-read"(点此查看),集合所有<Dubbo原理和源码解析>系列文章,后续将继续补充该系列,同时将针对Dubbo所做的功能扩展也进行 ...

  10. Merge K Sorted List(含Merge Two Sorted LIst) leetcode java

    问题描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...