Android之listview运用(美团美食列表)
首先我们将listview简单实现,有图形,有文字:效果如图


之前我们完成了一个较为简单的listview视图列表,但是生活中我们往往碰到的
是更为复杂列表,有图像有评分标准,不如我们来试一试手,做一个琳琅满目的美团美食列表,在看的口水涟涟份上我们来实现它。
首先我们定义模板,也是第一次在安卓中接触模板概念,在layout里定义模板文件,data_list.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/pic"
- android:padding="3px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:layout_width="200px"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/title"
- android:padding="3px"
- android:textSize="26px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- <TextView
- android:id="@+id/author"
- android:padding="3px"
- android:textSize="15px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/score"
- android:padding="5px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/type"
- android:padding="5px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
- </LinearLayout>
也就是这样:
然后在main.xml布局文件定义:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/textView1"
- android:textSize="50px"
- android:gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="美食列表" />
- <ListView //这里之后我们会把模板嵌入进去
- android:id="@+id/datalist"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
以上的listview只是一个名字定义,我们建立好模板就可以代入进去,如何代入还是用Simpleadapter适配器,这部分实现要在MainActivity.java里实现,首先我们准备好几张美食图片,以及打分。
然后在代码里实现:
- public class MainActivity extends Activity {
- private int[] pic=new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4
- ,R.drawable.a5,R.drawable.a6};
- private String data[][]=new String[][]{{"来御来三汁焖锅","烧烤烤肉"},{"SPK思必客麻辣香锅","烧烤烤肉"},
- {"冰果彩虹","蛋糕甜点"},{"德克士","小吃快餐"},{"老北京炸酱面","小吃快餐"},{"铁板传奇","其他美食"}};
- private List<Map<String,String>> list=new ArrayList<Map<String,String>>();
- private ListView datalist;
- private SimpleAdapter simpleadapter=null;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.datalist=(ListView)super.findViewById(R.id.datalist);
- for(int i=0;i<data.length;i++){
- Map map=new HashMap<String,String>();
- map.put("pic", String.valueOf(this.pic[i]));
- map.put("title", this.data[i][0]);
- map.put("author",this.data[i][1]);
- map.put("score",String.valueOf(R.drawable.star) );
- map.put("type", "岳麓区天马");
- this.list.add(map);
- }
- this.simpleadapter=new SimpleAdapter(this,this.list,R.layout.data_list,
- new String[]{"pic","title","author","score","type"}
- ,new int[]{R.id.pic,R.id.title,R.id.author,R.id.score,R.id.type});
- this.datalist.setAdapter(this.simpleadapter);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
如同上节课一样,一一匹配,首先将图片之类的统统存入数组,数组统统存入map健和value值,然后将map存入list队列里面,然后加入到适配器里面进行分封装,然后ListView就坐享其成得到这个适配器。
然后我们的美团列表就冰果诞生了:


Android之listview运用(美团美食列表)的更多相关文章
- Android开发 ListView(垂直滚动列表项视图)的简单使用
效果图: 使用方法: 1.在布局文件中加入ListView控件: <?xml version="1.0" encoding="utf-8"?> &l ...
- Android使用listView,BaseAdapter实现列表页
参考: 1.讲解很详细: blog.csdn.net/psuaije/article/details/7447391 总结: 代码:
- 我的Android进阶之旅------>Android二级ListView列表的实现
实现如下图所示的二级列表效果 首先是在布局文件中,布局两个ListView,代码如下: <LinearLayout xmlns:android="http://schemas.andr ...
- Android一个ListView列表之中插入两种不同的数据
http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...
- Android通过LIstView显示文件列表
[绥江一百]http://www.sj100.net 欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...
- 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)
引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...
- Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表
本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...
- Android 自定义 ListView 显示网络上 JSON 格式歌曲列表
本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶 ...
- Android 使用ListView显示信息列表
课程目标1.理解ListView的基础使用2.学会熟练运用两种适配器(ArrayAdapter.SimpleAdapter)3.学会熟练运用两种监听器(OnScrollListener.OnItemC ...
随机推荐
- uboot mmc read/write命令用法
mmc read用来读取mmc内容到内存, mmc write用来写入内存内容到mmc中 具体用法, mmc read <device num> addr blk# cnt [partit ...
- C#.NET常见问题(FAQ)-如何使用DataGridView跟Excel数据交互
1 从工具箱中拖进来一个DataGridView 2 就像Excel表头,可以添加一个表头(即一列的抬头,比如叫做A) 3 一次添加ABCDE等项目,也可以修改该类目的类型为Button或者C ...
- Using LACP with a vSphere Distributed Switch 5.1
Using LACP with a vSphere Distributed Switch 5.1 by Chris Wahl on Oct 15th, 2012 | 6,347 views One o ...
- VCAP5-DCA Objective 1.3 – Configure and Manage Complex Multipathing and PSA Plug-ins
http://virtuallyhyper.com/2012/10/vcap5-dca-objective-1-3-configure-and-manage-complex-multipathing- ...
- https调试
我们知道https通信在开始时会发送一个METHOD为CONNECT的请求,让服务器将证书以及相关的信息返回给浏览器,在没有得到这些信息之前,浏览器是不会信任服务器发来的任何数据的.So现在我们要让F ...
- css改变hr颜色
html中用css改变颜色,<hr style="border:0;height:1px;">如果不加border:0;的话,虽然颜色改变了,但是会显示一条黑色的边框. ...
- Selenium高亮页面对象
使用QTP习惯了,在QTP中可以通过访问对象的highlight方法直接高亮对象,确实很方便,那么如何让Selenium高亮页面的测试对象了,可以通过javascript修改页面对象的属性进而高亮对象 ...
- python模块之linecache
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之linecache import linecache ''' >>> h ...
- Windows下 VS2015编译boost1.62
VS2015编译boost1.62 Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有 ...
- Redis C客户端Hiredis代码分析
初始化 redisContext - Redis连接的上下文 /* Context for a connection to Redis */ typedef struct redisContext { ...