前两节关于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. 【Servlet】web.xml中welcome-file-list的作用

    今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...

  2. 【Linux】关于减号 - 的用途

    管线命令在 bash 的连续处理程序中是相当重要的!另外,在 log file 的分析当中也是相当重要的一环, 所以请特别留意!另外,在管线命令当中,常常会使用到前一个命令的 stdout 作为这次的 ...

  3. /proc 目录详细说明

    /proc路径详细: Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  4. Mac OS X各版本号的历史费用和升级关系

     Mac OS X各版本号的历史费用和升级关系 OS X 10.6 Snow Leopard 早在2009年10月,Mac OS X10.6雪豹是通过光盘发送.并在英国推出时.费用£25 OS X ...

  5. Sublime Text增加Build system类型,打造一个全能IDE

    Sublime text2是一款非常方便的文本编辑器,现在我基本上不用IDE去编写代码,一般都是在Sublime text2中编辑,当然,这里无法执行.debug是软肋,于是上网找了下资料,可以把添加 ...

  6. 转: springboot2.0下hystrix.stream 404

    springboot2.0下hystrix dashboard Unable to connect to Command Metric Stream解决办法https://blog.csdn.net/ ...

  7. BIO NIO AIO 简介

    原文: https://github.com/zhongmingmao/nio_demo 简介 NIO与AIO的简单使用 基本概念 同步与异步 同步和异步是针对应用程序和内核的交互而言的:同步指的是用 ...

  8. PhotoShop CS6实现照片背景虚化效果

    在摄影实践中,虚化背景是突出主体的常用手段.但是由于消费级DC镜头的实际焦距都很短,因此实现浅景深而虚化背景的难度较大.如果我们希望用消费级DC也能达到虚化背景突出主体的效果,那么,Photoshop ...

  9. 【八】注入框架RoboGuice使用:(Your First Injected Fragment)

        上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解     ...

  10. IOS 学习 Key-value coding

    1 前言 本节我们主要介绍键值编码,以及如何使一个类符合KVC编码. 2 详述 键-值编码是一个对于间接访问一个对象属性和关系使用字符标识的机制.它支持或者关系几个特别的Cocoa编程机制和技术,在C ...