安卓开发中Gallery控件的使用

前言:

微信扫码体验小程序(很有意思哦~~【坏笑】):

本文将介绍Gallery控件的简单使用,Gallery控件用于灵活展示图片。

先附上我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mytest.MainActivity">

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

接下来,我们需要在MainActivity的oNCreate方法中加入这段话:

Gallery gallery = (Gallery) findViewById(R.id.gallery);

这样一来,我们便可以对Gallery控件进行一些操作。

为了将图片资源用Gallery展示,我们需要创建一个适配器。在一般的工程项目中,自定义Adapter是比较常见的,因此,本文也选择使用自定义的Adapter:

 
package com.example.mytest;  import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView;  /**  * Created by ZWH on 2016/5/16.  */ public class GalleryAdapter extends BaseAdapter { private Context mContext;  //设置要展示的图片资源  int[] images = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};   public GalleryAdapter(Context context) { this.mContext = context;  } @Override  public int getCount() { return images.length;  } @Override  public Object getItem(int i) { return i;  } @Override  public long getItemId(int i) { return i;  } @Override  public View getView(int i, View view, ViewGroup viewGroup) { //在此最好判断一下view是否为空  ImageView image = new ImageView(mContext);  image.setImageResource(images[i]);  image.setAdjustViewBounds(true);  //设置宽高  image.setLayoutParams(new Gallery.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  return image;  } }

接下来,就在MainActivity中是为Gallery配置适配器了,附上我MainActivity中的代码:

 
package com.example.mytest;  import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Toast;  /**  * Created by ZWH on 2016/5/16.  */ public class MainActivity extends AppCompatActivity { GalleryAdapter galleryAdapter;   @Override  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  Gallery gallery = (Gallery) findViewById(R.id.gallery);  galleryAdapter = new GalleryAdapter(MainActivity.this);  gallery.setAdapter(galleryAdapter);  //相应的点击事件  gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "您点击的是" + i, Toast.LENGTH_LONG).show();  } });  } }

最后,附上效果图:

                             

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
非常抱歉之前的回答有误,PowerApps 的 DataTable 控件确实没有 VisibleRows 属性,也没有类似于 Gallery 控件的 Items 属性。以下是一种不使用 Gallery 控件实现分页的方法: 1.在 PowerApps 创建一个新的屏幕,然后添加一个 DataTable 控件。 2.将 DataTable 的 Items 属性设置为筛选后的数据源 filteredData。 3.为 DataTable 添加两个按钮,一个用于上一页,一个用于下一页,并为它们分别设置 OnSelect 属性。 4.在屏幕上添加一个文本框,用于显示当前页码,将其默认值设置为 1。 5.在文本框的 Text 属性添加以下公式,用于显示当前页码: ``` Text(currentPage) ``` 6.为上一页按钮的 OnSelect 属性添加以下公式: ``` If(Value(PageNumber.Text)>1,UpdateContext({currentPage:Value(PageNumber.Text)-1, firstRowIndex:If(currentPage=2,0,firstRowIndex-5)})) ``` 这里的 firstRowIndex 变量用于跟踪 DataTable 第一个显示的行的索引。当点击上一页按钮时,如果当前页码大于 1,则将 currentPage 减 1,同时将 firstRowIndex 减去 5。如果 currentPage 等于 2,则将 firstRowIndex 重置为 0。 7.为下一页按钮的 OnSelect 属性添加以下公式: ``` If(Value(PageNumber.Text)<Ceiling(CountRows(filteredData)/5),UpdateContext({currentPage:Value(PageNumber.Text)+1, firstRowIndex:If(currentPage=1,firstRowIndex+5,firstRowIndex)})) ``` 当点击下一页按钮时,如果当前页码小于总页数,则将 currentPage 加 1,同时将 firstRowIndex 加上 5。如果 currentPage 等于 1,则不需要改变 firstRowIndex 的值。 8.在 DataTable 的 Data 属性添加以下公式,用于根据 firstRowIndex 和每页显示的行数来计算应该显示哪些行: ``` Table.Skip(Table.FirstN(filteredData,firstRowIndex+5),firstRowIndex) ``` 这里的 Table.FirstN 函数用于获取 filteredData 的前 n 行数据,Table.Skip 函数用于跳过前 n 行数据。 完成以上步骤后,您可以尝试在应用程序运行该屏幕,并使用上下页按钮来分页显示 DataTable 的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抓蛙Sout

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值