android用户界面-组件Widget-列表视图ListView

简介:

ListView是比较常用的控件

程序效果是实现一个ListView,ListView里面有标题,内容和图片,并加入点击和长按响应。

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据 具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

 

SimpleAdapter的例子如下:

首先在xml里面定义一个ListView

 

 
  1. 1.<?xml version="1.0" encoding="utf-8"?>    
  2.  2. <LinearLayout      
  3.  3.    android:id="@+id/LinearLayout01"      
  4.  4.    android:layout_width="fill_parent"      
  5.  5.    android:layout_height="fill_parent"      
  6.  6.    xmlns:android="http://schemas.android.com/apk/res/android">    
  7.  7. <ListView android:layout_width="wrap_content"      
  8.  8.          android:layout_height="wrap_content"      
  9.  9.          android:id="@+id/ListView01"    
  10.  10.          />    
  11.  11. </LinearLayout>    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	android:id="@+id/LinearLayout01" 
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" 
	xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:id="@+id/ListView01"
          />
</LinearLayout>

 

定义ListView每个条目的Layout,用RelativeLayout实现:

 

 
  1. 1.<?xml version="1.0" encoding="utf-8"?>    
  2.  2.<RelativeLayout      
  3.  3.    android:id="@+id/RelativeLayout01"      
  4.  4.    android:layout_width="fill_parent"      
  5.  5.    xmlns:android="http://schemas.android.com/apk/res/android"      
  6.  6.    android:layout_height="wrap_content"      
  7.  7.    android:paddingBottom="4dip"      
  8.  8.    android:paddingLeft="12dip"    
  9.  9.    android:paddingRight="12dip">    
  10.  10.<ImageView      
  11.  11.    android:paddingTop="12dip"    
  12.  12.    android:layout_alignParentRight="true"    
  13.  13.    android:layout_width="wrap_content"      
  14.  14.    android:layout_height="wrap_content"      
  15.  15.    android:id="@+id/ItemImage"    
  16.  16.    />      
  17.  17.<TextView      
  18.  18.    android:text="TextView01"      
  19.  19.    android:layout_height="wrap_content"      
  20.  20.    android:textSize="20dip"      
  21.  21.    android:layout_width="fill_parent"      
  22.  22.    android:id="@+id/ItemTitle"    
  23.  23.    />    
  24.  24.<TextView      
  25.  25.    android:text="TextView02"      
  26.  26.    android:layout_height="wrap_content"      
  27.  27.    android:layout_width="fill_parent"      
  28.  28.    android:layout_below="@+id/ItemTitle"      
  29.  29.    android:id="@+id/ItemText"    
  30.  30.    />    
  31.  31.</RelativeLayout>    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
	android:id="@+id/RelativeLayout01" 
	android:layout_width="fill_parent" 
	xmlns:android="http://schemas.android.com/apk/res/android" 
	android:layout_height="wrap_content" 
	android:paddingBottom="4dip" 
	android:paddingLeft="12dip"
	android:paddingRight="12dip">
<ImageView 
	android:paddingTop="12dip"
	android:layout_alignParentRight="true"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:id="@+id/ItemImage"
	/> 
<TextView 
    android:text="TextView01" 
    android:layout_height="wrap_content" 
    android:textSize="20dip" 
    android:layout_width="fill_parent" 
    android:id="@+id/ItemTitle"
    />
<TextView 
	android:text="TextView02" 
	android:layout_height="wrap_content" 
	android:layout_width="fill_parent" 
	android:layout_below="@+id/ItemTitle" 
	android:id="@+id/ItemText"
	/>
</RelativeLayout>

 

