【Android】使用BaseAdapter实现复杂的ListView【转】
本文转载自:http://blog.csdn.net/jueblog/article/details/11857281
步骤
使用BaseAdapter实现复杂的ListView的步骤:
1. 数据你要准备好 List getData()。
2. 继承ListActivity专有屏,不再需要setContentView(xxx)。
3. 创建一个继承自BaseAdapter的类。
4. 为List绑定适配器 setListAdapter(adapter)。
5. 用传统的方式来覆写适配器的getView函数 (从参数convertView里映射布局文件,find各个控件填充数据)。
6. 改写:加入ViewHolder类(定义n个控件的声明) 。 用convertView.setTag(viewHolder)在View和Object之间进行关联.。
7. 给按钮注册点击监听器。可以用Toast或AlertDialogue弹出选择项的数据。
friend_list.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 这是范例ListView的布局文件,出了ListView,还可以放置其他控件 -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"
- android:background="#fff"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:text="微信"
- android:background="#2B3439"
- android:gravity="center"
- android:textSize="20sp"
- android:textColor="#FFFFFF"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="18dp"
- android:layout_marginRight="18dp"
- android:layout_marginTop="2dp"
- android:layout_marginBottom="2dp"
- android:background="@drawable/btn_style_four_normal">
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/sm_searchbtn"
- android:layout_marginRight="10dp"/>
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="35dp"
- android:background="@null"
- android:ems="10" >
- <requestFocus />
- </EditText>
- </LinearLayout>
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:paddingBottom="50dp"
- android:cacheColorHint="#00000000"
- android:layout_height="match_parent" >
- </ListView>
- </LinearLayout>
friend_list_item.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="80dp"
- android:orientation="horizontal"
- android:padding="5dip"
- android:paddingBottom="15dp" >
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5dp" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000"
- android:textSize="20sp" />
- <TextView
- android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="110dp"
- android:textColor="#000"
- android:textSize="18sp" />
- </LinearLayout>
- <TextView
- android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginTop="3dp"
- android:textColor="#000"
- android:textSize="15sp" />
- </LinearLayout>
- </LinearLayout>
WeixinActivity.java文件
- package com.app.weixin;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import com.app.wexin.R;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class WeixinActivity extends Activity {
- private ImageView img;
- private List<HashMap<String, Object>> mData;
- private ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.friend_list);
- mData = getData();//为刚才的变量赋值
- MyAdapter adapter = new MyAdapter(this);//创建一个适配器
- listView = (ListView) findViewById(R.id.listView1);//实例化ListView
- listView.setAdapter(adapter);//为ListView控件绑定适配器
- }
- /** 自定义适配器 */
- public class MyAdapter extends BaseAdapter {
- private LayoutInflater mInflater;// 动态布局映射
- public MyAdapter(Context context) {
- this.mInflater = LayoutInflater.from(context);
- }
- // 决定ListView有几行可见
- @Override
- public int getCount() {
- return mData.size();// ListView的条目数
- }
- @Override
- public Object getItem(int arg0) {
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- convertView = mInflater.inflate(R.layout.friend_list_item, null);//根据布局文件实例化view
- TextView title = (TextView) convertView.findViewById(R.id.title);//找某个控件
- title.setText(mData.get(position).get("title").toString());//给该控件设置数据(数据从集合类中来)
- TextView time = (TextView) convertView.findViewById(R.id.time);//找某个控件
- time.setText(mData.get(position).get("time").toString());//给该控件设置数据(数据从集合类中来)
- TextView info = (TextView) convertView.findViewById(R.id.info);
- info.setText(mData.get(position).get("info").toString());
- img = (ImageView) convertView.findViewById(R.id.img);
- img.setBackgroundResource((Integer) mData.get(position).get("img"));
- return convertView;
- }
- }
- // 初始化一个List
- private List<HashMap<String, Object>> getData() {
- // 新建一个集合类,用于存放多条数据
- ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
- HashMap<String, Object> map = null;
- for (int i = 1; i <= 40; i++) {
- map = new HashMap<String, Object>();
- map.put("title", "人物" + i);
- map.put("time", "9月20日");
- map.put("info", "我通过了你的好友验证请求");
- map.put("img", R.drawable.pic_person);
- list.add(map);
- }
- return list;
- }
- public void showInfo(int position){
- getData();
- }
- }
效果图
【Android】使用BaseAdapter实现复杂的ListView【转】的更多相关文章
- 【Android】以BaseAdapter做适配器的ListView及其性能优化
适配器的Java类 package com.app.adapter; import org.json.JSONArray; import org.json.JSONObject; import and ...
- 【转】【Android】使用BaseAdapter实现复杂的ListView
原文网址:http://blog.csdn.net/jueblog/article/details/11857281 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 ...
- 【Android】使用BaseAdapter实现复杂的ListView
步骤 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 List getData(). 2. 继承ListActivity专有屏,不再需要setContentView ...
- Android笔记——BaseAdapter的使用
Android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示. BaseAdapter是Android应用程序中经常用到的基 ...
- Android之使用Volley框架在ListView中加载大量图片
1.listview 中的条目要用 Volley 中的 NetworkImageView,如果直接用ImageView也可以,但是要在getView方法中使用url地址设置为imageView的tag ...
- android ArrayAdapter BaseAdapter SimpleAdapter使用讲解
不是我针对谁,我只想针对新手玩家. 不清楚Adapter作用的可以看一下http://www.cnblogs.com/zhichaobouke/p/5798672.html (括号里的内容都是我主观添 ...
- android 应用架构随笔三(ListView)
import java.util.ArrayList; import java.util.List; import com.heima.googleplay.holder.BaseHolder; im ...
- android 项目学习随笔十七(ListView、GridView显示组图)
ListView.GridView显示组图,处理机制相同 <?xml version="1.0" encoding="utf-8"?> <Li ...
- android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)
1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为 2,为了使LISTVIEW的ITEM在点 ...
随机推荐
- 为元素绑定监听键盘上的enter键被按下事件的方法
$("someElement").on("keydown", function(event){ var key = event.which; if(key == ...
- pycharm 和 Anaconda 下的 opencv 安装
学习真的切忌三天打鱼两天晒网!! 一开始python下的opencv已经都弄好了,中间电脑坏了一次,好久没有接触这个,就全部都忘完了.深感惋惜. 今天又从新安装了一下opencv.在anaconda下 ...
- Python 自学积累(一)
1. 当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/pythonS ...
- SQL SERVER大话存储结构(5)_SQL SERVER 事务日志解析
本系列上一篇博文链接:SQL SERVER大话存储结构(4)_复合索引与包含索引 1 基本介绍 每个数据库都具有事务日志,用于记录所有事物以及每个事物对数据库所作的操作. 日志的记录 ...
- tomcat启动时常见错误问题集锦
1:环境变量 问题:The JAVA_HOME environment variable is not defined This environment variable is needed to r ...
- Centos6与Centos7的区别
前言 centos7与6之间最大的差别就是初始化技术的不同,7采用的初始化技术是Systemd,并行的运行方式,除了这一点之外,服务启动.开机启动文件.网络命令方面等等,都说6有所不同.让我们先来了解 ...
- java 常见几种发送http请求案例
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...
- ArcGIS for Server的安装及站点中的集群配置 分类: ArcGIS for server 2015-07-18 14:14 16人阅读 评论(0) 收藏
坚信并为之坚持是一切希望的原因. (不足之处,欢迎批评指正!) --------------------环境:Windows server2008R2虚拟机两台----------------- ...
- html/css实现文字自动换行,超出部分出现(...)
PS:这是我在别人博客copy下来的 做前端的我们都会发现这样一个问题,当你控制文字出现多行时,而这多行是有限制的(比如超出部分隐藏不显示),而这多行文字如果全部是数字或者字母抑或是数字和字母的组合时 ...
- AC自动机板子题/AC自动机学习笔记!
想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...