Hello Views之Gallery(yaozq翻页,仅供参考)

简介: Gallery是用来将当前选择的图片在View中间显示,并水平滚动的一种布局组件。(Gallery is a layout widget used to display items in a horizontally scrolling list and positions the current selection at the center of the view。

Gallery是用来将当前选择的图片在View中间显示,并水平滚动的一种布局组件。(Gallery is a layout widget used to display items in a horizontally scrolling list and positions the current selection at the center of the view。)

在这个指导中,你将创建一个图片的gallery,并在每次gallery的项被选中的时候弹出一个toast。下面是具体步骤:

1,新建一个叫HelloGalleryAndroid项目。

2,找一些图片,并把这些图片保存到项目的res/drawable目录下。

3,打开res/layout/main.xml文件,并将下面的内容插入进去:

<?xml version="1.0" encoding="utf-8"?>
<Galley xmlns:android="http://schemas.android.com/apk/res/android">
android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

4,打开HelloGallery.java文件,在onCreate()方法中插入下面的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
  	  super.onCreate(savedInstanceState);
  	  setContentView(R.layout.main);

  	  Gallery gallery = (Gallery) findViewById(R.id.gallery);
  	  gallery.setAdapter(new ImageAdapter(this));

  	  gallery.setOnItemClickListener(new OnItemClickListener() {
 	       public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
        }
  	  });
}

这将把main.xml布局文件设为content view,并通过findViewById(int)得到布局文件中的Gallery对象。一个叫ImageAdapterBaseAdapter子类被实例化,并通过setAdapter()方法被用到Gallery对象上。(ImageAdapter将在后面定义。)然后一个匿名的AdapterView.OnItemClickListener被初始化。onItemClickAdapterView,View,int,long)回调方法接收AdapterView,这个AdapterView就是点击事件发生的地方,也是特定View接收点击、View被点击的位置、被点击项的行ID的地方。在这个例子中,所需要的数据就是得到点击发生的位置,然后使用一个Toast(通过调用makeText(Context, CharSequence, int)show()方法来使用)信息来说明点击的是哪个项。

5,在res/values目录下创建一个名为attrs.xml的新的XML文件,插入到如下的地方:

<?xml version='"1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemVackground" />
</declare-styleable>
</resources>

这是一个可以被运用到布局的定制的styleable资源。在这个例子中,它将被用到Gallery中的一个单独的items<attr>元素为styleable定义了一个明确的属性,并且在这个例子中,它引用了一个已经存在的平台属性:galleryItemBackground,这个平台属性为galleryitems定义了一个边界样式(a border styling)。在接下来的步骤中,你将看到这个属性是怎样被引用的和怎样被运用于gallery的每一个item中。

6,返回到HelloGallery.java文件。在onCreate(Bundle)方法之后,定义定制的ImageAdapter类:

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

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

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }

首先,有几个变量,包括在引用图片资源目录(res/drawable/)下图片的ID数组。

接下来就是,类的构造器,ImageAdapter对象的Context被定义,并需要上一步定义的styleable资源,并把这个资源保存到一个局部变量中。在构造器的末尾,recycle()方法被调用到TypedArray,这样的好处是TypedArray可以被系统循环利用。

getCount()getItem(int)getItemId(int)这些方法是为了简单查询Adapter必须要实现的方法。这个方法的作用就是把一张图片将被嵌入到Gallery的一个{@link android.widget.ImageView中。在这个方法中,成员变量Context是用来创建一个ImageView。在申请了drawable资源数组中的图片、设置Gallery.LayoutParams的图片的高宽、设置填充ImageView的尺寸、设置背景来使用这个构造器需要的styleable属性之后,ImageView便就绪了。

7,运行程序。


相关文章
|
5月前
|
XML Java Android开发
Android Studio App开发之翻页视图ViewPager的讲解及实战(附源码 包括翻页视图和翻页标签栏)
Android Studio App开发之翻页视图ViewPager的讲解及实战(附源码 包括翻页视图和翻页标签栏)
145 0
|
容器
Flutter 108: 图解 PageView 滑动页面预览小尝试
0 基础学习 Flutter,第一百零八步:学习一下 PageView 的基本应用!
1157 1
|
小程序 前端开发 定位技术
【小程序】view视图,swiper轮播图,scroll-view滑动列表 (在线详细手册)
【小程序】view视图,swiper轮播图,scroll-view滑动列表 (在线详细手册)
【小程序】view视图,swiper轮播图,scroll-view滑动列表 (在线详细手册)
|
中间件
Axure实战09:创建一个NavigationPage导航页网站
Axure实战09:创建一个NavigationPage导航页网站
339 0
Axure实战09:创建一个NavigationPage导航页网站
|
中间件 iOS开发
Axure实战12:创建一个GuidePage引导页示例
Axure实战12:创建一个GuidePage引导页示例
267 0
Axure实战12:创建一个GuidePage引导页示例
|
C#
C# PDF Page操作——设置页面切换按钮
概述 在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置按钮跳转到指定页面。两种方法适应不同的程序设计需要,可自行选择合适的添加方法。
1442 0
|
缓存 Android开发
Android:随笔——对页面的View进行截图
转载请标明地址 QuincySx:[http://www.jianshu.com/p/71309b2bd0e7] 我们在做项目时,往往有一个这样的需求:就是对视图的一部分进行截图然后分享出去 这个功能很简单还是简单的看代码吧 ...
1000 0