在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. 写给初学者css优先级问题

    首先需要搞清楚几个基本概念 1.内嵌样式: 写在元素标签内的例如:<div style="background-color:red"> </div> 2.内 ...

  2. 移动网络山寨版(OpenBTS)【2】频段的故事

    OpenBTS 系统有两个看点.一个是无线收发,尤其是频段的处理,另一个是网络系统,尤其是替代传统的基站(BTS),基站控制器(BSC),移动控制中心(MSC),以及(HLR/VLR)的另类方案. 先 ...

  3. C语言的本质(4)——浮点数的本质与运算

    C语言的本质(4)--浮点数的本质与运算 C语言规定了3种浮点数,float型.double型和long double型,其中float型占4个字节,double型占8个字节,longdouble型长 ...

  4. ceph理论及部署配置实践

    prefaces: ceph installation(quick)1,preflight(ins ceph-deploy repo tools)2,ceph storage cluster quic ...

  5. iOS开发之GCD使用总结

    GCD是iOS的一种底层多线程机制,今天总结一下GCD的常用API和概念,希望对大家的学习起到帮助作用. GCD队列的概念 在多线程开发当中,程序员只要将想做的事情定义好,并追加到DispatchQu ...

  6. DB_Links创建际删除

    创建 create database link gadata0008 connect to gadata0008 identified by "gold" using 'ORA11 ...

  7. IOS 8弃用api

    IOS 8弃用api 下面api是弃用: 的 UIApplication 方法和属性注冊通知. 使用新的API. 的 uiviewcontroller 面向接口的方法和属性. 中描写叙述的特征和大小类 ...

  8. 【Leetcode】Same Tree

    给定两棵二叉树,判断是否相等(即树的结构以及各结点中的值都一样) Given two binary trees, write a function to check if they are equal ...

  9. JS onkeydown控制HTML Input 只录入浮点数值

    // -1) return false; return index == 0 } keychar = String.fromCharCode(keynum) var newVal = oriVal.s ...

  10. jQuery源码笔记——三

    将类数组对象转化为数组对象 javascript中有许多类数组对象,比如HTMLCollection,NodeList,arguments.她们的特点是和数组一样有length属性,并且有0,1,2这 ...