Android Basics Getting Started tutorial--2.4.9 GridView (grid view) Basic usage
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
This section introduces you to the second adapter class of control--gridview (grid view), which is known as the ListView, which is a list,
The GridView is the display grid! He's like a ListView, a Abslistview subclass! A lot of things are connected to the ListView,
In this section we are going to learn his basic usage ~
1. Related properties:
Here are some of the properties in the GridView:
- android:columnwidth: Setting the width of a column
- android:gravity: How the component works on it
- android:horizontalspacing: spacing for each cell in horizontal direction
- android:verticalspacing: spacing for each cell in the vertical direction
- android:numcolumns: Set Number of columns
- android:stretchmode: Set stretch mode with the following optional values:
None: no stretching;spacingwidth: spacing gap between extruded elements
columnWidth: Simply stretching the table element itself
spacingwidthuniform: Both pull the element spacing and stretch their intervals between the air raids
2. Examples of use:
Here's a simple example to familiarize yourself with the use of this control:
(The adapter we use here is directly 2.4.7 the reusable baseadapter~ that we have written.)
implemented by :
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 write an entity class:Icon.java:
/** * Created by Jay on 2015/9/24 0024. * * Public class Icon { Private intIId;PrivateString Iname; Public Icon() { } Public Icon(intIId, String iname) { This. IID = IID; This. iname = Iname; } Public int getiid() {returnIId; } PublicStringGetiname() {returnIname; } Public void setiid(intIID) { This. IID = IID; } Public void Setiname(String iname) { This. iname = Iname; }}
And 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 to set how many per row is displayed -- <GridViewandroid: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 { PrivateContext Mcontext;PrivateGridView Grid_photo;PrivateBaseadapter Madapter =NULL;PrivateArraylist<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 =NewArraylist<icon> (); Mdata.add (NewIcon (R.mipmap.iv_icon_1,"Icon 1")); Mdata.add (NewIcon (R.mipmap.iv_icon_2,"Icon 2")); Mdata.add (NewIcon (R.mipmap.iv_icon_3,"Icon 3")); Mdata.add (NewIcon (R.mipmap.iv_icon_4,"icon 4")); Mdata.add (NewIcon (R.mipmap.iv_icon_5,"icon 5")); Mdata.add (NewIcon (R.mipmap.iv_icon_6,"icon 6")); Mdata.add (NewIcon (R.mipmap.iv_icon_7,"icon 7")); Madapter =NewMyadapter<icon> (Mdata, R.layout.item_grid_icon) {@Override Public void BindView(Viewholder 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 (NewAdapterview.onitemclicklistener () {@Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Toast.maketext (Mcontext,"you clicked."+ Position +"~ Item", Toast.length_short). Show (); } }); }}
Well, the code is very simple ~
3. Sample code Download:
Gridviewdemo1.zip
This section summarizes:
This section introduces you to the second UI control that needs to use adapter-grid view GridView, which is very simple in use ~
We can expand according to their own needs, such as using the GridView Display Phone album ~ Well, say so much,
Thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--2.4.9 GridView (grid view) Basic usage