In addition to the ListView using the adapter, there is also a gridview,listview is a single-column multi-line display, suitable for multi-item viewing. The GridView is a multi-row, multi-column display that is typically used to view image-style activity. A small demo. Each item is a line of text below the picture.
Some of the properties of Girdview:
android:numcolumns= "Auto_fit"--------number of columns set to Automatic
Android:columnwidth= "90DP",----------the width of each column, which is the width of item
Android:stretchmode= "columnWidth"------zoom is synchronized with column width size
android:verticalspacing= "10DP"----------vertical margins
android:horizontalspacing= "10DP"-------Horizontal margins
The custom adapter is used here,
Gridviewactivity
Public classGridviewactivityextendsActivity {PrivateGridView GridView; PrivateGridviewadapter Adapter; PrivateList<map<string,object>>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_grid_view); GridView=(GridView) Findviewbyid (R.id.gridview); List=NewArraylist<map<string, object>>(); Adapter=NewGridviewadapter ( This, GetData ()); Gridview.setadapter (adapter); }
One of the more important here is to set the GridView row height,
Convertview.setlayoutparams (New Gridview.layoutparams (100, 100));//Key line
But this attribute android:columnwidth don't know why it doesn't work. No matter how much you set, it doesn't change
classGridviewadapterextendsbaseadapter{PrivateLayoutinflater Mlayoutintfalter; Privatecontext Context; PrivateList<map<string,object>>list; PublicGridviewadapter (Context context,list<map<string,object>>list) { This. Context =context; This. List =list; This. Mlayoutintfalter =Layoutinflater.from (context); } @Override Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (intposition) { returnList.get (position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewhold Viewhold=NULL; if(Convertview = =NULL) {Viewhold=NewViewhold (); Convertview= Mlayoutintfalter.inflate (R.layout.gridviewitemlayout,NULL); Viewhold.image=(ImageView) Convertview.findviewbyid (R.ID.IMAGEVIEW2); Viewhold.textview=(TextView) Convertview.findviewbyid (R.ID.TEXTVIEW4); Convertview.settag (Viewhold); }Else{viewhold=(Viewhold) Convertview.gettag (); } intid = integer.parseint (list.get (position). Get ("Pic"). toString ()); ViewHold.image.setImageResource (ID); ViewHold.textView.setText (List.get (position). Get ("Titel"). toString ()); Convertview.setlayoutparams (NewGridview.layoutparams (100, 100));//Key Lines returnConvertview; } Public Final classviewhold{ PublicImageView image; PublicTextView TextView; }
Gridviewitemlayout.xml
The layout of each item
<?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 : Orientation= "vertical" > <ImageView android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:id= "@+id/imageview2"/> < TextView android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text= "New text" android:id= "@+id/textview4"/></linearlayout>
Use of the GridView for Android Learning notes