ListView的一些用法
Adapter用于实现要显示的列表,复杂的可以重写BaseAdapter提供的getView方法,简单的就用SimpleAdapeter(this,要显示的list,布局文件,提取list中Map的Key对应的value,布局中的那些控件作为列表项)
方法一:
让该activityentendsListActivity,并且用SimpleAdapter{
// 生成一个List对象,并按照SimpleAdapter的标准,将bean当中的数据添加到List当中去
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info) iterator.next();
//利用哈希map存储对应的键值
HashMap<String, String> map = new HashMap<String, String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("mp3_size", mp3Info.getMp3Size());
//并将map存入list
list.add(map);
}
// 创建一个SimpleAdapter对象
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },
new int[] { R.id.mp3_name, R.id.mp3_size });
// 将这个SimpleAdapter对象设置到ListActivity当中,则会显示在控件当中
setListAdapter(simpleAdapter);
可以新建一个xml,用于注册要显示内容的控件,R.id.mp3_name,R.id.mp3_size
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip"
>
<TextView android:id="@+id/mp3_name"
android:layout_height="30dip"
android:layout_width="180dip"
android:textSize="10pt"/>
<TextView android:id="@+id/mp3_size"
android:layout_height="30dip"
android:layout_width="180dip"
android:textSize="10pt"/>
</LinearLayout>
方法二:
制作漂亮的ListView
在xml中制作按钮,或者ListItem按下的效果
新建一个listview-selected.xml,在里面填写pressed的时候引用的图片。
在ListView的item项目布局中调用listView-selected
具体提代码:
按钮的状态:
我们一般搞UI设计,按钮通常有三个状态:normal(正常状态);focus(焦点状态),pressed(按下状态)。如下图所示:
![915.jpg 915.jpg]()
我们会在res/drawable目录下定义一个资源文件,比如我们本例中要用到的handle.xml,在里面定义三种状态,每种状态对应一张图片:
代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_window_focused="false" android:drawable="@drawable/handle_normal" />
- <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
- <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
- </selector>
-
复制代码
而我们使用这个资源文件的用法只需要引用drawable里的资源文件(android:background="@drawable/handle")代码如下:
- [/backcolor][/color][/align]
- [align=left][color=rgb(51,51,51)][backcolor=rgb(255,255,255)]<Button
- android:id="@+id/handle"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="@drawable/handle"
- /> [/backcolor][/color][/align]
- [align=left][color=rgb(51,51,51)][backcolor=rgb(255,255,255)]
复制代码
Android中的层:
看过《盗梦空间》的人都知道,梦境有多少层,而Android中也有层次之分,在Android中第一层"梦境",我们可以认为是壁纸。第二层就是应用的Activity,第三层就是放在Activity上的容器(ViewGroup以及它的子类FrameLayout,LinearLayout等布局对象),当然容器中还可以放容器,你也可以放到N层(最多放多少我还没验证过),总之最后一层就是那些继承于View的控件了(诸如,Button,TextView等.)
而ListView以及GridView中UI是怎么设计的呢,下面我们看一下效果图:
![454.jpg 454.jpg]()
上图是一个ListView的效果图,正常状态下是白色背景黑色字体,当我们点击一列时会出现黄色背景。这一效果是如何做到的呢?
ListView单元格显示的内容其实是我们事先定义在Layout目录下的一个布局文件,从这个效果来看,我们可以看出它一共有三个“层”
第一层容器(LinearLayout) 背景色为白色:
第二层也是容器(LinearLayout)当按下时,背景色为黄色,把第一层挡住(具体做法可以参照按钮):
第三层是控件(TextView)。
实例:
上面说了一些,有些人肯定会云里雾里,所以我们直接来个实例,实例做完后,再看一下,效果会更好,大家按照步骤跟我来:
第一步:首先准备素材,准备三个按钮,以及ListView的背景图(上面三个按钮已经有了,下面我只贴一个ListView背景图片):
![16.jpg 16.jpg]()
第二步:新建一个Android工程,命名为UIDemo.目录结构如下图所示:
![734.jpg 734.jpg]()
第三步:在res目录下新建一个drawable文件夹,定义两个资源文件一个是handle.xml另一个为listview_selected.xml,其中handle.xml代码已经在上面贴出,listview_selected.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]">
- <item android:state_pressed="true" android:drawable="@drawable/list_selector_background_pressed" />
- </selector>
-
复制代码
第四步:修改main.xml布局文件,这里我用到了SliddingDrawer控件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <SlidingDrawer
- android:id="@+id/slidingdrawer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- android:handle="@+id/handle"
- android:content="@+id/content">
- <Button
- android:id="@+id/handle"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="@drawable/handle"
- />
- <ListView
- android:id="@+id/content"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </SlidingDrawer>
- </LinearLayout>
-
复制代码
我们这里用到了ListView控件,而我们ListView控件显示的内容我事先在layout目录下定义两个TextView,命名为listview_layout.xml,代码如下(这里有三层哦!):
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/listview_selected"
- android:padding="6px"
- >
- <TextView
- android:id="@+id/bookname"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="20px"
- android:textColor="#000000"
- />
- <TextView
- android:id="@+id/author"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16px"
- android:textColor="#000000"
- />
- </LinearLayout>
- </LinearLayout>
-
复制代码
第五步:修改主核心程序UIDemo.java,代码如下:
- package com.tutor.uidemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- public class UIDemo extends Activity {
- private ListView mListView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setupViews();
- }
- private void setupViews(){
- mListView = (ListView)findViewById(R.id.content);
- mListView.setAdapter(new ListViewAdapter());
- }
- private class ListViewAdapter extends BaseAdapter{
- //这里返回10行,ListView有多少行取决于getCount()方法
- public int getCount() {
- return 10;
- }
- public Object getItem(int arg0) {
- return null;
- }
- public long getItemId(int arg0) {
- return 0;
- }
- public View getView(int position, View v, ViewGroup parent) {
- final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
- if(v == null){
- v = inflater.inflate(R.layout.listview_layout, null);
- }
- TextView mBookName = (TextView)v.findViewById(R.id.bookname);
- TextView mBookAuthor = (TextView)v.findViewById(R.id.author);
- mBookName.setText("Android傻瓜教程" + position);
- mBookAuthor.setText("Frankiewei" + position);
- return v;
- }
- }
- }
-
复制代码
第六步:运行上述工程,查看效果:
运行效果1:
![345.jpg 345.jpg]()
点击按钮效果2:
ListView正常效果3:
ListView正常效果4:
![513.jpg 513.jpg]()
分享到:
相关推荐
在Android开发中,ListView是一种常用的组件,用于展示大量的数据列表,具有良好的滚动性能和可定制性。本教程将深入探讨如何实现ListView,并在点击列表项时跳转到详细页面。 一、ListView的基本使用 1. 创建...
本篇文章将深入探讨如何在Android中编写ListView列表选项栏,包括使用系统默认的ListView以及自定义multiple ListView的方法。 首先,ListView的数据绑定通常通过适配器(Adapter)来实现。适配器主要有三种类型:...
在Android开发中,ListView是一种非常常见的控件...以上就是关于如何在Android中实现ListView列表视图以及点击列表项进行界面跳转的基本步骤。通过不断实践和优化,你将能创建出功能丰富且用户体验良好的ListView应用。
在Android开发中,ListView是一种常用的视图组件,用于展示大量数据列表。当需求涉及列表中的每个条目本身也需要展示一个子列表时,我们就会遇到ListView的嵌套问题。本教程将详细讲解如何在Android Studio环境下...
总的来说,ListView列表框是Windows Forms开发中的一个强大工具,提供了丰富的功能和灵活性,能满足各种数据展示和用户交互的需求。通过实践和探索,开发者可以充分利用它的潜力,提升应用程序的功能和用户体验。
本文实例为大家分享了Android ListView实现图文列表显示的具体代码,供大家参考,具体内容如下 目标效果: ListView如果内容过多,可以滑动屏幕来显示,并且点击某一行可使用吐司方法弹出对应的水果名字。 1....
在Android开发中,ListView是一种常用的控件,用于展示可滚动的多行数据列表。而“listview圆角列表demo”则是针对ListView进行的一种定制化设计,使得ListView的每一项(item)显示为具有圆角的矩形,以提升界面的...
本教程将详细讲解如何在ListView中更新数据并利用`notifyDataSetChanged()`方法来刷新列表视图。 首先,我们需要创建一个ListView的基本结构。在布局文件(如activity_main.xml)中,我们需要添加一个ListView控件...
在Android开发中,ListView是展示大量数据的一种常见视图组件,尤其适用于菜单、选项列表等场景。本资源提供了一个完整的Android应用示例,专门讲解如何实现ListView及其交互功能。 首先,ListView的基础在于...
`ListAdapter`类需要继承自`BaseAdapter`,并重写其中的`getView()`方法,该方法用于根据数据项创建或复用列表项视图: ```java @Override public View getView(int position, View convertView, ViewGroup parent)...
本项目“安卓高仿qq的listview列表”旨在模仿QQ应用的ListView设计,它不仅包含了基本的列表展示功能,还集成了进阶特性如上拉加载更多、下拉刷新以及滑动删除,从而提升用户体验。 1. **ListView基础**: - ...
动态添加和删除ListView列表项是提高用户交互性和应用灵活性的关键技术。本篇将深入讲解如何通过自定义Adapter来实现这一功能,并清空整个ListView。 首先,理解ListView的工作原理至关重要。ListView依赖于Adapter...
在Android开发中,ListView是一种常用的控件,用于展示可滚动的列表数据。当需要在一个ListView中播放多个视频时,这通常涉及到自定义Adapter、优化性能和处理播放状态等多个技术要点。下面将详细讲解如何实现这一...
ListViewAnimation库则是对原生ListView的功能扩展,使得开发者可以轻松地为ListView添加各种进阶动画效果。 1. **ListView Animation原理** ListView Animation主要通过自定义Adapter和OverScrollBy方法来实现。...
在Android开发中,ListView是一种常用的视图组件,用于展示大量数据列表。然而,在某些复杂的界面设计中,我们可能需要在一个ListView中嵌套另一个ListView来实现更丰富的交互效果,比如折叠列表。这种技术常用于...
在Android开发中,ListView是一种常见的UI组件,用于展示大量数据列表。当数据具有层次结构时,我们可以使用"分组ListView"来更清晰地组织信息。本文将深入探讨分组ListView的概念、实现方式以及如何实现滑动查看...
【Flutter】ListView 列表 ( List 集合的 map 方法说明 | 垂直列表 | 水平列表 | 代码示例 ) https://hanshuliang.blog.csdn.net/article/details/119932936 博客源码快照
本教程“045集-ListView列表项点击事件”深入讲解了如何处理ListView中的条目点击事件,这对于构建交互式的Android应用至关重要。在本教程中,我们将探讨以下几个关键知识点: 1. **ListView的基本使用**:首先,你...
在Android开发中,自定义的ListView结合RadioButton显示对话框列表数据是一种常见的用户界面设计,用于在弹出的对话框中展示一系列可选的列表项。这样的设计能够为用户提供清晰的选择方式,同时保持屏幕的整洁。以下...
在C#编程环境中,ListView控件是用于展示数据列表的常用组件,通常显示文本或图标。然而,标准的ListView控件可能无法满足某些特定需求,比如在每个列表项中嵌入其他控件,如按钮、复选框或者更复杂的用户界面元素。...