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

介绍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

运行效果

												

android入门——UI(5)的更多相关文章

  1. Android入门——UI(8)——Fragment(2)

    先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...

  2. Android入门——UI(9)

    SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...

  3. Android入门——UI(7)——Fragment

    先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  4. android入门——UI(6)——ViewPager+Menu+PopupWindow

    一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...

  5. android入门——UI(4)

    GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  6. android入门——UI(3)

    Spinner控件   ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...

  7. Android入门——UI(2)

    介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...

  8. android入门——UI(1)

    一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...

  9. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

随机推荐

  1. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  2. Mysql两种存储引擎的优缺点

    MyISAM引擎是一种非事务性的引擎,提供高速存储和检索,以及全文搜索能力,适合数据仓库等查询频繁的应用.MyISAM中,一个table实际保存为三个文件,.frm存储表定义,.MYD存储数据,.MY ...

  3. Curious Robin Hood(树状数组+线段树)

    1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...

  4. S3C2440实现wifi、3G上网和迷你无线路由的制作(一)

    S3C2440实现wifi.3G上网和迷你无线路由的制作 fulinux 凌云实验室 本文将通过ARM.linux平台,借助RT2070/RT3070芯片的无线模块(或使用RT2070/RT3070芯 ...

  5. 【Stackoverflow好问题】Java += 操作符实质

    问题 直到今天,我都一直以为: i += j 等同于 i = i + j; 但如果有: int i = 5; long j = 8; 这时 i = i + j不能编译.但i += j却能够编译.这说明 ...

  6. XML转化DS等

    public class XmlData    {        /// <summary>        /// 将DataTable对象转换成XML字符串        /// < ...

  7. asp.net ImageMap控件

    ImageMap 控件可创建包含定义的作用点区域的图像.当用户单击作用点区域时,该控件可生成到服务器的回发或导航到指定的 URL 首先是添加一个asp:ImageMap 选择asp:CircleHot ...

  8. 如何启用Oracle EBS Form监控【Z】

    前言: 有时候,因某些需要,必须知道Oracle的Form被使用的情况,以方面我们做出决策: 例如,如果某个Form被使用的次数非常多,那么,这个Form的相关SQL代码就应该优先处理,以减少服务器负 ...

  9. Programming C#.Classes and Objects.只读字段

    只读字段 当字段声明中含有 readonly 修饰符时,该声明所引入的字段为只读字段.给只读字段的直接赋值只能作为声明的组成部分出现,或在同一类中的实例构造函数或静态构造函数中出现.(在这些上下文中, ...

  10. 安装Eclipse Html Editor

    最近在eclipse中开发android项目,用到了jquery mobile框架,则会涉及到新建html文件,发现eclipse不自带新建html文件的插件,必须得新建一个其他形式的文件,譬如xml ...