前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android从零单排系列二十四】《Android视图控件——Gallery》

【Android从零单排系列二十四】《Android视图控件——Gallery》

作者头像
再见孙悟空_
发布2023-07-17 20:28:16
2050
发布2023-07-17 20:28:16
举报

前言

小伙伴们,在上文中我们介绍了Android视图组件PopupWindow,本文我们继续盘点,介绍一下视图控件的Gallery。

注:Gallery在API29中已被弃用。

一 Gallery基本介绍

二 Gallery使用方法

1.在XML布局文件中添加Gallery控件:

代码语言:javascript
复制
<Gallery
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

2.在Java代码中获取Gallery对象并设置适配器:

代码语言:javascript
复制
Gallery gallery = findViewById(R.id.gallery);
GalleryAdapter adapter = new GalleryAdapter(context, images); // 自定义适配器
gallery.setAdapter(adapter);

3.编写自定义适配器(GalleryAdapter)以提供数据和视图绑定:

代码语言:javascript
复制
public class GalleryAdapter extends BaseAdapter {
    private Context context;
    private List<Integer> images;

    public GalleryAdapter(Context context, List<Integer> images) {
        this.context = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object getItem(int position) {
        return images.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(images.get(position));
        return imageView;
    }
}

4.设置Gallery的监听器以响应用户操作:

代码语言:javascript
复制
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // 处理选中项的操作
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // 处理没有选中项的操作
    }
});

5.为了提升性能和滑动流畅度,你可以对Gallery进行进一步的定制和优化,例如添加缓存、优化视图重用等。

三 Gallery常见属性及方法

常见属性:

  1. android:animationDuration:指定图片切换时的动画持续时间。
  2. android:gravity:设置图片在Gallery中的位置(例如居中、左对齐、右对齐等)。
  3. android:spacing:设置相邻图片之间的间距。
  4. android:unselectedAlpha:设置非当前选中图片的透明度。

常见方法:

  1. setAdapter(SpinnerAdapter adapter):设置Gallery的适配器,用于提供数据和视图。
  2. setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener):设置监听器,以便在Gallery中的项被选中时触发回调。
  3. setOnItemClickListener(AdapterView.OnItemClickListener listener):设置监听器,以便在Gallery中的项被点击时触发回调。
  4. setSelection(int position):将Gallery定位到指定位置的项。
  5. getSelectedItemPosition():获取当前选中项的位置。
  6. getChildCount():获取Gallery中子项(图片)的数量。
  7. getChildAt(int index):获取指定索引处的子项View。

四 总结

Gallery在Android平台中已经不再被推荐使用,并且可能会对布局和交互造成一些限制。建议使用RecyclerView或ViewPager等更现代的控件来替代Gallery。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 一 Gallery基本介绍
  • 二 Gallery使用方法
  • 三 Gallery常见属性及方法
  • 四 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档