`
txf2004
  • 浏览: 6925335 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android ApiDemos示例解析(119):Views->Gallery->1. Photos

 
阅读更多

Gallery 和 ListView ,Spinner (下拉框) 用一个共同点,它们都是AdapterView的子类。AdapterView的显示可以通过数据绑定来实现,数据源可以是数组或是数据库记录,数据源和AdapterView是通过Adapter作为桥梁。通过Adapter,AdatperView可以显示数据源或处理用户选取事件,如:选择列表中某项。

Gallery 水平显示一个列表,并且将当前选中的列表项居中显示。常用来显示一组图片。 更一般来说Adapter 的 getView 可以返回任意类型的View。(如TextView)。

本例通过扩展BaseAdapter 创建一个 ImageAdapter,用于显示一组图片,图片存放在/res/drawable 目录下,对应的资源ID如下:

private Integer[] mImageIds = {
 R.drawable.gallery_photo_1,
 R.drawable.gallery_photo_2,
 R.drawable.gallery_photo_3,
 R.drawable.gallery_photo_4,
 R.drawable.gallery_photo_5,
 R.drawable.gallery_photo_6,
 R.drawable.gallery_photo_7,
 R.drawable.gallery_photo_8
};


这个Adpater的getView定义如下,返回一个ImageView:

public View getView(int position, View convertView,
 ViewGroup parent) {
 ImageView i = new ImageView(mContext);

 i.setImageResource(mImageIds[position]);
 i.setScaleType(ImageView.ScaleType.FIT_XY);
 i.setLayoutParams(new Gallery.LayoutParams(136, 88));

 // The preferred Gallery item background
 i.setBackgroundResource(mGalleryItemBackground);

 return i;
}


为Gallery 设置数据源:

// Reference the Gallery view
Gallery g = (Gallery) findViewById(R.id.gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));

此外,应用提供registerForContextMenu 为Gallery添加一个Context Menu,可以参见Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter

分享到:
评论

相关推荐

    Android ApiDemos示例解析(26):App->Notification->IncomingMessage

    NULL 博文链接:https://kakukemeit.iteye.com/blog/1806388

    android apidemos示例解析

    android apidemos代码的示例解析,讲的挺全的

    Android ApiDemos apk

    Android ApiDemos apk, 包括Android presentation双屏异显 demon, App>Activity>Presentation

    Android ApiDemos4.4 示例解析

    最新版ApiDemos Android SDK 中带有很多例子,其中ApiDemo 详细介绍了Android 平台主要API,分成了 · App · Content · Graphics · Media · OS · Text · Views 几个大类,每个大类又分为几个小类,...

    android1.6 apiDemos

    《Android 1.6 API Demos深度解析》 在Android开发的世界中,API Demos是一个不可或缺的学习资源,它为开发者提供了丰富的示例代码,帮助理解并掌握Android API的各种功能。本篇文章将深入探讨"android1.6 apiDemos...

    Android6.0 ApiDemos.apk

    从官方预览包里提取的Android6.0 ApiDemos.apk,方便安装在真机上查看实例的实际效果。

    jdk-8u202-linux-x64-demos.tar.gz

    demos :jdk-8u202-linux-x64-demos.tar.gz

    最新Android apidemos

    最新Android apidemos

    jdk-8u202-linux-arm64-vfp-hflt-demos.tar.gz

    demos :jdk-8u202-linux-arm64-vfp-hflt-demos.tar.gz

    Android2.2 ApiDemos

    《Android 2.2 ApiDemos深度解析》 在Android开发领域,ApiDemos是一个非常重要的参考资料,它是由Google官方提供的一个示例程序,包含了Android SDK中的各种API功能的演示。这个项目,针对的是Android 2.2(API...

    FairyGUI-Unity-Plugin-3.4.0.zip

    Run demos in Assets/FairyGUI/Examples/Scenes. The UI project is in Examples-UIProject.zip, unzip it anywhere. Use FairyGUI Editor to open it. Using FairyGUI in Unity: * Place a UIPanel in scene by ...

    android的ApiDemos

    API Demos 是 Google 为了 Android 开发者所提供的一个 Android API 合集,其中包含了很多的 API 范例,同时遵循了良好的代码规范,是一个值得开发者研究和学习的典型。android的ApiDemos,需要解压缩后使用。

    android ApiDemos

    android APIdemos,android 源码简单使用demos

    jdk-8u231-solaris-x64-demos.tar.gz

    文件名称: jdk-8u231-solaris-x64-demos.tar.gz 文件大小: 8.43 MB (8,846,115 字节) MD5: 3b62803c4e86b40f1a83b68be7bce224 SHA1: 3aba5de2157163b949179a5b654494da05b4da9c SHA256: 774c5d41a1422d22d026eaaa39...

    Android ApiDemos2.1

    `Android ApiDemos 2.1` 是Android SDK中一个非常重要的示例程序,它包含了Android API的各种功能和组件的演示,是开发者学习和理解Android系统API的重要资源。这个版本对应的是Android 2.1(API级别7)的时代,虽然...

    Microsoft.Premium.DP-300.by-.VCEup-.82q-DEMOS.pdf

    Microsoft.Premium.DP-300.by_.VCEup_.82q-DEMOS.pdf

    Android 5.1 ApiDemos.apk

    Android 5.1的ApiDemos安装包

    LigerUI V1.1.0

    api ------------- API目录 demos ------------- 本地demos目录 lib ------------- 框架源代码目录 index.htm ------------- 主页面 loading.gif ------------- loading图标 welcome.htm ------------- 欢迎页面 ...

    red5-1.0.0-webapps-demos.zip

    r下载后,解压,把解压后的文件直接放入到red5webaaps下

    Android6.0 Api Demos

    该Api Demos 源码是基于Android6.0 ,Eclipse下的,可以直接使用Android studio 导入工程即可。

Global site tag (gtag.js) - Google Analytics