【转】【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();
- }
- }
效果图
转载于:https://www.cnblogs.com/wi100sh/p/4461675.html
【转】【Android】使用BaseAdapter实现复杂的ListView相关推荐
- android 自定义baseadapter listview,android之ListView和BaseAdapter的组合使用
BaseAdapter是基本适配器,功能强大,凡是能用ArrayAdapter和SimpleAdapter解决的,都可以使用BaseAdapter解决. BaseAdapter是一个抽象类,所以需要自 ...
- Android使用BaseAdapter绑定ListView实现不同item的TextView多种文字变色
工程目录: Activity package com.example.test1121;import androidx.appcompat.app.ActionBar; import androidx ...
- android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)...
1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为 2,为了使LISTVIEW的ITEM在点 ...
- [转载]Android: 如何实现ScrollView中含有ListView?
原文地址:Android: 如何实现ScrollView中含有ListView?作者:mailofzxf ListView本身就含有ScrollView,因此把ListView放到ScrollView ...
- 使用BaseAdapter实现复杂的ListView
步骤 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 List getData(). 2. 继承ListActivity专有屏,不再需要setContentView ...
- Android之BaseAdapter—convertView回收机制与动态控件响应
前言:对于listView的BaseAdapter的派生,难度比较大.最难理解的莫过于getView(int position, View convertView, ViewGroup parent) ...
- Android 一个页面上下两个ListView的页面显示
Android 一个页面上下两个ListView,当上面的ListView过长时,下面的List基本没有了滑动空间,查阅网上资料,解决办法基本是采用ScrollView做页面滑动,notifyData ...
- Android 开发之漫漫长途 XIV——ListView
关注 code小生 ,每日一篇技术推送! 作者:忘了12138 地址:http://www.cnblogs.com/wangle12138/p/8441136.html 声明:本文是 忘了12138 ...
- Android Studio 中列表视图(ListView)的应用
目录 引言 一.列表视图的作用 二.列表视图的工作原理和工作方式 三.准备数据源 四.适配器的使用 五.布局文件中的列表视图 六.自定义列表项布局 七.列表项点击事件处理 八.列表的优化 十.示例代码 ...
最新文章
- JavaScript实现Apache .htaccess 转化nginx生成器工具-toolfk程序员工具网
- mysql timestamp 类型_MySQL中“诡异”的TIMESTAMP数据类型
- SQL中的撤销索引、表以及数据库
- Aspose.Cells使用总结大全
- 美国发布新的安全备忘录,提升关键基础设施的网络安全
- python能做什么工作-python语言能做什么工作
- Oracle 导出DMP
- LTE学习笔记二:扁平化的组网架构
- 使用导入 Excel 的方式批量修改文件名称及文件扩展名
- JAVA POI 对excel加密后 无法打开的解决办法
- THUSC2019:Illusory World
- ipsec-tools之racoon搭建
- 美创科技携零信任数据安全,亮相云安全联盟大中华区大会
- 微信应用架构!!-----微信应用能力篇
- 为什么面试完,总是让你回去等通知?
- 如何用Jmeter发送消息到Solace JNDI
- 可以一键去除水印的工具哪个好
- 嵌入式中SD卡接口电路设计
- kubernetes【工具】kind【2】集群配置
- python能参加奥赛吗-孩子学编程都能参加哪些含金量的比赛?
热门文章
- LeetCode - 695. Max Area of Island (Java)
- JSP的学习二(请求转发与 重定向)
- 小程序使用wxParse解析html
- SpringMVC中的文件上传
- C# indexof和indexofany区别(转)
- 【转载】关于Java堆和栈的解释,收藏下来以后学习
- 2012年河南省普通高校招生本科一批院校 平行投档分数线(理科)
- 插入透明32位png格式图片支持ie5.5+、 FF、chrome、safari
- div+css+theme
- linux下监控用户的操作记录