ListView显示信息列表
2017-01-16 21:17
211 查看
在主布局文件中声明一个ListView控件
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView>
在Main.activity中声明并获取此对象
private ListView listView; listView = (ListView) findViewById(R.id.listView1);
定义ListView中每一行的布局样式
item.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/appIcon" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:scaleType="fitXY" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/appName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/appIcon" android:text="AppName" android:textSize="24sp" /> <TextView android:id="@+id/appPackage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/appName" android:layout_below="@+id/appName" android:text="package" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/appSize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/appPackage" android:layout_toRightOf="@+id/appIcon" android:text="TextView" /> </RelativeLayout>
定义数据源
/* *数据源的格式是一个List集合在此集合中, *每一项又是一个Map集合, *此Map泛型定义为String,Object */ private List<Map<String, Object>> data; /* *此List即为上面Map中的object *此处的AppBean是获取手机中已安装的所有应用信息 */ private ArrayList<AppBean> src;
定义适配器
ArrayAdapter 主要用于显示简单的字符串信息要实现相对复杂的布局样式,需使用SimpleAdapter
simpleAdapter = new SimpleAdapter( this, data, R.layout.item, new String[] { "appIcon", "appName", "appPackage", "appSize" }, new int[] { R.id.appIcon, R.id.appName, R.id.appPackage, R.id.appSize } ); android.widget.SimpleAdapter.SimpleAdapter( Context context, //上下文 List<? extends Map<String, ?>> data, //数据源 int resource, //每一行的布局文件 //以下两个参数主要用于Map映射 String[] from, //Map中的每一个Key int[] to//布局文件中对应的id,Map中的Object )
为ListView绑定适配器
listView.setAdapter(simpleAdapter);
为ListView 绑定监听器
public class MainActivity extends Activity implements OnScrollListener, OnItemClickListener listView.setOnScrollListener(this); listView.setOnItemClickListener(this); @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub //此监听器中的此方法可实现动态下拉刷新 if (scrollState == SCROLL_STATE_IDLE) { if (view.getLastVisiblePosition() == view.getCount() - 1) { Toast.makeText(this, "继续向下滑动刷新列表", Toast.LENGTH_SHORT).show(); Map<String, Object> temp = new HashMap<String, Object>(); AppBean ap = src.get(0); temp.put("appIcon", ap.getAppIcon()); temp.put("appName", ap.getAppName()); temp.put("appPackage", ap.getAppPackageName()); temp.put("appSize", ap.getAppSizeM()); data.add(temp); simpleAdapter.notifyDataSetChanged(); listView.setSelection(simpleAdapter.getCount()-1); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } @Override public void onItemClick( AdapterView<?> parent,View view,int position,long id) { // TODO Auto-generated method stub Map<String,Object> temp = (Map<String, Object>) listView.getItemAtPosition(position); Toast.makeText(this,"已选中:"+temp.get("appName"),Toast.LENGTH_SHORT).show(); }
如何自定义布局文件中对数据源中对数据的使用方式
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { // TODO Auto-generated method stub if (view instanceof ImageView) { ImageView iv = (ImageView) view; iv.setImageDrawable((Drawable) data); return true; } return false; } }; simpleAdapter.setViewBinder(viewBinder);
相关文章推荐
- ListView显示信息列表
- 详细讲解ExpandableListView显示和查询仿QQ分组列表用户信息
- ListView显示列表,双击ListView里任意一行信息,出现一个可以修改的窗口
- ListView使用ArrayAdpater显示信息列表
- 使用ListView显示信息列表
- Android攻城狮的第二门课(第1季)第2章 使用ListView显示信息列表
- android学习:在ListView显示对象信息、侦听列表事件
- ListView信息显示列表介绍
- ListView中的数据一直显示在最底部(类似聊天中的信息列表)
- 使用ListView显示信息列表,用监听器和适配器,操作
- Android 使用ListView显示信息列表
- 使用ListView显示信息列表,用监听器和适配器,操作
- 空列表empty listview显示提示信息
- 具体评论ExpandableListView显示和查询模仿QQ组列表用户信息
- ListView(信息显示列表) 与ArrayAdapter
- Android实用笔记——使用ListView显示信息列表
- Android:使用ListView显示信息列表
- 第2章 使用ListView显示信息列表
- 使用ListView显示信息列表
- 使用ListView显示信息列表