【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在点 ...
随机推荐
- bigpipe&bigrender
bigpipe: 先输出页面的整体布局,在按块输出输出页面的每个部分.这样可以让服务器的运算.网络的传输和浏览器的渲染并行.适用于服务器运算较慢的时候. bigrender: 主要在浏览器端,先将字符 ...
- 解读 Android TTS 语音合成播报
随着从事 Android 开发年限增加,负责的工作项目也从应用层开发逐步过渡到 Android Framework 层开发.虽然一开始就知道 Android 知识体系的庞大,但是当你逐渐从 Appli ...
- 边的双联通+缩点+LCA(HDU3686)
Traffic Real Time Query System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- JS通过正则限制 input 输入框只能输入整数、小数(金额或者现金)
第一: 限制只能是整数 <input type = "text" name= "number" id = 'number' onkeyup= " ...
- linux 统计文件数量
查找当前目录下compose文件的数量 ls -lr | grep "compose" | wc -l
- 2.wireshark分析之TCP协议(一)
(1) TCP是怎么样的协议? TCP是一种面向连接(连接导向)的.可靠的基于字节流的传输层通信协议.TCP将用户数据打包成报文段,它发送后启动一个定时器,另一端收到的数据进行确认.对失序的数据重新排 ...
- Python开发【杂货铺】:写code经常记不住的事儿
1.添加系统环境变量: 每次写程序,把程序路径添加到环境变量中时,总是磕磕绊绊忘一些,搞得总是从之前的程序里直接copy # 程序目录添加到系统环境变量 import os import sys im ...
- django后台获取相同name名的数据
django后台获取相同name名的post数据html: <form method="post"> <input type="text" n ...
- Django-models进阶
Django-models进阶 extra extra(select=None, where=None, params=None, tables=None, order_by=None, select ...
- MDI QMdiArea 多文档区域
Qt下创建类似window平台的MDI多文档区域使用QMdiArea QMdiArea * mdiArea; mdiArea = new QMdiArea(this); //A widget 为win ...