Android user interface-widget-grid view gridview

Source: Internet
Author: User

The content of the gridview is displayed by row or column. It is generally suitable for displaying icons. Image and other content, mainly used to set the adapter.

Use the baseadapter class of the basic class to override the getview () method to set the image display format.

 

/Chapter04_ui_gridview01/src/COM/Amaker/test/mainactivity. Java

Code

  Package  Com. Amaker. test;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. gridview;
Import Android. widget. imageview;

Public Class Mainactivity Extends Activity {
Private Gridview GV;

@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Gv = (Gridview) findviewbyid (R. Id. gridview01 );
GV. setnumcolumns ( 4 );
// GV. setnumcolumns (3 );
// String [] STRs = {"A", "A1", "A2", "B", "B1", "B2", "C", "C1 ", "C2 "};
// Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_gallery_item, STRs );
GV. setadapter ( New Myadapter ( This ));

}

Class Myadapter Extends Baseadapter {

Private Integer [] IMGs = {
R. drawable. gallery_photo_1,
R. drawable. gallery_photo_2,
R. drawable. gallery_photo_3,
R. drawable. gallery_photo_4,
R. drawable. gallery_photo_5,
R. drawable. gallery_photo_6,
R. drawable. gallery_photo_7,
R. drawable. gallery_photo_8,

R. drawable. gallery_photo_1,
R. drawable. gallery_photo_2,
R. drawable. gallery_photo_3,
R. drawable. gallery_photo_4,
R. drawable. gallery_photo_5,
R. drawable. gallery_photo_6,
R. drawable. gallery_photo_7,
R. drawable. gallery_photo_8
};
Context context;
Myadapter (context ){
This . Context = Context;
}
Public Int Getcount (){
Return IMGs. length;
}

Public Object getitem ( Int ITEM ){
Return Item;
}

Public Long Getitemid ( Int ID ){
Return ID;
}

Public View getview ( Int Position, view convertview, viewgroup parent ){
Imageview;
If (Convertview = Null ){
Imageview = New Imageview (context );
Imageview. setlayoutparams ( New Gridview. layoutparams ( 45 , 45 ));
Imageview. setadjustviewbounds ( False );
Imageview. setscaletype (imageview. scaletype. center_crop );
Imageview. setpadding ( 8 , 8 , 8 , 8 );
} Else {
Imageview = (Imageview) convertview;
}
Imageview. setimageresource (IMGs [position]);
Return Imageview;
}
}
}

 

Related Article

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.