How to use the GridView in Android

Source: Internet
Author: User

Layout file Activity_main.xml
<Relativelayout 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:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    Tools:context=". Mainactivity ">    <GridViewandroid:id="@+id/gridview"android:layout_width="Fill_ Parent "android:layout_height=" Fill_parent "android:layout_margintop=" 16DP "  android:numcolumns="4"android:stretchmode="ColumnWidth" >                                                     </GridView></relativelayout>
Gridview_item.xml

This is an item of the cell style, with pictures and text

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical" >                <imageview  android:id
      = "@+id/image"  android:layout_width  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:paddingleft  =" 10DP " android:scaletype  =" Fitcenter " android:src  =" @drawable/ic_ Launcher "/>     <TextViewandroid:id= "@+id/textview"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_gravity=" Center "android:text="TextView " />                                        </linearlayout>
Mainactivity.java
Import Java. Util. ArrayList;Import Java. Util. HashMap;Import Java. Util. List;Import Java. Util. Map;Import Android. App. Activity;Import Android. OS. Bundle;Import Android. Widgets. GridView;public class Mainactivity extends Activity {private GridView GridView;Private Gridviewadapter Adapter;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Set the adapter's picture resource int[] imageId = new int[] {R. drawable. Chat_tool_camera, R. drawable. Chat_tool_location, R. drawable. Chat_tool_paint, R. drawable. Chat_tool_video, R. drawable. Chat_tool_voice, R. drawable. Chat_tool_camera, R. drawable. Chat_tool_location, R. drawable. Chat_tool_paint, R. drawable. Chat_tool_video, R. drawable. Chat_tool_voice, R. drawable. Chat_tool_camera, R. drawable. Chat_tool_location, R. drawable. Chat_tool_paint, R. drawable. Chat_tool_video, R. drawable. Chat_tool_voice, R. drawable. Chat_tool_camera, R. drawable. Chat_tool_location, R. drawable. Chat_tool_paint, R. drawable. Chat_tool_video, R. drawable. Chat_tool_voice};Set Title string[] title = new String[] {"Camera","Positioning","Brushes","Video","Sound","Camera","Positioning","Brushes","Video","Sound","Camera","Positioning","Brushes","Video","Sound","Camera","Positioning","Brushes","Video","Sound"};list<map<string, object>> ListItem = new arraylist<map<string, object>> ();Convert the above resource to the list set for (int i =0; i < title.length; i++) {map<string, object> map = new hashmap<string, object> ();Map. Put("Image", Imageid[i]);Map. Put("title", Title[i]);ListItem. Add(map);} adapter = new Gridviewadapter (mainactivity. this, ListItem);GridView = (GridView) this. Findviewbyid(R. ID. GridView);Gridview. Setadapter(adapter);}}
Gridviewadapter.java

This is the adapter.

ImportJava.util.List;ImportJava.util.Map;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public  class gridviewadapter extends baseadapter {    PrivateContext context;PrivateList<map<string, object>> ListItem; Public Gridviewadapter(Context context,list<map<string, object>> ListItem) { This. Context = Context; This. ListItem = ListItem; }@Override     Public int GetCount() {returnListitem.size (); }@Override     PublicObjectGetItem(intPosition) {returnListitem.get (position); }@Override     Public Long Getitemid(intPosition) {returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {if(Convertview = =NULL{Convertview = Layoutinflater.from (context). Inflate (R.layout.gridview_item,NULL);        } ImageView ImageView = (ImageView) Convertview.findviewbyid (r.id.image);        TextView TextView = (TextView) Convertview.findviewbyid (R.id.textview);        map<string, object> map = listitem.get (position); Imageview.setimageresource ((Integer) Map.get ("Image")); Textview.settext (Map.get ("title") +"");returnConvertview; }}

As follows:

How to use the GridView in Android

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.