GridView of Android Control

Source: Internet
Author: User


GridView is a data display control in Android. First try it out.

 


 

This is the effect of displaying only images in the GridView. Source code download http://www.bkjia.com/uploadfile/2012/0425/20120425103107648.rar
 

The following describes the GridView in detail.

I. Introduction

The Item is displayed in a two-dimensional scrollable mesh. The Item comes from the related ListAdapter.

Ii. Important Methods

GetStretchMode (): Get the Extended Mode of the GridView.

OnKeyDown (int keyCode, KeyEvent event): Default KeyEvent. Callback. onKeyMultiple ()

Iii. Specific applications

1. description in the layout File

<GridView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/grid"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: padding = "10dp"
Android: verticalSpacing = "10dp"

Android: horizontalSpacing = "10dp"
Android: numColumns = "auto_fit"
Android: columnWidth = "60dp"
Android: stretchMode = "columnWidth"

Android: gravity = "center"
/>

 

2. Program usage

Private GridView mGrid;

MGrid = (GridView) findViewById (R. id. grid );

 

3. Define the adapter

Public class extends adapter extends BaseAdapter {
Public writable adapter (){
}

Public View getView (int position, View convertView, ViewGroup parent ){
ImageView I;

If (convertView = null ){
I = new ImageView (GridDemo. this );
I. setScaleType (ImageView. ScaleType. FIT_CENTER );
I. setLayoutParams (new GridView. LayoutParams (50, 50 ));
} Else {
I = (ImageView) convertView;
}

ResolveInfo info = mApps. get (position );
I. setImageDrawable (info. activityInfo. loadIcon (getPackageManager ()));

Return I;
}

Public final int getCount (){
Return mApps. size ();
}

Public final Object getItem (int position ){
Return mApps. get (position );
}

Public final long getItemId (int position ){
Return position;
}
}

 

4. Application Adapter

MGrid. setAdapter (new container adapter ());

 

5. retrieve images

Private void loadApps (){
Intent mainIntent = new Intent (Intent. ACTION_MAIN, null );
MainIntent. addCategory (Intent. CATEGORY_LAUNCHER );

MApps = getPackageManager (). queryIntentActivities (mainIntent, 0 );
}

 

From the eternal memory

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.