最后在Activity里面调用和加入Listener,具体见注释:

 

 
  1. 1.package com.ray.test;     
  2.  2.    
  3.  3.import java.util.ArrayList;     
  4.  4.import java.util.HashMap;     
  5.  5.    
  6.  6.import android.app.Activity;     
  7.  7.import android.os.Bundle;     
  8.  8.import android.view.ContextMenu;     
  9.  9.import android.view.MenuItem;     
  10.  10.import android.view.View;     
  11.  11.import android.view.ContextMenu.ContextMenuInfo;     
  12.  12.import android.view.View.OnCreateContextMenuListener;     
  13.  13.import android.widget.AdapterView;     
  14.  14.import android.widget.ListView;     
  15.  15.import android.widget.SimpleAdapter;     
  16.  16.import android.widget.AdapterView.OnItemClickListener;     
  17.  17.    
  18.  18.public class TestListView extends Activity {     
  19.  19.    @Override    
  20.  20.    public void onCreate(Bundle savedInstanceState) {     
  21.  21.        super.onCreate(savedInstanceState);     
  22.  22.        setContentView(R.layout.main);     
  23.  23.        //绑定Layout里面的ListView     
  24.  24.        ListView list = (ListView) findViewById(R.id.ListView01);     
  25.  25.             
  26.  26.        //生成动态数组,加入数据     
  27.  27.        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();     
  28.  28.        for(int i=0;i<10;i++)     
  29.  29.        {     
  30.  30.            HashMap<String, Object> map = new HashMap<String, Object>();     
  31.  31.            map.put("ItemImage", R.drawable.checked);//图像资源的ID     
  32.  32.            map.put("ItemTitle""Level "+i);     
  33.  33.            map.put("ItemText""Finished in 1 Min 54 Secs, 70 Moves! ");     
  34.  34.            listItem.add(map);     
  35.  35.        }     
  36.  36.        //生成适配器的Item和动态数组对应的元素     
  37.  37.        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源      
  38.  38.            R.layout.list_items,//ListItem的XML实现     
  39.  39.            //动态数组与ImageItem对应的子项             
  40.  40.            new String[] {"ItemImage","ItemTitle""ItemText"},      
  41.  41.            //ImageItem的XML文件里面的一个ImageView,两个TextView ID     
  42.  42.            new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}     
  43.  43.        );     
  44.  44.            
  45.  45.        //添加并且显示     
  46.  46.        list.setAdapter(listItemAdapter);     
  47.  47.             
  48.  48.        //添加点击     
  49.  49.        list.setOnItemClickListener(new OnItemClickListener() {     
  50.  50.    
  51.  51.            @Override    
  52.  52.            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,     
  53.  53.                    long arg3) {     
  54.  54.                setTitle("点击第"+arg2+"个项目");     
  55.  55.            }     
  56.  56.        });     
  57.  57.             
  58.  58.      //添加长按点击     
  59.  59.        list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {     
  60.  60.                 
  61.  61.            @Override    
  62.  62.            public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {     
  63.  63.                menu.setHeaderTitle("长按菜单-ContextMenu");        
  64.  64.                menu.add(000"弹出长按菜单0");     
  65.  65.                menu.add(010"弹出长按菜单1");        
  66.  66.            }     
  67.  67.        });      
  68.  68.    }     
  69.  69.         
  70.  70.    //长按菜单响应函数     
  71.  71.    @Override    
  72.  72.    public boolean onContextItemSelected(MenuItem item) {     
  73.  73.        setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");      
  74.  74.        return super.onContextItemSelected(item);     
  75.  75.    }     
  76.  76.}    

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080693
相关文章
|
21天前
|
存储 设计模式 数据库
构建高效的安卓应用:探究Android Jetpack架构组件
【4月更文挑战第20天】 在移动开发的世界中,构建一个既高效又可维护的安卓应用是每个开发者追求的目标。随着Android Jetpack的推出,Google为开发者提供了一套高质量的库、工具和指南,以简化应用程序开发流程。本文将深入探讨Jetpack的核心组件之一——架构组件,并展示如何将其应用于实际项目中,以提升应用的响应性和稳定性。我们将通过分析这些组件的设计原则,以及它们如何协同工作,来揭示它们对于构建现代化安卓应用的重要性。
|
1月前
|
Android开发
Android四大组件详解2
Android四大组件详解
26 1
|
2天前
|
Java 开发工具 Android开发
如何在Eclipse中查看Android源码或者第三方组件包源码(转)
如何在Eclipse中查看Android源码或者第三方组件包源码(转)
12 4
|
12天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。
|
2天前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
|
2天前
|
Android开发
Android教程之Android 用户界面-表格视图(GridView)
Android教程之Android 用户界面-表格视图(GridView)
|
14天前
|
机器学习/深度学习 搜索推荐 Android开发
【专栏】构建高效安卓用户界面的指南,分为设计原则和技巧两部分
【4月更文挑战第27天】本文介绍了构建高效安卓用户界面的指南,分为设计原则和技巧两部分。设计原则包括一致性、简洁性和可访问性,强调遵循安卓系统规范,保持界面简洁,考虑不同用户需求。技巧方面,建议合理布局、优化图标和图片、运用动画效果、提供个性化设置及优化性能。随着技术发展,未来安卓应用开发将融合更多智能化和个性化元素,开发者需持续学习新技术,提升用户体验。
|
17天前
|
数据库 Android开发 开发者
安卓应用开发:构建高效用户界面的策略
【4月更文挑战第24天】 在竞争激烈的移动应用市场中,一个流畅且响应迅速的用户界面(UI)是吸引和保留用户的关键。针对安卓平台,开发者面临着多样化的设备和系统版本,这增加了构建高效UI的复杂性。本文将深入分析安卓平台上构建高效用户界面的最佳实践,包括布局优化、资源管理和绘制性能的考量,旨在为开发者提供实用的技术指南,帮助他们创建更流畅的用户体验。
|
21天前
|
设计模式 前端开发 数据库
构建高效Android应用:使用Jetpack架构组件实现MVVM模式
【4月更文挑战第21天】 在移动开发领域,构建一个既健壮又易于维护的Android应用是每个开发者的目标。随着项目复杂度的增加,传统的MVP或MVC架构往往难以应对快速变化的市场需求和复杂的业务逻辑。本文将探讨如何利用Android Jetpack中的架构组件来实施MVVM(Model-View-ViewModel)设计模式,旨在提供一个更加模块化、可测试且易于管理的代码结构。通过具体案例分析,我们将展示如何使用LiveData, ViewModel, 和Repository来实现界面与业务逻辑的分离,以及如何利用Room数据库进行持久化存储。最终,你将获得一个响应迅速、可扩展且符合现代软件工
24 0
|
25天前
|
Android开发 开发者
什么是Android Jetpack,它包括哪些组件?
【4月更文挑战第17天】Android Jetpack是Google提供的一套工具集,助力开发者高效、稳定地开发Android应用。它包含架构、UI、行为和基础组件,简化了后台任务、导航和生命周期管理,使开发者能专注于创新。随着不断更新,如CameraX的推出,掌握Jetpack对开发者面试和工作至关重要。
22 0