Reprint: Android GridView Implementation Merge cell effect

Source: Internet
Author: User

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

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.