ListView类

Class Overview


A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

一、使用数据 适配器, 创建ListView:

1、布局文件ListView
2、创建数据适配器
3、ListView与适配器关联,数据加载到ListView上
     lv.setAdapter(ada);
1 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<ListView

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

</ListView>

</LinearLayout>

2 数据适配器ListAdapter及实现类

public interface

ListAdapter

implements Adapter

android.widget.ListAdapter
Known Indirect Subclasses

ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, WrapperListAdapter

Class Overview


Extended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.

创建数据适配器:

//getView,getCount方法需要覆写,才会显示List记录

BaseAdapter adapter = new BaseAdapter() {

//List中所存放的View,如果是只是一个TextView(简单数据),则返回的是TextView.

TextView tv = new TextView(AdapterActivity.this);

tv.setPadding(8, 8, 8, 8);

tv.setTextSize(20);

tv.setText(arrs[position]);

//如果是一个自定义的复杂的布局,如微博的列表的布局,一行中包含有图片,摆放不同位置的TextView,返回的就是一个layout

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

// 1、获得行布局 用LayoutInflater

// 2、更新行布局

return null;

}

//显示List条数

@Override

public int getCount() {

// TODO Auto-generated method stub

return 0;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

};

//只有一个TextView的行的布局,listitem.xml (如果是复杂的布局,用布局加控件)

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

</TextView>

二、如果ListView一行是TextView,可以采用ArrayAdapter来实现,不用创建BaseAdapter
1、布局文件activity_main.xml一样
2、Activity

public class ArrayAdapterActivity extends Activity {

ListView lv ;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv = (ListView) findViewById(R.id.listView1);

String[] arr ={"aaa","bbb","ccc","ddd","eee"};

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);

lv.setAdapter(arrayAdapter);

}

}

simple_list_item_1是android系统提供的一个layout,每个列表项都是一个普通的TextView

三、自定义多控件的行布局:
1、效果
2、行布局文件custom_list.xml(activity_main.xml同前)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="20dp"

android:layout_marginTop="20dp"

android:src="@drawable/libai" />

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@+id/textView1"

android:layout_marginLeft="16dp"

android:layout_toRightOf="@+id/textView1"

android:textSize="20dp"/>

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:paddingLeft="20dp"

android:layout_below="@+id/imageView1"

android:text="TextView" />

</RelativeLayout>

3、 Activity

public class MainActivity extends Activity {

private String[] names = new String[]{"虎头","李白","弄玉","清照"};

private int[] imagesIds = new int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};

private String[] contents = new String[]{

"很可爱的小男孩的名字",

"唐代著名诗人,有一首诗,叫举头望明月,啊,故乡。。。。",

"一个小小女孩",

"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。"

};

ListView lv ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv = (ListView)findViewById(R.id.listView1);

//LayoutInflater inflater = getLayoutInflater();

BaseAdapter ada = new BaseAdapter() {

//获取每一行的布局

@Override

public View getView(int position, View view, ViewGroup viewGroup) {

// TODO Auto-generated method stub

//获取行布局

LayoutInflater inflater = getLayoutInflater();

RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.custom_list, null);

//行布局中的控件

ImageView image = (ImageView)layout.findViewById(R.id.imageView1);

TextView  tv1 = (TextView)layout.findViewById(R.id.textView1);

TextView  tv2 = (TextView)layout.findViewById(R.id.textView2);

//更新行布局中的内容

image.setImageResource(imagesIds[position]);

tv1.setText(names[position]);

tv2.setText(contents[position]);

return layout;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return names.length;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

};

lv.setAdapter(ada);

}

}

四、使用SimpleAdapter来实现上面的ListView的布局
SimpleAdapter构造方法需要5个参数:

Public Constructors


public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Added in API level 1

Constructor

Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

public class MainActivity extends Activity {

private String[] names = new String[]{"虎头","李白","弄玉","清照"};

private int[] imagesIds = new int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};

private String[] contents = new String[]{

"很可爱的小男孩的名字",

"唐代著名诗人,有一首诗,叫举头望明月,啊,故乡。。。。",

"一个小小女孩",

"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。"

};

ListView lv ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv = (ListView)findViewById(R.id.listView1);

List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();

for (int i = 0; i < names.length; i++) {

Map<String,Object> listItem = new HashMap<String,Object>();

listItem.put("header", imagesIds[i]);

listItem.put("personName", names[i]);

listItem.put("content",contents[i]);

listItems.add(listItem);

}

SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.custom_list,

new String[]{"personName","header","content"},

new int[]{R.id.name,R.id.header,R.id.content});

lv.setAdapter(adapter);

}

}

五、ListView事件
OnItemClick

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

Added in API level 1

Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.

Item长按事件

public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)

