GridView(網格視圖)的基本使用
本節引言:
本節給大家介紹的是第二個Adapter類別的控制項-GridView(網格視圖),見名知義,ListView是列表, GridView就是顯示網格!他和ListView一樣是AbsListView的子類別!很多東西和ListView都是相通的, 本節我們就來學習他的基本用法~
#1.相關屬性:
下面是GridView中的一些屬性:
- #android:columnWidth:設定列的寬度
- android:gravity:元件對其方式
- android:horizontalSpacing :水平方向每個單元格的間距
- android:verticalSpacing:垂直方向每個單元格的間距
- android:numColumns:設定列數
- android:stretchMode:設定拉伸模式,可選值如下:none:不拉伸;spacingWidth :拉伸元素間的間隔空隙columnWidth:僅僅拉伸表格元素本身spacingWidthUniform:既拉元素間距又拉伸他們之間的間隔空襲
#2.使用範例:
下面透過一個簡單的範例來熟悉這個控制項的使用: (這裡用的Adapter我們直接用之2.5.0中教大家寫的可復用的BaseAdapter~)
實現的效果圖:
#程式碼實作:
首先是GridView 的Item的佈局:item_grid_icon.xml:
<?
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp /img_icon"
android:layout_width="64dp"
android:layout_height="64 android:src="@mipmap/iv_icon_1" />
android:id="@+id/txt_icon"
and android :layout_below="@id /img_icon"
android:layout_centerHorizontal="true"
android:layout_margin android:textSize="18sp" / >
</RelativeLayout>
#
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp /img_icon"
android:layout_width="64dp"
android:layout_height="64 android:src="@mipmap/iv_icon_1" />
and android :layout_below="@id /img_icon"
android:layout_centerHorizontal="true"
android:layout_margin android:textSize="18sp" / >
</RelativeLayout>
#
連結我們寫個實體實體類別:Icon.java:
/**
* 由 Jay 創建於 2015/9/24 0024.
*/
public class Icon {
private int iId;
private String iName;
public Icon() {
}
= iId;
this.iName = iName;
}
public int getiId() {# # public String getiName() {
return iName;
}
##卷 public void setiId(int iId) {
## this.iId iId> this。 ) {
this.iName = iName;
}
}
* 由 Jay 創建於 2015/9/24 0024.
*/
public class Icon {
private int iId;
private String iName;
public Icon() {
}
= iId;
this.iName = iName;
}
public int getiId() {# # public String getiName() {
return iName;
}
##卷 public void setiId(int iId) {
## this.iId iId> this。 ) {
this.iName = iName;
}
}
最後是MainActivity的佈局以及Java程式碼
activity_main.xml:
公共類別MainActivity擴充AppCompatActivity{
#私有Context mContext;
私有GridView grid_photo;
私有BaseAdapter mAdapter =#fapullo>Afwhoommo;#IwI mData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
grid_photo = (GridView) findViewById(R.id.grid_photo);
mData new Icon(R.mipmap ) .iv_icon_1, "圖示1"));
mData.add(new Icon(R.mipmap.iv_icon_2, "圖示2"));
"圖示3"));
mData.add(new Icon(R.mipmap.iv_icon_4, "圖示4"));
mData.add(new I.5.mi mData.add(new I.5.mi ) );
mData.add(new Icon(R.mipmap.iv_icon_6, "圖示6"));
mData.add(new Icon(R.mi
## mAdapter = new MyAdapter(mData, R.layout.item_grid_icon) {
void bindView(ViewHolder holder, Icon obj) {
holder.set影像資源( R.id .img_icon, obj.getiId());
holder.setText(R.id. };
grid_photo. setAdapter(mAdapter );
grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
int position, long id) {
Toast.makeText(mContext, "點選了~" + position + "~項目", Toast.LENGTH_SHORT).show();
}
#
#私有Context mContext;
私有GridView grid_photo;
私有BaseAdapter mAdapter =#fapullo>Afwhoommo;#IwI mData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
grid_photo = (GridView) findViewById(R.id.grid_photo);
mData new Icon(R.mipmap ) .iv_icon_1, "圖示1"));
mData.add(new Icon(R.mipmap.iv_icon_2, "圖示2"));
"圖示3"));
mData.add(new Icon(R.mipmap.iv_icon_4, "圖示4"));
mData.add(new I.5.mi mData.add(new I.5.mi ) );
mData.add(new Icon(R.mipmap.iv_icon_6, "圖示6"));
mData.add(new Icon(R.mi
## mAdapter = new MyAdapter
void bindView(ViewHolder holder, Icon obj) {
holder.set影像資源( R.id .img_icon, obj.getiId());
holder.setText(R.id. };
grid_photo. setAdapter(mAdapter );
grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
int position, long id) {
Toast.makeText(mContext, "點選了~" + position + "~項目", Toast.LENGTH_SHORT).show();
}
#
MainActivity.java:
公共類別MainActivity擴充AppCompatActivity{
#私有Context mContext;
私有GridView grid_photo;
私有BaseAdapter mAdapter =#fapullo>Afwhoommo;#IwI mData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
grid_photo = (GridView) findViewById(R.id.grid_photo);
mData new Icon(R.mipmap ) .iv_icon_1, "圖示1"));
mData.add(new Icon(R.mipmap.iv_icon_2, "圖示2"));
"圖示3"));
mData.add(new Icon(R.mipmap.iv_icon_4, "圖示4"));
mData.add(new I.5.mi mData.add(new I.5.mi ) );
mData.add(new Icon(R.mipmap.iv_icon_6, "圖示6"));
mData.add(new Icon(R.mi
## mAdapter = new MyAdapter(mData, R.layout.item_grid_icon) {
void bindView(ViewHolder holder, Icon obj) {
holder.set影像資源( R.id .img_icon, obj.getiId());
holder.setText(R.id. };
grid_photo. setAdapter(mAdapter );
grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
int position, long id) {
Toast.makeText(mContext, "點選了~" + position + "~項目", Toast.LENGTH_SHORT).show();
}
#
#私有Context mContext;
私有GridView grid_photo;
私有BaseAdapter mAdapter =#fapullo>Afwhoommo;#IwI mData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
grid_photo = (GridView) findViewById(R.id.grid_photo);
mData new Icon(R.mipmap ) .iv_icon_1, "圖示1"));
mData.add(new Icon(R.mipmap.iv_icon_2, "圖示2"));
"圖示3"));
mData.add(new Icon(R.mipmap.iv_icon_4, "圖示4"));
mData.add(new I.5.mi mData.add(new I.5.mi ) );
mData.add(new Icon(R.mipmap.iv_icon_6, "圖示6"));
mData.add(new Icon(R.mi
## mAdapter = new MyAdapter
void bindView(ViewHolder holder, Icon obj) {
holder.set影像資源( R.id .img_icon, obj.getiId());
holder.setText(R.id. };
grid_photo. setAdapter(mAdapter );
grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
int position, long id) {
Toast.makeText(mContext, "點選了~" + position + "~項目", Toast.LENGTH_SHORT).show();
}
#
嗯,代码非常简单~
3.示例代码下载:
GridViewDemo1.zip
本节小结:
本节给大家介绍了第二个需要使用Adapter的UI控件——网格视图GridView,用法很简单~ 大家可以根据自己的需求进行扩展,比如用GridView显示手机相册~嗯,就说这么多, 谢谢~