Control of the Android learning note GridView

Source: Internet
Author: User

1.xml Layout file

Main.xml

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6     7     <GridView8         Android:id= "@+id/girdview1"9 Android:numcolumns= "2"Ten android:verticalspacing= "10DP" One android:horizontalspacing= "10DP" A Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content"> -     </GridView> the      -    <!-- - Android:numcolumns//Number of columns - distance between android:verticalspacing//two rows + distance between android:horizontalspacing//two columns -      - +  A </LinearLayout>

Item.xml

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 android:gravity= "Center" >7     8     <ImageView9         Android:id= "@+id/imageview1"Ten Android:layout_width= "60DP" One Android:layout_height= "60DP" A android:src= "@drawable /A"/> -      -     <TextView the         Android:id= "@+id/textview1" - Android:layout_margintop= "5DP" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + android:textsize= "15SP" - Android:text= "Arsenal"/> +      A  at </LinearLayout>

2.java Code

1  Public classMainactivityextendsActionbaractivityImplementsOnitemclicklistener {2     3     PrivateGridView gv1;4     PrivateList<map<string,object>>dataList;5     Private int[] pic ={R.DRAWABLE.A,R.DRAWABLE.L,R.DRAWABLE.M,R.DRAWABLE.Q};6     PrivateString[] name = {"Arsenal", "Liverpool", "Manchester United", "Chelsea"};7     Privatesimpleadapter sa;8 9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); -          -GV1 =(GridView) Findviewbyid (r.id.girdview1); the         //Initialize -DataList =NewArraylist<map<string,object>>(); -          -         //1. Preparing the data source +         //2. New Adapter (Simpleadapter) -         //3.GridView Load Adapter +         //4.GridView Configuration Time Listener (Onitemclicklistener) A          atSA =NewSimpleadapter ( This, GetData (), R.layout.item,Newstring[]{"pic", "name"},New int[]{r.id.imageview1,r.id.textview1}]; - Gv1.setadapter (SA); -Gv1.setonitemclicklistener ( This); -          -         -          in     } -         PrivateList<map<string,object>>GetData () { to                   for(intI =0;i<pic.length;i++){ +map<string,object> map =NewHashmap<string, object>(); -Map.put ("Pic", Pic[i]); theMap.put ("name", Name[i]); * datalist.add (map); $         }Panax Notoginseng                 returndataList; -         } the @Override +          Public voidOnitemclick (adapterview<?> Parent, view view,intposition, A                 LongID) { the             //TODO auto-generated Method Stub +Toast.maketext ( This, "I am" +Name[position], toast.length_short). Show (); -         }   $}

Control of the Android learning note GridView

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.