ExpandableListView:List中的每一项可以展开收缩. 一种伸缩式的ListView.

android:cacheColorHint="#00000000"

这个设置可以去除拖动view时背景变成黑色的效果
android:listSelector="#00000000"

可以去除选中时的黄色底色

使用SimpleExpandableListAdapter显示ExpandableListView
public SimpleExpandableListAdapter (Context context, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo)
* 参数1.上下文对象Context
* 参数2.一级条目目录集合
* 参数3.一级条目对应的布局文件
* 参数4.fromto,就是map中的key,指定要显示的对象
* 参数5.与参数4对应,指定要显示在groups中的id
* 参数6.二级条目目录集合
* 参数7.二级条目对应的布局文件
* 参数8.fromto,就是map中的key,指定要显示的对象
* 参数9.与参数8对应,指定要显示在childs中的id

例子:

<?xml version="1.0" encoding="utf-8"?><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.light.android.study;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;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 {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {//设置组视图的图片private int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};//设置组视图的显示文字private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };//子视图显示文字private String[][] generals = new String[][] {{ "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },{ "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },{ "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }};//子视图图片public int[][] generallogos = new int[][] {{ R.drawable.xiahoudun, R.drawable.zhenji,R.drawable.xuchu, R.drawable.guojia,R.drawable.simayi, R.drawable.yangxiu },{ R.drawable.machao, R.drawable.zhangfei,R.drawable.liubei, R.drawable.zhugeliang,R.drawable.huangyueying, R.drawable.zhaoyun },{ R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,R.drawable.zhouyu, R.drawable.sunshangxiang } };//自己定义一个获得文字信息的方法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中的各个方法@Overridepublic int getGroupCount() {return generalsTypes.length;}@Overridepublic Object getGroup(int groupPosition) {return generalsTypes[groupPosition];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic int getChildrenCount(int groupPosition) {return generals[groupPosition].length;}@Overridepublic Object getChild(int groupPosition, int childPosition) {return generals[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition) {return true;}};ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);expandableListView.setAdapter(adapter);//设置item点击的监听器expandableListView.setOnChildClickListener(new OnChildClickListener() {@Overridepublic 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;}});}}

效果:

Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView_第1张图片
Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView_第2张图片







更多相关文章

  1. Android Jetpack应指南学习笔记8——Navigation(二)使用safe arg
  2. android上传图片和参数(属性)到服务器
  3. Android jni中数组参数的传递方式
  4. Android中实现整个视图切换的左右滑动效果
  5. Android 访问Webservice接口,参数对象不能串行化问题解决(java.lan
  6. Android中main.xml界面参数笔记
  7. android中LayoutParams设置参数的理解
  8. Android OKhttp 上传文件袋参数

随机推荐

  1. Android 初始化ini.rc中启动zygote服务
  2. android @id和@+id的区别
  3. Java乔晓松-android中获取图片的缩略图(
  4. Android中的线程池与任务队列
  5. Android[中级教程]第十一章 Android异步
  6. Android面试系列文章2018之Android部分Bi
  7. android studio导入其他项目工程报错解决
  8. 基于Java Socket的自定义协议,实现Android
  9. 手机硬件形态
  10. Android apk 签名总结