The GridView is used to display multiple components by row and column distribution on the interface. The GridView and ListView have a common parent class: Abslistview. The difference between the GridView and the ListView is that the ListView is distributed only in one direction, and the GridView is distributed in two directions. So when using the GridView is generally specified numcolumns greater than 1, otherwise the default value of the property is 1, which means that the GridView only one column, it becomes a ListView
The XML attribute of the GridView
The value of the Android:strtchmode is:
Imageswitcher is derived from Framelayout, Imageswitcher components are similar to ImageView, but imageswitcher one more feature: it can animate when the picture you are displaying is toggled.
When using Imageswitcher, it is often necessary to set a imageswitcher.viewfactory Makeview () method for it to display ImageView
The following combination of the GridView and Imageswitcher to achieve an example
(1) Activity_main.xml layout GridView and Imageswitcher
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:gravity=" Center_horizontal "> <gridview android:id=" @+id/grid " android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:horizontalspacing= "1DP" android:verticalspacing= "2DP" android:numcolumns= "4"/> <imageswitcher android:id= "@+id/imageswitch" android: Layout_gravity= "Center_vertical|center_horizontal" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content"/> </LinearLayout>
(2) Img.xml layout ImageView for displaying the GridView picture
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:orientation=" horizontal " android:layout_width=" fill_parent " android:layout_ height= "Fill_parent" > <imageview android:id= "@+id/img" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
(3) Mainactivity.java
Package Com.example.viewimage;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.gridview;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.simpleadapter;import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity {//define component private GridView Gredview = Null;private imageswitcher imageswt =null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get layout component Gredview = (GridView) Findviewbyid (R.id.grid) ; imageswt = (Imageswitcher) Findviewbyid (r.id.imageSwitch);//Picture final int images[] = new INT[]{R.DRAWABLE.NAME01,R.DRAWABLE.NAME02,R.DRAWABLE.NAME03,R.DRAWABLE.NAME04, R.drawable.name05,r.drawable.name06,r.drawable.name07,r.drawable.name08,r.drawable.name09,r.drawable.name10, r.drawable.name11,r.drawable.name12,r.drawable.name13,r.drawable.name14,r.drawable.name15,r.drawable.name16};/ /Create List collection, save picture list<map<string,object>> list = new arraylist<map<string,object>> (); for (int i =0;i<images.length;i++) {map<string,object> Map = new hashmap<string,object> () map.put ("img", images[ I]); List.add (map);} Set picture change effect//fade-in effect Imageswt.setinanimation (Animationutils.loadanimation (this, Android. r.anim.fade_in));//Fade Out Effect Imageswt.setoutanimation (Animationutils.loadanimation (this,android. r.anim.fade_out));//Set Picture conversion effect Imageswt.setfactory (new Viewfactory () {@Overridepublic View Makeview () {ImageView ImageView = new ImageView (mainactivity.this); Imageview.setlayoutparams (new Imageswitcher.layoutparams (300, 300)); Imageview.setscaletype(scaletype.fit_center); return imageView;}); /set Adapter Simpleadapter simpleadapter = new Simpleadapter (this, list, r.layout.img, New string[]{"IMG"}, New Int[]{r.id.img} );//Adapter Gredview.setadapter (Simpleadapter);//Check Event listener Gredview.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void onitemselected (adapterview<?> parent, View view,int position, long id) {LOG.I (" Onitemselected "," onitemselected "); Imageswt.setimageresource (Images[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});// Click event Listener Gredview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<? > Parent, View view,int position, long id) {LOG.I ("Onitemclick", "Onitemclick"); Imageswt.setimageresource (images[ Position]),}});}}
The results are as follows: