The GridView of Android

Source: Internet
Author: User

The GridView usage is basically similar to a ListView.

Program Layout file Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" >
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "@string/hello"/>
<gridview android:id= "@+id/gridview01" android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>
</LinearLayout>

Where the GridView layout file for each row is grid_row.xml as follows:

<?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= "Horizontal" >
<imageview android:id= "@+id/imageview01" android:scaletype= "Fitxy"
Android:layout_width= "50dip" android:layout_height= "50dip"/>
<textview android:id= "@+id/tv01" android:layout_width= "100dip"
android:layout_height= "Wrap_content" android:textsize= "24dip"
android:paddingleft= "5dip"/>
<textview android:id= "@+id/tv02" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:textsize= "24dip"
android:paddingleft= "5dip"/>
</LinearLayout>

Configure the adapter of the GridView in the main function:

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

GridView = (GridView) Findviewbyid (R.ID.GRIDVIEW01);
Simpleadapter adapter = new Simpleadapter (this, generatedatalist (),
R.layout.grid_row, new string[] {"Col1", "col2", "Col3"},
New int[] {r.id.imageview01, r.id.tv01, r.id.tv02});
Gridview.setadapter (adapter);
}

where Generatedatalist () generates the data in the adapter, whose type is list< Extends Map<string,? >>:

Private list<? Extends Map<string,?>> generatedatalist () {
TODO auto-generated Method Stub
Arraylist<map<string,object>> list=new arraylist<map<string,object>> ();
int rowcount=drawableids.length;
for (int i=0;i<rowcount;i++) {
Hashmap<string, object> hmap=new hashmap<string, object> ();
Hmap.put ("Col1", Drawableids[i]);
Hmap.put ("Col2", This.getresources (). getString (Nameids[i]));
Hmap.put ("Col3", This.getresources (). getString (Msgids[i]));
List.add (HMAP);
}
return list;
}

To add an event to the GridView:

Gridview. Setonitemselectedlistener (New Onitemselectedlistener () {

@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
TODO auto-generated Method Stub
TextView TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW01);
LinearLayout ll = (linearlayout)arg1;
TextView tv01 = (TextView) ll.getchildat (1);
TextView tv02 = (TextView) ll.getchildat (2);
StringBuilder sb = new StringBuilder ();
Sb.append (Tv01.gettext ());
Sb.append ("");
Sb.append (Tv02.gettext ());
Textview.settext (Sb.tostring ());

}

@Override
public void onnothingselected (adapterview<?> arg0) {
TODO auto-generated Method Stub

}
});
Gridview. Setonitemclicklistener (New Onitemclicklistener () {

@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
Long Arg3) {
TODO auto-generated Method Stub
LinearLayout ll = (linearlayout) arg1;
TextView tv01 = (TextView) ll.getchildat (1);
TextView tv02 = (TextView) ll.getchildat (2);
StringBuilder sb = new StringBuilder ();
Sb.append (Tv01.gettext ());
Sb.append ("");
Sb.append (Tv02.gettext ());
Toast.maketext (Mainactivity.this, sb.tostring (),
Toast.length_long). Show ();
}
});

The GridView for 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.