在android应用开发过程中,Listview 是经常使用的数据展现控件,往往用于显示列表形式的数据。

假设只显示数据往往会显得非常单调。非常多时候依据须要定义不同的item 背景选项。比如定义数据的标题。表头或者间隔显示Listview item背景内容。

如图的所看到的的背景内容,如今以一个WeatherDemo为模型分析Listview的相关用法。

实现思路:定义Listview不同的背景,首先要定义好标题与表格内容的两个不同的xml布局文件(city_item.xml,content_item.xml)。

在填充数据的时候往往採用构造一个Adapter数据类型,依据构造的数据类型,推断不同的数据类型,採用LayoutInflater类填充不同的layout文件,从而返回不同的View。

构造WeatherAdapter类继承BaseAdapter并实现相关的接口函数,实现对列表数据的填充。

当中数据的结构比較关键,能够控制相关的标题和内容的关系。

WeatherAdapter类则主要是实现并填充对应的数据。显示相关数据。

程序代码例如以下:

public class WeatherAdapter extends BaseAdapter {

	// 数据源
private List<HashMap<String, String>> list;
private Context context;
private int[] type; // 构造函数
public WeatherAdapter(Context context, List<HashMap<String, String>> list, int[] type) {
this.context = context;
this.list = list;
this.type = type;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = LayoutInflater.from(context);
// 产生一个View
View view = null;
// 依据type不同的数据类型构造不同的View,也能够依据1,2,3天数构造不同的样式
if (type[position] == 0) {
view = mInflater.inflate(R.layout.city_item, null);
// 获取城市名称
String cityName = list.get(position).get("data");
ImageView image = (ImageView) view.findViewById(R.id.weather_image); if (cityName.equals("北京")) {
image.setImageResource(R.drawable.beijing);
} else if (cityName.equals("上海")) {
image.setImageResource(R.drawable.shanghai); } else if (cityName.equals("广州")) {
image.setImageResource(R.drawable.guangzhou); } else if (cityName.equals("深圳")) {
image.setImageResource(R.drawable.shenzhen); }
TextView city = (TextView) view.findViewById(R.id.city);
city.setText(cityName);
} else {
view = mInflater.inflate(R.layout.content_item, null);
// 获取数据
String content = list.get(position).get("data");
// 分离数据
String[] items = content.split(","); TextView weather = (TextView) view.findViewById(R.id.content);
weather.setText(items[0] + "天气: " + items[1] + ";温度: " + items[2]);
TextView date = (TextView) view.findViewById(R.id.date);
date.setText(items[3]); }
return view;
} }

MainActivity.java 

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list_items); ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); // 各个城市的天气
String[] data = { "广州", "今天,晴天,22℃,2014-4-18",
"明天,多云转阵雨,32~23℃,2014-4-19", "后天,阴转多云,33~23℃,2014-4-20", "上海",
"今天,阵雨转雷阵雨,31~25℃,2014-4-18", "明天,雷阵雨转阴,31~24℃,2014-4-19",
"后天,阵雨转多云,32~27℃,2014-4-20", "北京", "今天,中雨转暴雨,32~25℃,2014-4-18",
"明天,暴雨转大到暴雨,29~25℃,2014-4-19", "后天,暴雨转阵雨,29~25℃,2014-4-20",
"深圳", "今天,中雨转暴雨,31~25℃,2014-4-18", "明天,暴雨,29~24℃,2014-4-19",
"后天,大雨转阵雨,28~25℃,2014-4-20" };
// 能够是城市的类型推断或者第几天推断,依据不同的需求构造不同的数组结构适应不同的应用
int[] type = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };
int size = data.length;
for (int i = 0; i < size; i++) {
HashMap<String, String> map = new HashMap<String, String>();
// 依据不同需求能够构造更复杂的数据,眼下之构造一个数据
map.put("data", data[i]);
listItem.add(map);
}
WeatherAdapter listItemAdapter = new WeatherAdapter(this, listItem, type);
list.setAdapter(listItemAdapter); }
}

三个xml布局文件:

