The--gridview of Android learning

Source: Internet
Author: User

Background Knowledge

The GridView (nine Gongge) is often used in Android development as in the ListView. As is often the case with the Quick view, there is an option to change the layout of the picture to a grid (that is, the GridView).


definition

GridView is a This ViewGroup displays items in a two-dimensional, scrollable grid. The grid items is automatically inserted to the layout using a ListAdapter

A grid view is a two-dimensional, scrollable grid of view collections that show grid items. Grid items are automatically inserted into the layout using ListAdapter.

Demo

Mainactivity

1  PackageCom.johntsai.gridviewdemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 6 Importandroid.app.Activity;7 Importandroid.content.Intent;8 ImportAndroid.os.Bundle;9 ImportAndroid.view.View;Ten ImportAndroid.widget.AdapterView; One ImportAndroid.widget.AdapterView.OnItemClickListener; A ImportAndroid.widget.GridView; - ImportAndroid.widget.SimpleAdapter; - ImportAndroid.widget.Toast; the  -  Public classMainactivityextendsActivity { -     Private intids[]={ - R.drawable.aaa, + r.drawable.bbb, - R.DRAWABLE.CCC, + r.drawable.ddd, A R.drawable.eee, at R.drawable.fff, - R.DRAWABLE.GGG, - R.drawable.hhh, - r.drawable.aaa -     }; -     PrivateGridView GridView; in @Override -     protected voidonCreate (Bundle savedinstancestate) { to         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -          theGridView =(GridView) Findviewbyid (R.id.gridview); *      $ArraylistNewArraylist();Panax Notoginseng          for(inti=0;i<ids.length;i++){ -hashmap<string, Object>map =NewHashmap<string, object>(); theMap.put ("Image", Ids[i]); +Map.put ("Id", "Android" +i+ "number")); A images.add (map); the         } +      -Simpleadapter adapter =NewSimpleadapter ( This, $ images, R.layout.gridview_item, $                 Newstring[]{"Image", "Id"},New int[]{r.id.imageitem,r.id.iditem}]; - Gridview.setadapter (adapter); -          theGridview.setonitemclicklistener (NewOnitemclicklistener () { - Wuyi @Override the              Public voidOnitemclick (adapterview<?>Parent, view view, -                     intPositionLongID) { Wu Toast.maketext (Getapplicationcontext (), -"You clicked on the" + position+ "number., Toast.length_long). Show (); About  $             } -         }); -     } -}

Main interface Layout file

<?XML version= "1.0" encoding= "Utf-8"?><GridViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/gridview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:columnwidth= "90DP"Android:numcolumns= "Auto_fit"Android:stretchmode= "ColumnWidth"android:gravity= "Center"/><!--android:columnwidth= "90DP" column width set to 90DP android:numcolumns= "auto_fit" column number set to Auto android:stretchmode= "ColumnWidth" Scaling is synchronized with the width of the column -

GridView Item Layout File

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent" 5 Android:paddingbottom= "4dip">6     7     <ImageView8         Android:id= "@+id/imageitem"9 Android:layout_height= "70DP"Ten Android:layout_width= "70DP" One Android:layout_centerhorizontal= "true" A         /> -     <TextView -         Android:id= "@+id/iditem" the Android:layout_height= "Wrap_content" - Android:layout_width= "70DP" - Android:layout_centerhorizontal= "true" - Android:layout_below= "@id/imageitem" +         /> - </Relativelayout>

Operating effect:

The--gridview of Android learning

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.