Go Android grid view (GridView)

Source: Internet
Author: User

Some properties of the GridView:

1.android:numcolumns= "Auto_fit"//gridview the number of columns is set to Automatic, can also be set to 2, 3, 4 ...

2.android:columnwidth= "90DP"//width of each column, which is the width of item

3.android:stretchmode= "columnWidth"//zoom is synchronized with column width size

4.android:verticalspacing= the margin between "10DP"//Two rows

5.android:horizontalspacing= margin between "10DP"//Two columns

6.android:cachecolorhint= "#00000000"//Remove the default black background when dragging

7.android:listselector= "#00000000"//Remove the yellow background when selected

8.android:scrollbars= "None"//Hide GridView scroll bar

9.android:fadescrollbars= "true"//set to True to enable auto-hide and display of scroll bars

10.android:fastscrollenabled= "true"//gridview a fast scrolling button (at least 4 page scrolling is displayed)

11.android:fadingedge= "None"//gridview fading (Fade) edge color is empty, the default value is vertical. (Can be understood as the top and bottom edge of the hint color)

12.android:fadingedgelength= "10dip"//defines the length of the fading (fade) edge

13.android:stackfrombottom= "true"//set to True, your list will show the bottom of your list

14.android:transcriptmode= "Alwaysscroll"//When you add data dynamically, the list will automatically scroll down the latest entries to automatically scroll to the viewable range

15.android:drawselectorontop= "false"//click on a record, the color will be the background color after the record, the text of the content is visible (default is False)

The following example shows the program icon and caption:

Child element layout:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">    < ImageView       android:id= "@+id/itemimage"      android:layout_width= "80DP"      android:layout_height= "80DP"      android:layout_centerhorizontal= "true"/>    <textview       android:id= "@+id/itemname"      android: layout_below= "@id/itemimage"      android:layout_width= "wrap_content"      android:layout_height= "wrap_content "      android:layout_centerhorizontal=" true "/>    </RelativeLayout>  

GridView layout, 3 per column:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >    <gridview        android:id= "@+id/gridview"        android:layout_ Width= "Match_parent"        android:layout_height= "wrap_content"        android:horizontalspacing= "2DP"        android: numcolumns= "3"        android:verticalspacing= "2DP"/></linearlayout>

Data binding, given click event:

Package Com.example.androidgridview;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Java.util.map;import Android. R.string;import Android.os.bundle;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.bitmapfactory.options;import Android.view.Menu;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.gridview;import Android.widget.simpleadapter;import Android.widget.toast;public class MainActivity    Extends Activity {private GridView GridView;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                        GridView = (GridView) This.findviewbyid (R.id.gridview);        list<map<string, object>> item = GetData (); The Simpleadapter object that matches the element in the ArrayList simpleadapter SimplEadapter = new Simpleadapter (this, item, R.layout.gridviewitem, new string[] {"Itemimage", "ItemName"}, new int[] {r.id        . Itemimage, R.id.itemname});        Gridview.setadapter (Simpleadapter); Add Click event Gridview.setonitemclicklistener (New Onitemclicklistener () {@Override public void O Nitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {int index = ARG2 + 1;//ID is starting from 0, so            Requires +1 Toast.maketext (Getapplicationcontext (), "You pressed the option:" + index, 0). Show ();    }        }); /** * Save icon Picture and icon name in ArrayList * * @return */private list<map<string, object>> GetData        () {list<map<string, object>> items = new arraylist<map<string, object>> (); int[] listimg = new int[] {r.drawable.browser, r.drawable.email, R.drawable.camera, R.drawable.clock, R.drawable.contacts, R.drawable.gallery, R.drawable.gmail, R.drawable.gtalk, R.drawable.maps, R.DRawable.market, R.drawable.messages, R.drawable.phone, R.drawable.voice, R.drawable.music, R.drawable.calender}; string[] ListName = new string[] {"Nine blue", "nine email", "Nine Firfox", "Nine Gongju", "Nine Heguo", "Nine Kaiguan", "Nine LeTV", "nine Libra        Y "," Nine List "," Nine Luxiang "," Nine Main "," Nine Ooo "," Nine Twite "," Nine Voda "," Nine WiFi "}; for (int i = 0; i < listimg.length; i++) {map<string, object> item = new hashmap<string, Object&gt            ;();            Item.put ("Itemimage", Listimg[i]);            Item.put ("ItemName", Listname[i]);        Items.Add (item);    } return items; } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; }}

Operating effect:

Download Image Resources

When running the error:

81000-byte external allocation too large for this process.

VM won ' t let us allocate 81000 bytes

Google after getting information is Android Dalvik VM for an application to provide about 16MB of memory, generally we handle more than 8MB of the picture will appear OutOfMemoryError exception (memory overflow exception)

One of the methods: Http://stackoverflow.com/questions/8250704/external-allocation-too-large-for-this-process

In the online search, refer to the method of netizens, the usual reason is that we loaded the picture is too big, I looked at my picture, with 500W pixels of the defy, the original size is generally around 800KB, such a large picture led to memory leaks, the usual solution to the online gods are, With Bitmapfactory decoding with the SET options command (I just don't have options), usually the options are as follows:

Bitmapfactory.options options=new Options ();
Options.indither=false; /* Do not image jitter processing */
Options.inpreferredconfig=null; /* Settings let the decoder decode in the best way */
options.insamplesize=4; /* Picture length/Width reduction multiplier */
Bitmap img=bitmapfactory.decodebytearray (buffer, 0, buffer.length, options);

After this modification, I found that the image uploaded to the phone is generally about 200KB, that is, reduced to the original 1/4, and looks OK, no distortion, hardly affect the viewing effect, the problem also solved ~

By the way, it's better to call the Recycle () method after you've finished using bitmap, which is more useful for memory recycling.

The pictures I use here are more than 80KB, there is drawable folder, directly with R.drawable. To invoke, useless to bitmap, so you can only reduce the size of the picture.

«Previous: List View (ListView and Listactivity)
» Next post: Resources Management in Android

Go Android grid view (GridView)

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.