正常访问状态! 设为首页 | 加入收藏夹 | 浏览历史  
  http://www.guosp.com
 碧海澜涛居
  海纳百川,有容乃大。壁立千刃,无欲则刚!
 
 
关键词:
  网站首页 | 关于本站 | 技术资料 | 美文日志 | 读书收藏 | 影视收藏 | 软件收藏 | 摄影相册| 留言板 
  技术资料 >> Android技术 关闭(快捷键alt+C)
搜索标签: ListView 图文混排
ListView详解 (ListView图文混排)
[阅读次数:698次]  [发布时间:2013年12月3日]

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
ListView详解 - ppy2790@126 - ITAIRKnown Indirect Subclasses

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


ListView详解 - ppy2790@126 - ITAIR


三、自定义多控件的行布局:
1、效果
ListView详解 - ppy2790@126 - ITAIR
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>

ListView详解 - ppy2790@126 - ITAIR

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<, /FONT> 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
, style="LINE-HEIGHT: normal; MARGIN-TOP: 0px; FONT-FAMILY: Monaco; MARGIN-BOTTOM: 0px; COLOR: rgb(119,119,119); FONT-SIZE: 11px">@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[]{

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

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

"一个小小女孩",

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

<, SPAN style="WHITE-SPACE:, pre">};

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




本页地址: [复制地址]
该页内容非本站原创 收藏自:http://blog.163.com/ppy2790@126/blog/static/1032422412013731115643829/
返回顶部 关闭(快捷键alt+C)
评论统计(0条)| 我要评论
暂无评论内容!
我要评论 
我要评论: 带*部分需要填写
 姓名称呼: * 请填写您的姓名或呢称
联系方式: QQ,MSN,Email都可以,方便交流 (仅管理员可见)
 评论内容: * 不超过100字符,50汉字
验证码:
    
  推荐链接
  最近更新  
·Host 'XXX' is not allowed...
·Win2008或IIS7的文件上传大...
·IIS7.0上传文件限制的解决方...
·测试信息2015-03-11
·asp.net中处理图片
·ASP.NET之Web打印-终极解决...
·Asp.net下C#调用Word模版实...
·asp.net下将页面内容导入到...
·asp.net导出为pdf文件
·asp.net生成pdf文件
·FCKeditor 文本编辑器的使用...
·ASP.NET 将数据生成PDF
·asp.net2.0导出pdf文件完美...
·AspJpeg的安装与测试
·JS验证浏览器版本对IE11的支...
  热门浏览  
·IE8和IE9出现“此网页上的问...
·无线路由器密码破解,教你断...
·js替换所有回车换行符
·QQ/MSN在线交流代码
·如何取消键盘上的一些快捷键...
·IE弹出“中国工商银行防钓鱼...
·win7声音小的解决方法
·webdav漏洞的利用
·强制两端对齐的函数或者CSS...
·win7下成功安装sql server ...
·显示器分辨率调的过高导致电...
·天诺时空技术技术论坛
·js验证手机号码格式
·JS展开和收缩效果(二)
·本地计算机上的 MSSQLSERVE...
  碧海澜涛居
网站首页关于本站站长简介开发案例技术资料美文日志摄影相册读书收藏影视收藏留言板
版权所有:碧海澜涛 QQ:410436434 Email:shaopo_guo@163.com 苏ICP备15000526号
免责声明:本站为个人网站,站内所有文字、图片等各类资料均为个人兴趣爱好所收集,不用作任何商业用途,亦不保证资料的真实性,若有因浏览本站内容而导致的各类纠纷,本站也不承担任何责任。本站部分内容来自互联网,如有涉及到您的权益或隐私请联系站长解决。