package com.example.test; import android.app.activity;import android.content.context; import android.graphics.color;import android.os.bundle;import android.view.gravity;import android.view.view;import android.view.viewgroup;import android.widget.abslistview;import Android.widget.baseadapter;import android.widget.gridview;import android.widget.linearlayout;import android.widget.textview; public class mainactivity extends activity { private static GridView mGridView; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); mgridview = new gridview (Getapplicationcontext ()); madapter adapter = new madapter (Mgridview.getcontext (), new string[]{ "Hello 1", "Hello 2", "Hello 3", "Hello 4", "Hello 5", "Hello 6", "Hello 7", "Hello 8", "Hello 9", "Hello", "Hello 11", "Hello 12", "Hello 13", "Hello 14", "Hello 15", "Hello 16", " Hello 17 "," Hello 18 "," Hello 19 "," Hello ", " Hello 21 "," Hello 22 "," Hello 23 "," Hello 24 "," Hello 25 "," Hello 26 "," Hello 27 "," Hello 28 "," Hello 29 "," Hello " } ); linearlayout headlayout = new LinearLayout (Getapplicationcontext ()); Linearlayout.layoutparams params = new linearlayout.layoutparams ( linearlayout.layoutparams.fill_parent,  300); headlayout.setlayoutparams (params); Headlayout.setbackgroundcolor (color.red); mgridview.setadapter ( adapter); mgridview.setnumcolumns (2); setcontentview (Mgridview); } static class MAdapter extends BaseAdapter{ private String[] items; private context context; public madapter ( Context context, string[] items) { this.items = items; this.context = context; } @Override public int getcount () { return items.length; } @Override public object getitem (int Position) { return position; } @ Override public long getitemid (int position) { return position; } @Override public int getitemviewtype (int Position) { return Super.getitemviewtype (position); } @Override public view getview (Int position, view convertview, viewgroup parent) { textview textview = null; if (position %4 == 0) { &Nbsp; textview = new mytext ( Context); Abslistview.layoutparams params = new abslistview.layoutparams (   ABSLISTVIEW.LAYOUTPARAMS.FILL_PARENT, 100); textview.setlayoutparams (params); textview.settextcolor (Color.BLACK); Textview.settextsize (; ) textview.settext (Items[position]), Textview.setbackgroundcolor (0x88ff0000); textview.setgravity (gravity.left | gravity.center_vertical); } else { textview = new textview (context); Abslistview.layoutparams params = new abslistview.layoutparams (   ABSLISTVIEW.LAYOUTPARAMS.FILL_PARENT, 200); &nbsP;textview.setlayoutparams (params); textview.settextcolor (Color.Black); textview.settextsize (; ) textview.settext (Items[position]); textview.setvisibility ( view.visible); if (position %4 == 1) { // The row height of the GridView is determined by the height of the last element of the line AbsListView.LayoutParams params2 = new Abslistview.layoutparams (   &NBsp;        ABSLISTVIEW.LAYOUTPARAMS.FILL_PARENT, 100); Textview.setlayoutparams (PARAMS2); textview.setvisibility (view.invisible); } textview.setgravity (Gravity.CENTER); } return textView; } class mytext extends textview { public mytext (Context Context) { super (context); } @Override protected void onmeasure (Int widthmeasurespec, int  HEIGHTMEASURESPEC) { super.onmeasure (Widthmeasurespec, heightmeasurespec); int targetWidth = Mgridview.getmeasuredwidth ()     &NBsp; - mgridview.getpaddingleft () - Mgridview.getpaddingright (); widthmeasurespec = measurespec.makemeasurespec (targetwidth, measurespec.getmode (WIDTHMEASURESPEC)); super.onmeasure (WIDTHMEASURESPEC, HEIGHTMEASURESPEC); } } }}
Reprint: Android GridView Implementation Merge cell effect