This section will achieve the following effects
Ps: Looks good, and very much like nine/se
-----------------------------------------------------------------------
Below to get to the point [S1]:
Step 1: Create a new layout, inside the GridView
<gridview
android:id="@+id/gridview"
Android:layout_width="Wrap_content"
android:layout_height="Wrap_content"
android:layout_margintop="10DP"
android:numcolumns="3"
android:horizontalspacing="10DP"
android:verticalspacing="10DP" >
</GRIDVIEW>[S2]
Step 2: Create Java code
This effect is achieved using the ListView
So in the need to involve the knowledge of the ListView
ListView requires data source, adapter, listener
① Considering the GridView contains pictures, text, so in advance to add pictures, text
Private int [] icon={r.drawable. Address_book, r.drawable. Calendar, r.drawable. camera, r.drawable. clock, r.drawable. Games_control, r.drawable. Messenger, r.drawable. ringtone, r.drawable. settings, r.drawable. Speech_balloon, r.drawable. Weather,
R.drawable. World, r.drawable. YouTube};
Private String[] iconname={"Contacts", "Calendar", "Camera", "clock", "Games", "SMS", "ringtones", "Settings", "Voice", "Weather", "browser", "Youtube"};
② New Adapter
---because it contains pictures, text, so the Simpleadapter adapter is used
Explanation of Ps:simpleadapter parameters
Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to)
* Simpleadapter ()
*---->>context: Context
*---->data: Data source list<? Extends Map<string,?>> data, a Map composed of a list collection
* Each map will correspond to one row in the ListView list
* The key in each map (key ~ value pair) must contain all the keys specified in the From
*---->resource: The ID of the list item layout file
*----The key name in >from:map
*---->to: ID in bound Data View, corresponding to from
Adapter=new Simpleadapter (this, GetData (), R.LAYOUT.VIEW_GRID[S3], new string[]{"image", "Text"}, new Int[]{r.id.image , r.id.text});//New Adapter
Gridview.setadapter (adapter);
Style code for Ps:r.layout.view_grid
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
Android:layout_width="Match_parent"
android:layout_height="Match_parent"
android:orientation="Vertical"
android:gravity="center"
android:background="#000000">
<imageview
android:id="@+id/image"
android:src="@drawable/ic_launcher"
Android:layout_width="60DP"
android:layout_height="60DP"/>
<textview
android:id="@+id/text"
android:layout_margintop="5DP"
Android:layout_width="Wrap_content"
android:layout_height="Wrap_content"
android:textcolor="#ffffff"
android:text=" text "/>
</LinearLayout>
③ Adding a data source
Private list<map<string,object>>datalist;//Statement
Datalist=new arraylist<map<string,object>> ();
Private List<map<string,object>> GetData () {
for (int i=0;i<icon.length;i++) {
map<string,object>map=New hashmap<string,object> ();
Map.put ("image", icon[i]);
Map.put ("text", Iconname[i]);
Datalist.add (map);
}
return dataList;
}
④ Creating listeners
Implementing interfaces
public class View extends Activity implements Onitemclicklistener
Gridview.setonitemclicklistener (this);
⑤ Implementing Listener Events
Public void Onitemclick (adapterview<?> arg0, Android.view.View arg1,
int position, long ID) {
Toast. Maketext (This, "I am" +iconname[position],toast. length_short). Show (); }
[S1]/*
* 1. Prepare the data source
* 2. New adaptation <SimpleAdapter>
* 3.GridView Load Adapter
* 4.GridView Configuration Event Listener (Onitemclicklistener)
*/
[S2] android:numcolumns= "" Each row shows how many columns android:horizontalspacing= "" The spacing between two columns
android:verticalspacing= "" Spacing between two lines
[S3] This is the new layout
(that is, the style of the displayed text and pictures)
Android Growth Diary-use GridView to display multiple rows of data