GridView in Android using custom adapters for graphical view arrangement

Source: Internet
Author: User

The GridView component in Android is used to arrange views in a grid, similar to a matrix, which can be used when there are many elements (text, pictures, or other elements) on the screen that need to be displayed. Below we have implemented the code as an example (for convenience, set event handling (toast) to the top of the screen)

Layouts under Gridview.xml layout file:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "vertical" android:layout_width= "match_parent"4android:layout_height= "Match_parent" >5<GridView6Android:id= "@+id/gv"7Android:layout_width= "Match_parent"8android:layout_height= "Match_parent"9Android:numcolumns= "3"TenAndroid:columnwidth= "90DP" OneAndroid:stretchmode= "ColumnWidth" AAndroid:verticalspacing= "10DP" -android:horizontalspacing= "10DP" > -</GridView> the</LinearLayout>

The GridView property setting resolves:

Anroid:numcolumns= "3" GridView column number set to 3

Android:columnwidth= "90DP", width of each column, which is the width of item

Android:stretmode= "ColumnWidth" Zoom and column width size synchronization

Android:verticalspacing= the margin between two lines of "10DP"

Android:horizontalspacing= the margin between two columns of "10DP"

Layout under Item_layout.xml Layout File ( that is, the layout of each cell within the grid ) :

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:gravity= "Center"(you don't need to repeat settings elsewhere when you set gravity to center)6android:orientation= "Vertical" >7<ImageView8Android:id= "@+id/iv"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" OneAndroid:scaletype= "Centercrop" Aandroid:src= "@mipmap/ic_launcher"/> -<TextView -Android:id= "@+id/tv" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "function" (test using the text setting in the text box here is not added)/> -</LinearLayout>
Java Code Implementation function (core is a custom adapter):
1  Public classGridviewdemoextendsappcompatactivity {2     PrivateGridView GV;//declaring the GridView view3 @Override4     protected voidonCreate (@Nullable Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.gridview);7GV =(GridView) Findviewbyid (R.ID.GV);8Gv.setadapter (NewMyadapter ( This));//To implement a grid layout by setting up an adapter9         //Add a Click event for each cell (item)TenGv.setonitemclicklistener (NewAdapterview.onitemclicklistener () { One @Override A              Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { -TextView TV =(TextView) View.findviewbyid (r.id.tv); -Toast T =toast.maketext (Gridviewdemo. This, Tv.gettext (). toString (), toast.length_short); theT.setgravity (gravity.top,0,0); - t.show (); -             } -         }); +     } -     //Custom Adapters (by inheriting Baseadapter) +     classMyadapterextendsBaseadapter { AContext context;//declaring the context referenced in the adapter at         //encapsulate the picture and text that you want to refer to in each group -         int[] Images ={r.mipmap.ic_launcher, r.mipmap. - Ic_launcher, R.mipmap.ic_launcher, R.mipmap. - Ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,}; -String[] names = {"Function 1", "Function 2", "Function 3", "Function 4", "Function 5", "function 6"}; -         //initializing a context by constructing a method in          PublicMyadapter (Context context) { -              This. Context =context; to         } + @Override -          Public intGetCount () { the             returnNames.length;//images can also *         } $ @OverridePanax Notoginseng          PublicObject GetItem (intposition) { -             returnNames[position];//images can also the         } + @Override A          Public LongGetitemid (intposition) { the             returnposition; +         } - @Override $          PublicView GetView (intposition, View Convertview, ViewGroup parent) { $             //fill the layout within a grid cell with the layout filler Layoutinflater -View v = layoutinflater.from (context). Inflate (R.layout.item_layout,NULL); -             //use Findviewbyid to find the picture and text of the layout within the cell theImageView IV =(ImageView) V.findviewbyid (R.ID.IV); -TextView TV =(TextView) V.findviewbyid (r.id.tv);Wuyi             //referencing elements within an array to set the contents of a picture and text inside a layout the Iv.setimageresource (images[position]); - Tv.settext (names[position]); Wu             //The return value must be cell overall layout v -             returnv; About         } $     } -}
At this point the code is complete, in order to achieve the function of the picture and text style is not refined, interested friends can try to play on their own.

GridView in Android using custom adapters for graphical view arrangement

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.