概述

1、GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数。 
2、ExpandableListView:与ListView相比,可以让每一列单元都拥有子列表。

内容

GridView

显示3列和多行的图片以及名称 
布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:layout_margin="15dp"
android:orientation="vertical"> <GridView
android:id="@+id/grid_view"
android:background="#88000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"> </GridView> </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

每个表单元的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item_imageview"
android:layout_width="100dp"
android:layout_height="100dp"/> <TextView
android:id="@+id/item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

先写一个Fruit类

public class Fruit {
private String name;
private int img; public Fruit(String name, int img) {
this.name = name;
this.img = img;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getImg() {
return img;
} public void setImg(int img) {
this.img = img;
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

接着建立一个GridView的适配器

public class FruitGridAdapter extends BaseAdapter {
private List<Fruit> mFruits;
private LayoutInflater mInflater; public FruitGridAdapter(List<Fruit> mFruits, LayoutInflater mInflater) {
this.mFruits = mFruits;
this.mInflater = mInflater;
} @Override
public int getCount() {
return mFruits.size();
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.grid_item,null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.item_imageview);
viewHolder.textView = (TextView) convertView.findViewById(R.id.item_textview);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
Fruit fruit = mFruits.get(position);
viewHolder.imageView.setImageResource(fruit.getImg());
viewHolder.textView.setText(fruit.getName());
return convertView;
} class ViewHolder{
ImageView imageView;
TextView textView;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

主活动

public class MainActivity extends Activity {
private List<Fruit> mFruits;
private LayoutInflater mInflater;
private GridView gridView;
private FruitGridAdapter mAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid_view);
mInflater = this.getLayoutInflater(); mFruits = new ArrayList<>();
Fruit apple = new Fruit("apple", R.mipmap.a_logo1);
Fruit pear = new Fruit("pear", R.mipmap.image);
Fruit pineapple = new Fruit("pineapple", R.mipmap.pk);
Fruit grape = new Fruit("grape", R.mipmap.ic_launcher);
for (int i = 0; i < 20; i++) {
mFruits.add(apple);
mFruits.add(pear);
mFruits.add(pineapple);
mFruits.add(grape);
}
mAdapter = new FruitGridAdapter(mFruits,mInflater);
gridView.setAdapter(mAdapter);
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

结果演示 

ExpandableListView

写一个可以显示各个班级信息和班级学生信息的活动 
布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/search_frame"
android:orientation="vertical"> <ExpandableListView
android:id="@+id/expandable_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"> </ExpandableListView> </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

主单元项班级的布局

<?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:background="#55ff0000"
android:gravity="center_horizontal"
android:orientation="horizontal"> <TextView
android:id="@+id/clazz_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/> <TextView
android:id="@+id/clazz_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/> <TextView
android:id="@+id/students_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

子单元项学生的布局

<?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:background="#8800ffff"
android:gravity="center_horizontal"
android:orientation="horizontal"> <TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/> <TextView
android:id="@+id/student_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/> <TextView
android:id="@+id/student_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/> </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

首先写个班级类

public class Clazz {
private String clazzName;
private String clazzNum;
private List<Student> students; public Clazz(String clazzName,String clazzNum) {
this.clazzNum = clazzNum;
this.clazzName = clazzName;
} public String getClazzName() {
return clazzName;
} public void setClazzName(String clazzName) {
this.clazzName = clazzName;
} public String getClazzNum() {
return clazzNum;
} public void setClazzNum(String clazzNum) {
this.clazzNum = clazzNum;
} public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

接着写建立一个学生类

public class Student {
private String studentName;
private String sex;
private String age; public Student(String studentName, String sex, String age) {
this.studentName = studentName;
this.sex = sex;
this.age = age;
} public String getStudentName() {
return studentName;
} public void setStudentName(String studentName) {
this.studentName = studentName;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

适配器

public class MyExpAdapter extends BaseExpandableListAdapter {
private List<Clazz> mClazzs;
private LayoutInflater mInflater; public MyExpAdapter(List<Clazz> mClazzs, LayoutInflater mInflater) {
this.mClazzs = mClazzs;
this.mInflater = mInflater;
} @Override
public int getGroupCount() {
return mClazzs.size();
} @Override
public int getChildrenCount(int groupPosition) {
return mClazzs.get(groupPosition).getStudents().size();
} @Override
public Object getGroup(int groupPosition) {
return groupPosition;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder gvh = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.clazz_item, null);
gvh = new GroupViewHolder();
gvh.clazzName = (TextView) convertView.findViewById(R.id.clazz_name);
gvh.clazzNum = (TextView) convertView.findViewById(R.id.clazz_num);
gvh.studentsNum = (TextView) convertView.findViewById(R.id.students_num);
convertView.setTag(gvh);
} else {
gvh = (GroupViewHolder) convertView.getTag();
} Clazz clazz = mClazzs.get(groupPosition);
gvh.clazzName.setText(clazz.getClazzName());
gvh.clazzNum.setText(clazz.getClazzNum());
gvh.studentsNum.setText(clazz.getStudents().size() + "人");
return convertView;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder cvh = null;
if(convertView==null) {
cvh = new ChildViewHolder();
convertView = mInflater.inflate(R.layout.students_item, null);
cvh.studentName = (TextView) convertView.findViewById(R.id.student_name);
cvh.studentSex = (TextView) convertView.findViewById(R.id.student_sex);
cvh.studentAge = (TextView) convertView.findViewById(R.id.student_age);
convertView.setTag(cvh);
}else{
cvh = (ChildViewHolder) convertView.getTag();
} Student student = mClazzs.get(groupPosition).getStudents().get(childPosition);
cvh.studentName.setText(student.getStudentName());
cvh.studentSex.setText(student.getSex());
cvh.studentAge.setText(student.getAge());
return convertView;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
} private class GroupViewHolder {
TextView clazzName;
TextView clazzNum;
TextView studentsNum;
} private class ChildViewHolder {
TextView studentName;
TextView studentSex;
TextView studentAge;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

主活动

public class MainActivity extends Activity {
private List<Clazz> mClazzs;
private ExpandableListView mExpandableListView;
private MyExpAdapter mMyExpAdapter;
private LayoutInflater mInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mExpandableListView = (ExpandableListView)findViewById(R.id.expandable_listview);
initData();
mInflater = getLayoutInflater();
mMyExpAdapter = new MyExpAdapter(mClazzs,mInflater); mExpandableListView.setAdapter(mMyExpAdapter);
} private void initData() {
mClazzs = new ArrayList<>();
Clazz clazz1 = new Clazz("一班","201501");
Clazz clazz2 = new Clazz("二班","201502");
Clazz clazz3 = new Clazz("三班","201503");
Clazz clazz4 = new Clazz("四班","201504"); List<Student> students = new ArrayList<>();
Student zhangsan = new Student("张三","男","21");
Student lisi = new Student("李四","女","20");
Student wangwu = new Student("王五","男","22");
Student zhaoliu = new Student("赵六","女","19");
students.add(zhangsan);
students.add(lisi);
students.add(wangwu);
students.add(zhaoliu); clazz1.setStudents(students);
clazz2.setStudents(students);
clazz3.setStudents(students);
clazz4.setStudents(students); mClazzs.add(clazz1);
mClazzs.add(clazz2);
mClazzs.add(clazz3);
mClazzs.add(clazz4); }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

结果演示 

Android常用控件之GridView与ExpandableListView的用法的更多相关文章

  1. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  2. Android基本控件之GridView

    我们在使用手机的过程中,会看到一些图片配上文字的一些情况,今天我们就来介绍一下安卓控件的GridView GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元 ...

  3. Android常用控件及对应Robotium API

    最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...

  4. 常用的基本控件 android常用控件

    1.TextView:(文本框):不能编辑    android:textColor="@color/tv_show_color" 字体颜色    android:textSize ...

  5. Android常用控件

     Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...

  6. Android常用控件之RatingBar的使用

    RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...

  7. android常用控件的使用方法

    引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...

  8. Android常用控件之ExpandableList的使用

    先来看下什么是ExpandableListView 跟列表有点像,这种是可以折叠的列表,下面来看下是如何在代码中实现 一.在布局文件中声明一个ExpandableListView <Linear ...

  9. Android 常用控件的介绍

    http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...

随机推荐

  1. 整数划分问题-解法汇总(暂有DP-递归)

    整数划分问题是一个锻炼组合数学,递归以及动态规划很好的例子,虽然问题看似简单,但是其中玄机万千,有人转化成为背包问题,有人用生成函数解,有人以此作为企业面试题目,可见这种问题的认可度还是很高的. 整数 ...

  2. 1.kvm的基本搭建

    一.kvm简介 KVM 是指基于 Linux 内核的虚拟机(Kernel-based Virtual Machine). 2006 年 10 月,由以色列的Qumranet 组织开发的一种新的&quo ...

  3. JavaScript高级程序设计学习笔记--变量、作用域和内存问题

    传递参数 function setName(obj){ obj.name="Nicholas"; obj=new object(); obj.name="Greg&quo ...

  4. C#string类;math类;datetime类

    String类: .Length字符的长度   .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格   .ToUpper()全 ...

  5. webclient 和httpclient 应用

    //webclient应用 MyImageServerEntities db = new MyImageServerEntities(); public ActionResult Index() { ...

  6. 【转】CV_EXPORT定义的作用,lib及dll的区别

    http://blog.csdn.net/viewcode/article/details/8021989 在core.hpp中,CV_EXPORT是出现频率最高的词之一. 1. CV_EXPORT是 ...

  7. java调用cmd命令删除文件夹及其所有内容

    /** * *删除D盘下面test目录,感觉以前用io流遍历删除好慢! * **/ public static void main(String[] args) { Runtime run = Run ...

  8. September 19th 2016 Week 39th Monday

    We come nearest to the great when we are great in humility. 我们最为谦逊的时候越接近伟大. When you are powerful en ...

  9. Redis基础命令

    redis本身不区分命令的大小写,这里一律用小写,以下是部分简单的命令. 1.连接操作命令    quit:关闭连接(connection)    auth:简单密码认证    help cmd: 查 ...

  10. Callable 和 Future接口 学习

    * Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务. * Callable和Runnable有几点不同: * (1)C ...