ListView蛮好用
知识点如下:
1. ListView的基本用法
2. ArrayAdapter和SimpleAdapter的用法
3. OnScrollListener 和 OnItemClickListener
4. notifyDataChanged 刷新数据
每一个ListView都可以包含很多列表项目Item。
关于适配器:
数据适配器将指定数据源(数组、链表、数据库、集合)填充在指定控件上。
ArrayAdapter :适用于数据源是集合和数组。
SimpleAdapter : 适用于特定的泛型集合(只能是集合)
实现过程: 新建适配器 –>添加数据源到适配器 –> 视图加载适配器
图例:
(左:数组适配器;右:简单适配器)
可见适配器是数据源和控件之间的桥梁。
来写两个代码试试看:
1. ArrayAdapter
布局文件:main_activity.xml
<RelativeLayout 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" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
MainActivity.java文件
public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> arr_adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); String[] rsc = new String[]{"商品1","商品2","商品3", "商品4","商品5","商品6","商品7","商品8", "商品9","商品10","商品11","商品12","商品13"}; //三步处理ArrayAdapter //1.2.新建适配器, 绑定数据源 arr_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,rsc); //3. 视图加载适配器 listView.setAdapter(arr_adapter); } }
而且ListView 可以滚动,如下图:
2. 代码2 (SimpleAdapter)
在创建该适配器的时候,需要这么些参数:
simpleAdapter = new SimpleAdapter(context,data,resource,from, to);
context: 上下文
data: 数据源 List<? extends Map<String,?> data> map组成的list集合,每个map代表list中的一项item
resource: 列表项的布局文件id, 就是要指定一个布局(可以使系统提供的,也可以自己写一个,然后指定)
from: map中键的名字
to: 控件中id (from和to联合使用,指定在控件的哪个位置,加载map中的哪些数据)
首先先去写一个布局文件
item.xml
<?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:orientation="horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000000" android:text="商品" /> </LinearLayout>
main_activity.xml和上面代码1一样,主布局不变
MainActivity.java
public class MainActivity extends Activity { private ListView listView; private SimpleAdapter simpleAdapter; private List<Map<String,Object>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); data = new ArrayList<Map<String,Object>>(); for (int i = 0; i < 20; i++) { Map<String,Object> map = new HashMap<String,Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text", "商品"+i); data.add(map); } int resource = R.layout.item ; String[] from = new String[]{"pic","text"}; int[] to = new int[]{R.id.image,R.id.text}; simpleAdapter = new SimpleAdapter(this, data,resource,from, to); //一定记得让视图加载适配器 listView.setAdapter(simpleAdapter); } }
效果如下:
可以上下拖动
再添加监听器来响应该控件上相关动作
代码:
1. OnItemClickListener: 可以处理视图中单个条目的点击事件
---需要重写:OnItemClick()方法
2. OnScrollListener: 检测滚动的变化,可以用于视图在滚动中加载数据
---需要重写: onScrollStateChanged()
传入的参数 scrollState 有三个预定值:
SCROLL_STATE_TOUCH_SCROLL : 手指没有离开,视图随手指滑动
SCROLL_STATE_FLING: 手指离开了,界面依靠惯性继续滑动
SCROLL_STATE_IDLE: 视图已经停止滑动
(注意其调用顺序)
监听器是程序和用户(系统)交互的桥梁
小案例:(演示一下,手指下拉能动态的增加数据就可以了)
主要是这个方法: simpleAdapter.notifyDataSetChanged();
这个时候出错了,意思是说数据源的变化,没有通知视图
需要在SCROLL_STATE_FLING分支中加上一句话:
simpleAdapter.notifyDataSetChanged();
这句话可以通知UI控件刷新界面,但是它一个劲儿,不停地刷新----不停地增加数据-----这样不好,呵呵
ListView蛮好用的更多相关文章
- ListView真的蛮好用
老规矩,今晚学过的,明天,依靠回忆写出来. 打个卡,占个版面.
- 张高兴的 UWP 开发笔记:横向 ListView
ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...
- Android—万能ListView适配器
ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...
- Android—ListView条目背景为图片时,条目间距问题解决
ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- listview下拉刷新和上拉加载更多的多种实现方案
listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局 android系统为listview提供了addfootview ...
- Android listview和gridview以及view的区别
GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...
- mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context
需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...
随机推荐
- Oracle ->> TRUNC, ROUND, CEIL, FLOOR
), ), CEIL(10.01), FLOOR(10.9999) FROM dual; 结果: TRUNC是直接截断小数位 ROUND是四舍五入 CEIL和FLOOR则是和SQL SERVER一样返 ...
- hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据
参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...
- ubuntu下安装与测试mysql
1.在决定安装mysql之前,要先确定系统是否已经安装mysql. 输入: 1 mysql 结果:说明尚未安装mysql The program 'mysql' is currently notins ...
- 《JAVA编程那点事儿》读书笔记(二)——类和对象
方法: 1. 基本的main方法: public static void main(String[] args) 2.静态方法内部调用非静态方法:重新声明一个类,通过这个类来调用非静态方法 publi ...
- POJ 1904 HDU 4685
这两道题差不多,POJ这道我很久以前就做过,但是比赛的时候居然没想起来.. POJ 这道题的题意是,N个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...
- R.id.layout等不能识别:cannot be resolved or is not a field
Do not modify the R class. The error means there's something syntactically wrong with your XML layou ...
- Model Browser
http://www.entityframeworktutorial.net/model-browser-in-entity-framework.aspx We have created our fi ...
- 【Android】 PopupWindow使用小结
PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法: 1.PopupWindow中的listview无响应 这个主要是因为show写在了set ...
- @Repository @Resource
Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...
- php的类型约束
//如下面的类 class MyClass { /** * 测试函数 * 第一个参数必须为 OtherClass 类的一个对象 */ public function test(OtherClass $ ...