【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90%以上都是需要自己自定义listview的,这一篇,我们将介绍如何使用自定义布局的listview
先看效果图
好了,其实这种自定义布局的实现,是通过自定义adapter来实现的,首先我们简单介绍下adapter
每个listview要想实现数据的显示,都必须绑定一个adapter,adapter主要实现将数据和listview中的每一个item进行绑定,这样就可以灵活的设计自己的布局了,我们看一下这次的项目目录
现在开始看代码
主界面的布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>
item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="10dp"
android:src="@drawable/ic_launcher" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="标题"
android:textSize="20sp" /> <TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:text="简介"
android:textSize="16sp" />
</LinearLayout> </LinearLayout>
实体类thing.java
package com.example.diylistview; public class Thing { private String title;
private String Introduce;
private int picture; public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getIntroduce() {
return Introduce;
} public void setIntroduce(String introduce) {
Introduce = introduce;
} public int getPicture() {
return picture;
} public void setPicture(int picture) {
this.picture = picture;
} /**
*
*/
public Thing() {
super();
} /**
* @param title
* @param introduce
* @param picture
*/
public Thing(String title, String introduce, int picture) {
super();
this.title = title;
Introduce = introduce;
this.picture = picture;
} }
MyAdapter.java
package com.example.diylistview; import java.util.List; 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; public class MyAdapter extends BaseAdapter { private Context context;
private List<Thing> lists;
private LayoutInflater layoutInflater;
ImageView img;
TextView tv1;
TextView tv2; /**
* 构造函数,进行初始化
*
* @param context
* @param lists
*/
MyAdapter(Context context, List<Thing> lists) {
this.context = context;
this.lists = lists;
layoutInflater = LayoutInflater.from(this.context);
} // 获得长度,一般返回数据的长度即可
@Override
public int getCount() {
return lists.size();
} @Override
public Object getItem(int position) {
return lists.get(position);
} @Override
public long getItemId(int position) {
return position;
} /**
* 最重要的方法,每一个item生成的时候,都会执行这个方法,在这个方法中实现数据与item中每个控件的绑定
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convertView对象就是item的界面对象,只有为空的时候我们才需要重新赋值一次,这样可以提高效率,如果有这个对象的话,系统会自动复用
//item_listview就是自定义的item的布局文件
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item_listview, null);
}
//注意findViewById的时候,要使用convertView的这个方法,因为是在它里面进行控件的寻找
img = (ImageView) convertView.findViewById(R.id.img);
tv1 = (TextView) convertView.findViewById(R.id.tv1);
tv2 = (TextView) convertView.findViewById(R.id.tv2);
//将数据与控件进行绑定
img.setBackgroundResource(lists.get(position).getPicture());
tv1.setText(lists.get(position).getTitle());
tv2.setText(lists.get(position).getIntroduce());
return convertView;
} }
MainActivity.java
package com.example.diylistview; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private ListView listview;
private List<Thing> lists; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
//初始化数据
lists = getLists();
//设置适配器
listview.setAdapter(new MyAdapter(this, lists));
//设置监听
listview.setOnItemClickListener(this);
}
//返回数据
private List<Thing> getLists() {
List<Thing> lists = new ArrayList<Thing>();
for (int i = 0; i < 20; i++) {
Thing thing = new Thing();
thing.setPicture(R.drawable.ic_launcher);
thing.setTitle("我是标题" + i);
thing.setIntroduce("我是简介" + i);
lists.add(thing);
}
return lists;
}
//item的点击监听时间
@Override
public void onItemClick(AdapterView<?> view, View arg1, int position,
long arg3) {
Toast.makeText(this, ((Thing)view.getItemAtPosition(position)).getTitle(), 0).show(); } }
好了,自定义的listview的使用就到这里了,如果有疑问,请留言
【Android基础】listview控件的使用(4)-----自定义布局的listview的使用的更多相关文章
- 【Android基础】listview控件的使用(1)------最简单的listview的使用
listview控件是项目开发中最常用的空间之一,我将慢慢推出关于listview的一系列的文章,先从最简单的,系统自带的listview开始吧! 先上效果图: activity_one.xml &l ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- ListView控件的Insert、Edit和Delete功能(第一部分)
摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...
- C# ListView 控件和 INotifyPropertyChanged 接口
ListView 控件和 DataGridView 控件 ListView 是跟 Winform 中 DataGridView 用法以及显示效果差不多的一个 WPF 控件,可以通过列表的方式方便的显示 ...
- ListView 控件和 INotifyPropertyChanged 接口
原文:ListView 控件和 INotifyPropertyChanged 接口 ListView 控件和 DataGridView 控件 ListView 是跟 Winform 中 DataGri ...
- 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview
前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...
- 【Android基础】listview控件的使用(2)-------继承自ListActivity的普通listview
由于listview在android控件中的重要性,所以android为我们直接封装了一个类ListviewActivity,直接将listview封装在了activity之中,在本篇中,我将介绍在L ...
- Android学习之基础知识五—ListView控件(最常用和最难用的控件)
ListView控件允许用户通过上下滑动来将屏幕外的数据拉到屏幕内,把屏幕内的数据拉到屏幕外. 一.ListView的简单用法第一步:先创建一个ListViewTest项目,在activity_mia ...
随机推荐
- android
在特殊应用的特殊功能,以帮助通信系统的问题
在实际工程中的应用,进入一个特殊的应用后,系统的某个功能不能起作用. 当然,这个通信有非常多办法能够做到.笔者能够想到的至少有例如以下几种 1.利用property熟悉来实现,这种话须要添加一个特殊的 ...
- poj-2195-Going Home最小费用最大流
重新切一遍最小费用最大流~~~ 这到题目的数据范围有问题,尽量开大就好了~~ #include<stdio.h> #include<iostream> #include< ...
- linux常用系统配置命令汇总
系统配置及查看信息相关命令 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看C ...
- 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)——自定义安装
原文:使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)--自定义安装 自定义产品卸载方式 继续从上一次的基础上前进,现在我们已经知道了最简单的bootstr ...
- Codeforces 452A Eevee
#include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...
- mongoDB 查询附近的人的语句
mongoDB 自带LBS查询附近的人 {"location":{ $nearSphere: { $geometry: { type : "Point", co ...
- 在VS2012中使用GitHub
注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(Test) *创建完成后,记录该仓库的地址:https://github.com/ ...
- Cocos2d-X研究之v3.x瓦片地图具体解释
在游戏开发过程中,我们会遇到超过屏幕大小的地图,比如即时战略游戏,使得玩家能够在地图中滚动游戏画面.这类游戏一般会有丰富的背景元素,假设直接使用背景图切换的方式,须要为每一个不同的场景准备一张背景图, ...
- 逆向project第004篇:令计算器程序显示汉字(下)
一.前言 钩子技术是一项很有有用价值的技术.在Windows下HOOK技术的方法比較多,使用比較灵活,常见的应用层的HOOK方法有Inline HOOK(详见<反病毒攻防研究第012篇:利用In ...
- windows使用nginx+memcached实现负载均衡和session或者缓存共享
windows使用nginx+memcached实现负载均衡和session或者缓存共享 两台server server1:115.29.186.215 windows2008 64位操作系统 ser ...