Key code
/** * Set the width of the gridview * * @param defaultcolumns Set Number of columns 4 * @param Defaultrows Set the number of rows 2 */ Private void Setgridviewvalue(GridView GridView,intPostion,intDefaultcolumns,intDefaultrows) {intCount = Recomgameadapter.getcount (); Gridview.setadapter (Recomgameadapterlist.get (postion));intcolumns =0;if(Count >=8) {//When Count is greater than 8 o'clock arranged as follows //| 1 | 3 | 5 | 7 | //| 2 | 4 | 6 | 8 |Columns = (count%2==0) ? Count/2: Count/2+1; }Else{//When Count is less than 8 o'clock, arrange as follows //| 1 | 2 | 3 | 4 | //| 5 | 6 | 7 | 8 |Columns = (count% Defaultrows = =0) ? Count/defaultrows:count/defaultrows +1;if(Columns < Defaultcolumns) {columns = Defaultcolumns; } }intColumnWidth = (Dm.widthpixels-mcontext.getresources (). Getdimensionpixeloffset (R.dimen.home_common_margin) *2)/defaultcolumns; Relativelayout.layoutparams params =NewRelativelayout.layoutparams (Columns * columnWidth, RelativeLayout.LayoutParams.WRAP_CONTENT); Gridview.setlayoutparams (params); Gridview.setcolumnwidth (ColumnWidth);//Gridview.sethorizontalspacing (hspacing);Gridview.setstretchmode (Gridview.no_stretch); Gridview.setnumcolumns (columns); }
Layout file
<? XML version="1.0" encoding="Utf-8"? ><horizontalscrollview Xmlns:android = "http://schemas.android.com/apk/res/android" android:id =" @+id/scrollview " android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity =" center " android:fillviewport = "true" android:scrollbars =; <GridViewandroid:id= "@+id/recom_game_gv"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_below=" @+ Id/day_recom_rl "android:numcolumns=" 4 "android:scrollbars=" None " Android:stretchmode="ColumnWidth"> </GridView> <</horizontalscrollview>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Realization of horizontal sliding of Android multi-line GridView