前两节关于ListView的,已经使用了ArrayAdapter,SimpleAdapter了,两个比较基本的适配器

这里来用一个用的最多的一个适配器BaseAdapter。

还是先上效果图。大概和微博首页差不多的

上代码:创建主页面 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

创建 ListView 要显示的内容页面:listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <ImageView
android:id="@+id/imageView"
android:layout_width="64dp"
android:layout_height="64dp"
app:srcCompat="@android:color/holo_blue_light" /> <!-- 定义一个竖直方向的LinearLayout,把QQ呢称与说说的文本框设置出来 --> <LinearLayout
android:id="@+id/new_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#1D1D1C"
android:textSize="20sp" /> <TextView
android:id="@+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" /> <TextView
android:id="@+id/times"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="14sp"/> <ImageView
android:id="@+id/image"
android:layout_width="223dp"
android:layout_height="170dp"
app:srcCompat="@drawable/ic_launcher_background" /> </LinearLayout> </LinearLayout>

创建一个dto类:

package action.sun.com.listview3;

public class Animal {
private String aName;
private String aSpeak;
private int aIcon; private String time;
private int Image; public Animal(String aName, String aSpeak, int aIcon,String atime,int aImage) {
this.aName = aName;
this.aSpeak = aSpeak;
this.aIcon = aIcon;
this.time = atime;
this.Image = aImage; } public String getaName() {
return aName;
} public String getaSpeak() {
return aSpeak;
} public int getaIcon() {
return aIcon;
} public String getTime() {
return time;
} public int getaImage() {
return Image;
} public void setaName(String aName) {
this.aName = aName;
} public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
} public void setaIcon(int aIcon) {
this.aIcon = aIcon;
} public void setTime(String time) {
this.time = time;
} public void setImage(int Image) {
this.Image = Image;
}
}

创建一个适配器:

package action.sun.com.listview3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import java.util.LinkedList; public class AnimalAdapter extends BaseAdapter {
private LinkedList<Animal> mData;
private Context mContext; public AnimalAdapter(LinkedList<Animal> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
} //listview 总数
@Override
public int getCount() {
return mData.size();
} @Override
public Object getItem(int i) {
return null;
} @Override
public long getItemId(int i) {
return i;
} // 视图
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.listview_item,viewGroup,false); ImageView img_icon = (ImageView) view.findViewById(R.id.imageView);
TextView txt_aName = (TextView) view.findViewById(R.id.name);
TextView txt_aSpeak = (TextView) view.findViewById(R.id.says);
TextView txt_times = (TextView) view.findViewById(R.id.times);
ImageView txt_image = (ImageView) view.findViewById(R.id.image); img_icon.setBackgroundResource(mData.get(i).getaIcon());
txt_aName.setText(mData.get(i).getaName());
txt_aSpeak.setText(mData.get(i).getaSpeak());
txt_times.setText(mData.get(i).getTime());
txt_image.setBackgroundResource(mData.get(i).getaImage());
return view;
}
}

主类MainActivity:

package action.sun.com.listview3;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView; import java.util.LinkedList;
import java.util.List; public class MainActivity extends AppCompatActivity { private List<Animal> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this; list_animal = (ListView) findViewById(R.id.list_item); mData = new LinkedList<Animal>();
mData.add(new Animal("Tom", "22222", R.mipmap.ic_launcher,"1天前",R.drawable.ic_launcher_background));
mData.add(new Animal("Jack", "33333333", R.mipmap.ic_launcher,"2天前",R.drawable.ic_launcher_background));
mData.add(new Animal("Json", "4444444", R.mipmap.ic_launcher,"3天前",R.drawable.ic_launcher_background));
mData.add(new Animal("Kumi", "1121313", R.mipmap.ic_launcher,"4天前",R.drawable.ic_launcher_background));
mData.add(new Animal("Timi", "88888", R.mipmap.ic_launcher,"5天前",R.drawable.ic_launcher_background));
mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
list_animal.setAdapter(mAdapter);
}
}