city_item.xml

<?xml version="1.0" encoding="utf-8"?

>
<!-- items选项 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9ACD32"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip" > <ImageView
android:id="@+id/weather_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="4dip"
android:contentDescription="@string/weather_image"/> <TextView
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:textColor="@color/black"
android:textSize="18sp"/> </RelativeLayout>

content_item.xml

<?xml version="1.0" encoding="utf-8"?

>
<!-- items选项 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip" >
<!-- 能够删除
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="22dip"
android:contentDescription="@string/image" />
-->
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:textSize="15sp"/> <TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/content"
android:paddingRight="20dip"/> </RelativeLayout>

main.xml

<?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" > <ListView
android:id="@+id/list_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/white"
android:dividerHeight="1dip" >
</ListView> </LinearLayout>

源代码下载地址:http://download.csdn.net/detail/yangweixing10/7214057

android样式布局---&gt;ListView(附上源代码)的更多相关文章

  1. android页面布局(listview填充中间)

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  2. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

  3. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

  4. Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件

    UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...

  5. Android修行之路------ListView自定义布局

    主布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  6. android 开发 实现一个ListView套嵌GirdView的滚动布局

    效果图 实现思维: 首先要处理管理好需要导入的数据,我们这里创建class来处理这些数据并且便于管理它们. 创建一个主activity的布局,里面需要一个ListView控件. 创建一个class继承 ...

  7. 浅谈Android样式开发之布局优化

    引言 今天我们来谈一下Android中布局优化常用的一些手段.官方给出了3种优化方案,分别是</include>.</viewstub>.</merge>标签,下面 ...

  8. 让我们创建屏幕- Android UI布局和控件

    下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...

  9. Android 样式 (style) 和主题(theme)

    转载:https://gold.xitu.io/post/58441c48c59e0d0056a30bc2 样式和主题 样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度.填充.字 ...

随机推荐

  1. 获得当前设备可用的内存 和 获取当前任务所占用的内存 (单位:MB)(转)

    获取当前任务所占的内存: #include <sys/sysctl.h> #include <mach/mach.h> // 任务占用内存 double usedMemory( ...

  2. 解决js跨域问题

    如何解决js跨域问题 Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下 的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨 ...

  3. VBS解析时候遇到时间

    http://msdn.microsoft.com/en-us/library/aa393687(v=vs.85).aspx MSDN说的很详细么. http://msdn.microsoft.com ...

  4. spring MVC通过json与前台交互

    这里用的是spring4.1.4,jquery2.1.3,其它环境为:myeclipse2014,tomcat7,jdk7 首先,新建一个web工程,并导入springMVC的jar包(为了方便起见我 ...

  5. Android UI ActionBar功能-ActionBarSherlock 的使用

    ActionBarSherlock实现了在ActionBar上添加一个下拉菜单的功能,也是App常用的功能之一: ActionBarSherlock是第三方提供的一个开源类库,下载地址:http:// ...

  6. python 生成器理解

    通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素 ...

  7. 机顶盒加密系统流程 ECM EMM CW SK

    随着数字视频广播(DVB)的发展.观众将面对数字电视节目的选择多.广播业因为投资成本增加,这是需要收取费用的用户观看. 有条件接收系统(Conditional Access System).它的主要功 ...

  8. JavaScript中的计时器原理

    理解John Resig 在 How JavaScript Timers Work. 原理分析 timer(setInterval,setTimeout)有一个很重要的概念,时间延迟的长短是不稳定的. ...

  9. css 中的若干心得

    css布局中定位机制主要是普通的流,也就是说按照HTML文本的顺序在窗口上从上到下.从左到右去显示,遇见块级元素就换行显示.为了更进一步的控制,我可以使用相对定位.绝对定位.固定定位以及浮动. 相对定 ...

  10. git本地仓库与github远程仓库链接协议问题

    前提条件:有github账号,本地安装了git,能上网. 环境:ubuntu14.0.4LTS 首先在你得在github上创建一个仓库new repository,然后再本地创建一个文件夹mkdir ...