Added in API level 1

Callback method to be invoked when an item in this view has been clicked and held. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent The AbsListView where the click happened
view The view within the AbsListView that was clicked
position The position of the view in the list
id The row id of the item that was clicked
Returns
  • true if the callback consumed the long click, false otherwise

ListView详解 (ListView图文混排)相关推荐

  1. Flutter ListView详解

    ListView详解 ListView常用构造 ListView ListView 默认构建 效果 ListView ListTile ListTile 属性 ListTile 使用 效果 ListV ...

  2. 利用ListView实现新闻客户端的新闻内容图文混排

    如图: 布局文件: <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/a ...

  3. ListView异步加载图片,完美实现图文混排

    昨天参加一个面试,面试官让当场写一个类似于新闻列表的页面,文本数据和图片都从网络上获取,想起我还没写过ListView异步加载图片并实现图文混排效果的文章,so,今天就来写一下,介绍一下经验. Lis ...

  4. 实现图文混排ListView展示 ---- Android版

    使用eclipse创建一个Android Application  创建完成之后,开始java代码的编写 声明变量 //listview列表 private ListView listView; // ...

  5. android 实现表格横向混动_Android图文混排实现方式详解

    在使用TextView的时候,我们经常需要在TextView中进行图文混排,比如在QQ中聊天的消息中的表情,底部tab图标等. 一.场景 二.实现方式 Android官方对TextView的图文混排提 ...

  6. android ListView详解,你不知道的事 (后附焦点解决方法)

    对于android开发者来说,ListView无疑是最为常见的一个控件之一,android系统给我们提供了一些ArrayAdapter(文字),SimpleAdapter(图文),CursorAdap ...

  7. python 输出结果图文混排_div css图文混排列表设计中的基础问题总结

    最近业务需要,想设计一个比较通用的图文混排的列表.结果设计的过程中遇到了不少问题,虽然都是一些css设计中比较基础的问题,但是自己认为有必要总结下,希望可以帮到一些css设计的初学者,同时也想扩大下自 ...

  8. 计算机应用基础word说课,全国“XX杯”说课大赛计算机应用基础类优秀作品:Word图文混排教案.doc...

    全国"XX杯"说课大赛计算机应用基础类优秀作品:Word图文混排教案.doc 文档编号:1054624 文档页数:5 上传时间: 2020-05-30 文档级别:精品资源 文档类型 ...

  9. iOS火焰动画效果、图文混排框架、StackView效果、偏好设置、底部手势等源码

    iOS精选源码 高性能图文混排框架,构架顺滑的iOS应用. 使用OpenGLE覆盖阿尔法通道视频动画播放器视图. 可选最大日期截至当日日期的日期轮选器ChooseDatePicker 简单轻量的图片浏 ...

最新文章

  1. OutputFormat接口实现类
  2. Compound Interest Calculator4.0
  3. 【android】系统属性=(属性服务,属性文件,白名单)
  4. 第一台定制商用NAS存储服务器
  5. 【渝粤教育】电大中专工程图学基础 (3)作业 题库
  6. vCenter Server Appliance 所需的端口
  7. Android 仿美团网,大众点评购买框悬浮效果之修改版
  8. qmc0文件怎么转换mp3_音频转换器哪个好 怎么剪切MP3音频制作手机铃声
  9. 43.django中form组件
  10. [翻译]Popfly系列课程7 –深入幕后:使用 Popfly学习XML的初学者指南
  11. 数据库SQL语句总结大全
  12. openbci脑电帽3d打印文件下载
  13. 机器学习面试题(上)
  14. 关于awk 中如何使用 if条件判断句
  15. Linux shadow文件
  16. 读书的作用--引用《儒道至圣》小说章节
  17. 计算机软考答题卡填涂格式,软考填涂答题卡(纸)须知
  18. python星号直角三角形边长公式_直角三角形求边长公式图解
  19. 直观理解深度学习中的反卷积、导向反向传播
  20. NLP 语义相似度常用计算方法总结

热门文章

  1. 【Android 异步操作】线程池 ( 线程池 reject 拒绝任务 | 线程池 addWorker 添加任务 )
  2. 【Java 网络编程】NIO Buffer 简介 ( 概念 | 数据传输 | 标记 | 位置 | 限制 | 容量 | 标记 | 重置 | 清除 | 翻转 | 重绕 | 链式操作 )
  3. 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
  4. AsyncTask应用解析
  5. 利用Lucene.net搜索引擎进行多条件搜索的做法
  6. 爬虫基础-登陆github获取个人信息
  7. 深入Activity的作业完成
  8. 使用SqlBulkCopy数据导入和复制
  9. 动态二维数组赋值及for循环遍历和toString遍历
  10. 设计模式--装饰者模式