代码完毕,运行即可。

Android ListView的使用(三)的更多相关文章

  1. Android实训案例(三)——实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果!

    Android实训案例(三)--实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果! 感叹离春节将至,也同时感叹时间不等人,一年又一年,可是我依然是android道路上的小菜鸟,这篇讲 ...

  2. android ListView 属性

    android:divider="#fffff" 分割线颜色 android:dividerHeight="1px" 分割线高度 divider 分割线-去掉分 ...

  3. 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...

  4. Android ListView 常用技巧

    Android ListView 常用技巧 Android TextView 常用技巧 1.使用ViewHolder提高效率 ViewHolder模式充分利用了ListView的视图缓存机制,避免了每 ...

  5. Android listview addHeaderView 和 addFooterView 详解

    addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...

  6. Android -----listView的属性大全

    http://www.cnblogs.com/zhengbeibei/archive/2013/03/29/2988814.html 01     <?xml version="1.0 ...

  7. Adroid 总结--android ListView美化,个性化更改的属性

    首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和falseandroid:stackFromBottom="true"   ...

  8. Android ListView OnItemLongClick和OnItemClick事件内部细节分享以及几个比较特别的属性

    本文转自 http://blog.sina.com.cn/s/blog_783ede030101bnm4.html 作者kiven 辞职3,4个月在家休息,本以为楼主要程序员逆袭,结果失败告终继续码农 ...

  9. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

  10. Android ListView无法触发ItemClick事件

    Android ListView无法触发ItemClick事件 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承Base ...

随机推荐

  1. mysql 查询 根据时分秒取数据 比如 取 时间为 8点半的 dateformat 时间函数转换

     date_format(date,'%H') = 8 and date_format(date,'%i') = 30   SELECT * FROM `t_pda_trucklog` WHERE D ...

  2. hadoop相关内容

    数据库导出到hadoop http://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1212liuqy/ http://www. ...

  3. 安装gitlab遇到的问题

    参考文章: http://www.pickysysadmin.ca/2013/03/25/how-to-install-gitlab-5-0-on-centos-6/ 一直跟着这篇文章做,还挺顺利的, ...

  4. Groovy 学习手册(1)

    1. 需要安装的软件 Java / Groovy 对应 Java 和 Groovy,你需要安装以下软件: Java JDK,例如 JDK 8 IDE,例如 Eclipse,NetBeans 8 Gro ...

  5. ICDAR2015 数据处理及训练

    训练数据处理: 天池ICPR2018和MSRA_TD500两个数据集: 1)天池ICPR的数据集为网络图像,都是一些淘宝商家上传到淘宝的一些商品介绍图像,其标签方式参考了ICDAR2015的数据标签格 ...

  6. Centos 二进制安装node.js

    一.登录node的官网查看最新的稳定版,以及需要下载的Linux版本,你可以有多种Linux安装方式(源码安装,二进制安装等). 二.Node安装及配置 1.创建安装目录:创建目录node.js [r ...

  7. scrapy定时执行抓取任务

    在ubuntu环境下,使用scrapy定时执行抓取任务,由于scrapy本身没有提供定时执行的功能,所以采用了crontab的方式进行定时执行: 首先编写要执行的命令脚本cron.sh #! /bin ...

  8. 关于 IOS code signe 和 Provisioning Files 机制 浅析

    可以先读下这个译文. http://www.cnblogs.com/zilongshanren/archive/2011/08/30/2159086.html 读后,有以下疑惑. 在mac 机上生成的 ...

  9. 设计模式-建造者模式(Builder Pattern)

    建造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 建造者模式要求建造过程中是稳定的. Android 用到的 Builder 模 ...

  10. Atitit 路径规划法attilax总结 扫描线路法

    Atitit 路径规划法attilax总结 扫描线路法 2017/2/8 20:43:37[吐槽]深圳-小 2017/2/8 20:43:37 群主做什么的2017/2/10 10:03:15系统消  ...