Androidui components of the GridView

Source: Internet
Author: User

The Java code is as follows (a simple point of knowledge I will explain in the form of annotations):
Package com.gc.gridviewdemo;/** * @author Android General *//** * Knowledge Points: * 1, GridView is used to display multiple components in a row and column distribution on the interface. * The GridView and ListView have a common parent class: Abslistview, so the GridView * and the ListView have very high similarities, both of which are list items. * The only difference between the GridView and the ListView is that the ListView displays only one column, while the GridView * can display multiple columns. From this point of view, the ListView is equivalent to a special gridview, * If the GridView only displays a column, then the GridView becomes a ListView. * Similar to the ListView, the GridView also needs to provide the displayed data via adapter * 2, GridView Common XML attributes: * android:columnwidth------Setting the width of the column * Android: NumColumns-------Set Number of columns * android:horizontalspacing----Set the horizontal spacing between elements * android:verticalspacing----Set the vertical spacing between elements * * Import Java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.gridview;import Android.widget.ImageView ; Import Android.widget.simpleadapter;public class MainActivity extends Activity {private GridView grid;private ImageView imageview;int[] imageids=new int[]{ R.drawable.addresslist1,r.drawable.conference2,r.drawable.daiban3,r.drawable.email4,r.drawable.file5, R.DRAWABLE.INFROM6,R.DRAWABLE.ORGANIZATION7,R.DRAWABLE.PUBLICFILE8,R.DRAWABLE.REQUEST9,R.DRAWABLE.SIGN10, R.DRAWABLE.TASK11,R.DRAWABLE.WORK12}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Create a list object, the element of the list object is maplist<map<string, Object>> listitems=new arraylist<map<string,object>> (); for (int i=0;i<imageids.length;i++) { Map<string, object> listitem=new hashmap<string,object> (); Listitem.put ("image", imageIds[i]); Listitems.add (ListItem);} Gets the imageviewimageview= (ImageView) Findviewbyid (R.id.imageview) that displays the picture;//Create a Simpleadaptersimpleadapter Simpleadapter=new Simpleadapter (This, ListItems, R.layout.cell, new string[]{"image"}, new Int[]{r.id.image1}); Grid= ( GridView) findViewbyid (R.ID.GRID01);//For GridView set Adaptergrid.setadapter (Simpleadapter);// Add list item selected listener Grid.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void onitemselected ( Adapterview<?> parent, View view,int position, long ID) {//TODO auto-generated method stub//display the currently selected picture imageview.se Timageresource (Imageids[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});// Add list item clicked Listener Grid.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (Adapterview <?> parent, View view,int position, long ID) {//TODO auto-generated method stub//show clicked Picture Imageview.setimageresourc E (imageids[position]);}});}}

The code for the XML layout file is as follows:

Activity_main.xml

<linearlayout 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:orientation= "vertical"    android:layout_gravity= "Center_horizontal"     >    <!-- Define a GridView component--    <gridview         android:id= "@+id/grid01"        android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:horizontalspacing= "1pt"        android:verticalspacing= " 1pt "        android:numcolumns=" 4 "        android:gravity=" center "       />    <imageview         android:id=" @+id/imageview "        android:layout_width=" 240DP "        android:layout_height=" 240DP "        android:layout_ Gravity= "Center_horizontal"        /></linearlayout>

The code for Cell.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <imageview         android:id= "@+id/image1"        android:layout_width= "60DP"        android:layout_height= "60DP"        /></linearlayout>

Operating effect:





Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/25342001

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.