最近时间实在匆忙,博客的代码基本没有解释。

介绍ExpandableListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <ExpandableListView
android:id="@+id/demo_expandable_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> </ExpandableListView>
</LinearLayout>

expandable_list_view_index.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

item_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="50dp"> <TextView
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

item_child.xml

我们首先使用SimpleExpandableListAdapte来为其填充数据

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by wkp on 2016/8/24.
*/
public class JustLook6 extends Activity { ExpandableListView expandableListView;
String[] groupStringArr = {"腾讯", "百度", "阿里"};
String[][] childStringArrs = {
{"QQ", "微信", "QQ浏览器"},
{"百度搜索", "百度地图", "百度外卖"},
{"淘宝", "支付宝", "天猫商城"}};
List<Map<String, ?>> groupData=new ArrayList<>();
List<List<Map<String,?>>> childData=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view_index); expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view); for (int i = 0; i < groupStringArr.length; i++) {
Map<String, String> map = new HashMap<>();
map.put("groupName", groupStringArr[i]);
groupData.add(map); List<Map<String,?>> itemList=new ArrayList<>();
for(int j=0;j<childStringArrs[i].length;j++){
Map<String,String> map0=new HashMap<>();
map0.put("itemName",childStringArrs[i][j]);
itemList.add(map0);
}
childData.add(itemList);
} SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child}); expandableListView.setAdapter(simpleExpandableListAdapter);
}
}

JustLook6.java

SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child}); 这个函数比较吓人,有9个参数,但是我们可以拆分成1+4+4的形式,其中的后面两组4个参数和SimpleAdapter类似。
依次为数据源,布局文件,“从哪里来(from)”,“对应到哪里去(to)” 然后我们可以使用实现BaseExpandableListAdapter类来填充数据
package com.ouc.wkp.ui1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; /**
* Created by wkp on 2016/8/24.
*/
public class JustLook7 extends Activity { private ExpandableListView expandableListView;
private MyDemoBaseExpandableListAdapter adapter;
String[] groupStringArr = {"腾讯", "百度", "阿里"};
String[][] childStringArrs = {
{"QQ", "微信", "QQ浏览器"},
{"百度搜索", "百度地图", "百度外卖"},
{"淘宝", "支付宝", "天猫商城"}}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view_index); expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view); adapter = new MyDemoBaseExpandableListAdapter(); expandableListView.setAdapter(adapter);
} class MyDemoBaseExpandableListAdapter extends BaseExpandableListAdapter { @Override
public int getGroupCount() {
return groupStringArr.length;
} @Override
public int getChildrenCount(int i) {
return childStringArrs[i].length;
} @Override
public Object getGroup(int i) {
return groupStringArr[i];
} @Override
public Object getChild(int i, int i1) {
return childStringArrs[i][i1];
} @Override
public long getGroupId(int i) {
return i * 100;
} @Override
public long getChildId(int i, int i1) {
return i * 100 + i1;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_group, null);
TextView textView = (TextView) view.findViewById(R.id.tv_group);
Object data = getGroup(i);
textView.setText((String) data);
if (i % 2 == 1) {
textView.setTextColor(Color.RED);
} else {
textView.setTextColor(Color.GREEN);
}
return view;
} @Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_child,null);
TextView textView=(TextView)view.findViewById(R.id.tv_child);
Object childData=getChild(i,i1);
textView.setText((String)childData);
if(i1>1){
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);
}else{
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
}
return view;
} @Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
}

JustLook7.java

运行效果

												

最新文章

  1. jquery checkbox勾选/取消勾选的诡异问题
  2. 爱默生UPS并机系统:进入与退出操作方法
  3. Ubuntu14.10 更新源,现在很多源已经停用了建议使用ubuntu15以上
  4. 超级内存NVDIMM:下一代数据中心存储关键技术
  5. collection系列用法-namedtuple()
  6. Swift迁入第三方库时的版本错误解决
  7. Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added
  8. jenkins - MultiJob使用
  9. Markdown不常见功能
  10. [Python Study Notes]匿名函数
  11. R语言-逻辑回归建模
  12. PAT甲级1022 Digital Library
  13. 根域名服务器(root DNS Servers)会被DDoS打垮么?
  14. mysql优化概述2
  15. maven(19)-生命周期和内置插件
  16. 线性判别分析 LDA
  17. mysql的if null 的用法
  18. iOS之富文本(一)
  19. 初识smarty
  20. 腾讯WeTest开启“测试扶持计划”赠送重磅福利(含MTSC/TiD门票)

热门文章

  1. Hessian Servlet和Hessian Spring的简单应用
  2. Linux 下防火墙端口设置
  3. shell 变量自增(转)
  4. UC浏览器开发者版调试手机页面
  5. 重写equal要重写 hashCode的原因
  6. PHP程序效率优化
  7. WinRAR 自动解压 解压完成后,执行批处理文件
  8. $(function() {});和$(document).ready(function() {});区别
  9. 自定义配置文件的使用(web.config/app.config)
  10. CSS3中新增的内容