The general idea is: since there is no set method in the gridview, add it directly to the item and change the Display Effect of the item through other methods to achieve the method of setting the split line ~ Not much nonsense ~ The following is a detailed introduction: the layout of the gridview, Android: numcolumns = "3" three columns per row
<GridView android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="match_parent" android:numColumns="3" > </GridView>
Next is the layout of items.
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="105dp" android:layout_marginBottom="13dp" android:layout_marginTop="13dp" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="105dp" android:scaleType="centerCrop" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="bottom" android:background="#50000000" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:maxLines="2" android:paddingLeft="10dp" android:paddingRight="10dp" android:textColor="@color/text_color" android:textSize="16sp" /> </RelativeLayout> </FrameLayout> <View android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="1px" android:background="@color/line_color" /></LinearLayout>
Next, the most important data is displayed. Customize a gridviewadapter to inherit from
Public class gridviewadapter extends baseadapter {private layoutinflater Inflater; private context; private list <Map <string, Object> List = new arraylist <Map <string, Object> (); public gridviewadapter (context, list <Map <string, Object> List) {super (); this. context = context; this. list = List; Inflater = layoutinflater. from (context) ;}@ override public int getcount () {return list. size () ;}@ override public object getitem (INT position) {return list. get (position) ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup arg2) {viewholder = NULL; If (convertview = NULL) {convertview = Inflater. inflate (R. layout. gridview_item, null); viewholder = new viewholder (convertview); convertview. settag (viewholder);} else {viewholder = (viewholder) convertview. gettag ();} viewholder. update (list. get (position), position); Return convertview;} public class viewholder {private imageview; private textview; private framelayout layout; private view; Public viewholder (view convertview) {imageview = (imageview) convertview. findviewbyid (R. id. imageview); textview = (textview) convertview. findviewbyid (R. id. textview); layout = (framelayout) convertview. findviewbyid (R. id. layout); view = convertview. findviewbyid (R. id. line);} public void Update (Map <string, Object> map, int position) {textview. settext (map. get ("name "). tostring (); int I = 0; I = position % 3; Switch (I) {Case 0: // The first item in each column does not include the right inner margin of layout of the split line, which is 20 layout. setpadding (0, 0, 20, 0); break; Case 1: // the second item in each column does not include 10 layout for each left and right padding of the split line. setpadding (10, 0, 10, 0); break; Case 2: // The third item in each column does not include the layout left margin of the split line 20; the width of the three items must be consistent with that of layout. setpadding (20, 0, 0, 0); break; default: break;} string id = map. get ("ID "). tostring (); setimage (ID, imageview); setline (Position, view) ;}} private void setimage (string ID, imageview) {If (ID. equals ("1") {imageview. setimageresource (R. drawable. img1);} else if (ID. equals ("2") {imageview. setimageresource (R. drawable. img2);} else if (ID. equals ("3") {imageview. setimageresource (R. drawable. img3);} else if (ID. equals ("4") {imageview. setimageresource (R. drawable. img4);} else if (ID. equals ("5") {imageview. setimageresource (R. drawable. img5) ;}} private void setline (INT position, view) {int I = 0; I = List. size () % 3; if (Position + I + 1> list. size () {// hide the view on the last line. setvisibility (view. gone );}}
From: http://www.easymorse.com/index.php/archives/2120