使用BaseAdapter实现复杂的ListView
步骤
使用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();
- }
- }
效果图
使用BaseAdapter实现复杂的ListView相关推荐
- 【转】【Android】使用BaseAdapter实现复杂的ListView
原文网址:http://blog.csdn.net/jueblog/article/details/11857281 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 ...
- Android Listview 自定义BaseAdapter的实现及Listview优化示例
上一篇文章中我们讲了Android Listview SimpleAdapter的使用完整示例(实现用户列表)_左眼看成爱的博客-CSDN博客 本示例实现的效果图: 每个item中的checkbox选 ...
- Android BaseAdapter与ListView的使用
Android BaseAdapter与ListView的使用 定义:BaseAdapterextends Objectimplements ListAdapter SpinnerAdapter Ba ...
- Android的ArrayAdapter、SimpleAdapter、BaseAdapter与ListView的使用
工程目录: MainActivity package com.example.demo_four;import androidx.appcompat.app.AppCompatActivity;imp ...
- ListView使用BaseAdapter与ListView的优化
2019独角兽企业重金招聘Python工程师标准>>> 在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据, ...
- Android ListView使用BaseAdapter与ListView的优化
在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点.读者可 ...
- listview 滑动更改标题
2019独角兽企业重金招聘Python工程师标准>>> package com.example.listviewtest; import java.util.ArrayList; i ...
- ListView、AdapterView、RecyclerView全面解析
原文链接:http://blog.csdn.net/carson_ho/article/details/51472640 AdapterView简介 AdapterView本身是一个抽象类,Adapt ...
- Android listview与adapter用法
2019独角兽企业重金招聘Python工程师标准>>> 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListVi ...
最新文章
- 如何查看某个端口被谁占用
- PHP Mysql类【转】
- 自学Zabbix3.0版本以上资产清单inventory
- 为什么牛在天上飞 因为有人在地上吹 谈高德地图说日活过亿
- 四元数相关总结-未完
- ab plc编程软件_三菱PLC编程程序PLC的软件编程
- asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法...
- Python开发【第一篇】:目录
- dataframe读取列表中对应的列
- 高德上线“家人地图”惹争议 官方回应:用户确认授权后才能使用
- db powerdesign CDM、LDM、PDM、OOM的区别
- 功放限幅保护_功放限幅电路的制作方法
- 英语写作神器Quillbot---如何使用免费的Premium功能
- Centos 7 设置静态IP地址
- websocket断开重连解决方案,基于子慕大诗人博客修改 健壮强化版
- 一梦江湖获取服务器信息卡住,一梦江湖4月10日更新了什么 副本减负再临绝境天道盟开放...
- 解决fidder一直报“The sever(XXX) presented a certificate that did not validate,....”
- 基于Unity3D的PCAndroid端2D横屏冒险类闯关游戏
- UniApp:Vue特性篇:vue2.0的广播与接收(待详细了解)
- anchor free:CornerNet解读【目标检测】
热门文章
- nginx 域名配置_nginx 一个域名配置多个vue项目
- java 成员变量的初始化_Java类变量和成员变量初始化过程
- pythonappend用法_python中append实例用法总结
- esp32 怎么分配freertos 堆栈大小_详解STM32单片机的堆栈
- python 视频字幕替换_利用python调整srt字幕时间轴
- python操作excel表格写入多行和多列_python多处理:写入同一excel-fi
- 2018牛客网暑期ACM多校训练营(第十场)A	Rikka with Lowbit (树状数组)
- 45-机器设计问题(深搜)
- C#下的两种加密方式MD5和DEC
- 第一冲刺阶段工作总结02