0. Introduction
the GridView and ListView have common parent classes: Abslistview, so the GridView and ListView have certain similarities. The main difference between the GridView and ListView is that the ListView is distributed only in one direction, while the GridView is distributed in two directions.
Similarly to ListView, the GridView also needs to provide data displayed through adapter: Developers can provide data to the GridView either through Simpleadapter or by developing Baseadaptei subclasses for the GridView Provide data. Whichever way you use it, the GridView is basically consistent with ListView's usage.
1. Related properties:
Here are some of the attributes in the GridView:
(1) Android:columnwidth: Set the width of the column
(2) Android:gravity: component to its way
(3) Android:horizontalspacing: Horizontal direction of spacing of each cell
(4) Android:verticalspacing: spacing of each cell in the vertical direction
(5) Android:numcolumns: Set the number of columns
(6) Android:stretchmode: Set stretch mode, the optional values are as follows: none: not stretched; Spacingwidth: extrude space between elements ColumnWidth: just stretch the table element itself Spacingwidthuniform: Both pull the element spacing and stretch their interval between the air raids
2. Use examples:
The following is a simple example to familiarize yourself with the use of this control: (Here we use the adapter to 2.5.0 the reusable baseadapter~ that we use in our direct usage)
Implementation of the effect diagram:
Code implementation:
The first is the layout of the GridView item: Item_grid_icon.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: padding= "5DP" >
<imageview
android:id= "@+id/img_icon"
android:layout_width= "64DP"
Android : layout_height= "64DP"
android:layout_centerinparent= "true"
android:src= "@mipmap/iv_icon_1"/>
<textview
android:id= "@+id/txt_icon"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:layout_below= "@id/img_icon"
android:layout_centerhorizontal= "true"
android:layout_margintop= "30DP"
android:text= "hehe"
android:textsize= "18sp"/>
</ Relativelayout>
Then we'll write a entity entity class: Icon.java:
public class Icon {
private int iId;
Private String iname;
Public icon () {
} public
icon (int iId, String iname) {
this.iid = iId;
This.iname = Iname;
}
public int getiid () {return
iId;
}
Public String Getiname () {return
iname;
}
public void setiid (int iId) {
this.iid = iId;
}
public void Setiname (String iname) {
this.iname = iname;
}
}
Finally, the layout of the mainactivity and the Java code
Activity_main.xml:
<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 "
android:padding= "5DP"
tools:context= ". Mainactivity ">
<!--numcolumns set how many--> to display per line
<gridview
android:id=" @+id/grid_photo
" Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:numcolumns= "3"/>
</RelativeLayout>
Mainactivity.java:
public class Mainactivity extends Appcompatactivity {private context mcontext;
Private GridView Grid_photo;
Private Baseadapter madapter = null;
Private arraylist<icon> mdata = null;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mcontext = Mainactivity.this;
Grid_photo = (GridView) Findviewbyid (R.id.grid_photo);
Mdata = new arraylist<icon> ();
Mdata.add (New icon (r.mipmap.iv_icon_1, "icon 1");
Mdata.add (New icon (r.mipmap.iv_icon_2, "icon 2");
Mdata.add (New icon (r.mipmap.iv_icon_3, "icon 3");
Mdata.add (New icon (r.mipmap.iv_icon_4, "icon 4");
Mdata.add (New icon (r.mipmap.iv_icon_5, "icon 5");
Mdata.add (New icon (r.mipmap.iv_icon_6, "icon 6");
Mdata.add (New icon (r.mipmap.iv_icon_7, "icon 7"); Madapter = new Myadapter<icon> (Mdata, R.layout.item_grid_icon) {@Override public void BindView (viewhold ER holder, Icon obj) {
Holder.setimageresource (R.id.img_icon, Obj.getiid ());
Holder.settext (R.id.txt_icon, Obj.getiname ());
}
};
Grid_photo.setadapter (Madapter); Grid_photo.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (Ad Apterview<?> Parent, view view, int position, long id) {Toast.maketext (Mcontext, "you clicked ~" + position + "~ Item",
Toast.length_short). Show ();
}
});
}
}