The GridView belief is also very common, but the GridView has one drawback is that it cannot control the height of the entire GridView, only through the sub-view to control its height.
This is often the case in a project where a GridView is covered with the entire screen
Key points:
The item height is calculated in the adapter of the GridView and then set.
Core code:
@Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder;if (convertview==null) {convertview= layoutinflater. from(context). Inflate(R. Layout. Item_shmain, parent, false);Holder=new Viewholder ();Holder. ITEMIV= (ImageView) convertview. Findviewbyid(R. ID. IV_item);Holder. ITEMTV= (TextView) convertview. Findviewbyid(R. ID. TV_item);Convertview. Settag(holder);}else {holder= (viewholder) Convertview. Gettag();} Holder. ITEMIV. Setbackgroundresource(Images[position]);Holder. ITEMTV. SetText(Names[position]);LinearLayout. LayoutparamsLp=new LinearLayout. Layoutparams(ViewGroup. Layoutparams. MATCH_parent, (Commonutil. Getdeviceheight(context)-commonutil. Getbarheight(context)) /3);Convertview. Setlayoutparams(LP);Return Convertview;}
This setting height is the height of the screen minus the height of the title bar, and then divided by the number of rows of the GridView (this is directly written dead 3). The GridView fills the entire screen by dynamically setting the height of the adapter Convertview.
Finally post all the code records:
Public class mainactivity extends appcompatactivity { PrivateGridView Mgridview;PrivateString[] itemname={"Sweep code Installation","Sweep code Repair","Device data query","My record of work","About Us","Settings"};Private int[] imgid={r.drawable.icon_sh_1, r.drawable.icon_sh_2, R.drawable.icon_sh_3, R.drawable.icon_sh_4, R.drawable.i Con_sh_5, r.drawable.icon_sh_6};PrivateShgridadapter Madapter;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); mgridview= (GridView) Findviewbyid (R.ID.GV_SH); Madapter=NewShgridadapter ( This, Itemname,imgid); Mgridview.setadapter (Madapter); }
Public class shgridadapter extends baseadapter{ PrivateContext context;PrivateString[] names;Private int[] images; Public Shgridadapter(context context, string[] names,int[] images) { This. Context = Context; This. names = names; This. images = images; }@Override Public int GetCount() {returnNames.length; }@Override PublicObjectGetItem(intPosition) {returnPosition }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder holder;if(convertview==NULL) {convertview= Layoutinflater.from (context). Inflate (R.layout.item_shmain, parent,false); Holder=NewViewholder (); holder.itemiv= (ImageView) Convertview.findviewbyid (R.id.iv_item); holder.itemtv= (TextView) Convertview.findviewbyid (R.id.tv_item); Convertview.settag (holder); }Else{holder= (Viewholder) Convertview.gettag (); } holder.itemIV.setBackgroundResource (Images[position]); Holder.itemTV.setText (Names[position]); Linearlayout.layoutparams lp=NewLinearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, commonutil.getdeviceheight (context)-commonu Til.getbarheight (context))/3); CONVERTVIEW.SETLAYOUTPARAMS (LP);returnConvertview; } class viewholder{PrivateImageView ITEMIV;PrivateTextView ITEMTV; }}
Public class commonutil { Public Static int Getdeviceheight(Context context) {WindowManager wm= (windowmanager) Context.getsystemservice (Context.window_service);returnWm.getdefaultdisplay (). GetHeight (); }/** * Get status bar height * / Public Static int Getbarheight(Context context) {class<?> c =NULL; Object obj =NULL; Field field =NULL;intx =0, Sbar = -;//defaults to 38, which seems to be mostly the case Try{c = class.forname ("Com.android.internal.r$dimen"); obj = C.newinstance (); field = C.getfield ("Status_bar_height"); x = Integer.parseint (Field.get (obj). toString ()); Sbar = Context.getresources (). getdimensionpixelsize (x); }Catch(Exception E1) {E1.printstacktrace (); }returnSbar }}
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =" wrap_content " android:orientation =" vertical " android:gravity = "center" android:background =" @drawable/bg_gridview " android:paddingtop =" 30DP " Android:paddingbottom =; <imageview android:id
= "@+id/iv_item" android:layout_width =< Span class= "Hljs-value" > "60DP" android:layout_height =" 60DP " android:layout_centerhorizontal =" true " android:background =" @ Drawable/icon_sh_1 "/> <textview android:id = "@+id/tv_item" android:layout_width = "match_parent" android:layout_height = "match_parent" android:text =" Sweep code Installation " android:gravity =" Center_ Horizontal " android:textsize =" 17SP " android:layout_centerhorizontal = "true"
android:layout_margintop
= "20DP" /> </linearlayout>
Here the border effect of each GridView is achieved through a background (in fact, there are flaws, some places have duplicates)
<selector xmlns:android="Http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="Rectangle"> <stroke android:width="1.0px"android:color="#e12121"/> <gradient android:angle="270.0"android:endcolor="#ffe8ecef" Android:startcolor="#ffe8ecef"/> </shape> </Item> <item android:state_focused="true"> <shape android:shape="Rectangle"> <shape android:shape="Rectangle"> <stroke android:width="1.0px"android:color="#e12121" /> <gradient android:angle="270.0"android:endcolor="#ffe8ecef" Android:startcolor="#ffe8ecef"/> </shape> </shape> </Item> <item> <shape android:shape="Rectangle"> <gradient android:angle="270.0"android:endcolor="#ffffffff" Android:startcolor="#ffffffff" /> <stroke android:width="1.0px"android:color="#e12121" /> </shape> </Item></selector>
OK, the code is very functional and very basic. Feel useful friends can message support, progress together!
The Android GridView cannot populate the activity's workaround