Android可伸缩列表ExpandableListView
<?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" >
<ExpandableListView
android:id="@+id/province"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF">
</ExpandableListView>
</LinearLayout>
package com.example.yanlei.my; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
//设置组视图的显示文字
private String[] province = new String[] { "河南省", "河北省", "山东省","山西省" };
//子视图显示文字
private String[][] city = new String[][] {
{ "郑州市", "开封市", "新乡市", "安阳市", "南阳市"},
{ "石家庄市", "邯郸市", "保定市", "廊坊市"},
{ "济南市", "青岛市", "日照市", "烟台市", "威海市" },
{ "太原市", "大同市", "晋城市", "吕梁市", "长治市" }
}; //自己定义一个获得文字信息的方法
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_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 province.length;
} @Override
public Object getGroup(int groupPosition) {
return province[groupPosition];
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public int getChildrenCount(int groupPosition) {
return city[groupPosition].length;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return city[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);
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);
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.province);
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).toString(),
Toast.LENGTH_SHORT).show(); return false;
}
});
}
}
Android可伸缩列表ExpandableListView的更多相关文章
- android开发之ExpandableListView的使用,实现类似QQ好友列表
由于工作需要,今天简单研究了一下ExpandableListView,做了一个类似QQ列表的Demo,和大家分享一下. 效果图如下: 先来看看主布局文件: <RelativeLayout xml ...
- 在android里用ExpandableListView实现二层和三层列表
转载自http://www.cnblogs.com/nuliniaoboke/archive/2012/11/13/2767957.html 二层列表是直接用androidAPI中的Expandabl ...
- Android零基础入门第48节:可折叠列表ExpandableListView
原文:Android零基础入门第48节:可折叠列表ExpandableListView 上一期学习了AutoCompleteTextView和MultiAutoCompleteTextView,你已经 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中使用ExpandableListView实现好友分组
一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...
- Android 文章列表
Android --列表-- Android(1)-Handler Looper Message MessageQueuehttp://www.cnblogs.com/TS-qrt/articles ...
- Android数据适配-ExpandableListView
Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...
- Android权限列表permission说明
网络上不乏android权限列表,但是很少有将列表和使用方法放在一起的,所以特此总结一下 需要在AndroidManifest.xml中定义相应的权限(以获取internet访问权限为例),如下: & ...
- Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)
Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)
随机推荐
- JAVA并发-线程协作
这段时间有点忙,技术博客更新的比较少,今天更新一下相关并发的常用线程协作的类吧. ExecutorService 线程池,用于创造和复用线程,他有几种模式. 我举一个自定义线程池数量的例子如下 Exe ...
- 错误error: Exception at 0x613baa14, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)的解决办法
一.错误原因 出现这个错误的原因多半是由于指针出现错误,即出现了空指针,也有说是变量没有初始化,笔者碰到的情况是发生了内存溢出. 二.定位方法 一般这种问题直接导致程序崩溃,直接调试,就能找到对应的问 ...
- 第二章 Internet 地址结构
注意: 这个系列的博客只是为了巩固我学习的知识,参考的价值不是很大,如果需要,请转到http://www.cnblogs.com/ZCplayground/p/7764436.html Interne ...
- 详解Linux运维工程师应具备的十大技能
Linux系统如果是学习可以选用Redhat或CentOS,特别是CentOS在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...
- JZOJ 5279 香港记者
一句话题意:一个带点权边权的无向图,输出从1号点到n号点的最短路中字典序最小的一条路径 样例输入: 8 9 1 2 3 4 5 6 7 8 1 2 2 2 3 3 3 8 3 1 4 3 4 5 2 ...
- BZOJ 1043 【bzoj1043】[HAOI2008]下落的圆盘 | 暴力么??
题目: 题解: 大概是黄学长的博客 #include<cstdio> #include<algorithm> #include<cstring> #include& ...
- 设置RobotFramework的ftplibrary中,将Upload_file操作的异常改为回显错误信息。
测试中需要通过FTP通道,将数据发送给服务器,而这个上传的数据要被阻断.在结合RobotFramework测试中,安装的ftplibrary,使用upload_file操作,如果上传动作失败,会抛出异 ...
- Codeforces 932.A Palindromic Supersequence
A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 【飞天奔月出品】memcached四大注意事项(key长度,空格限制,最大item)
1. key值最大长度? memcached的key的最大长度是250个字符. 注意250是memcached服务器端内部的限制(可以修改) 如果您使用的客户端支持"key的前缀&quo ...
- 自定义View Measure过程(2)
目录 目录 1. 作用 测量View的宽/高 在某些情况下,需要多次测量(measure)才能确定View最终的宽/高: 在这种情况下measure过程后得到的宽/高可能是不准确的: 建议在layou ...