Android ApiDemos example resolution (121): Views-& gt; Grid-& gt; 1. Icon

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201208/147770.html

Previously, ListView, Gallery, and Spinner are subclasses of AdapterView. In this example, GridView is also a subclass of AdapterView. The display of AdapterView can be achieved through data binding. The data source can be an array or database record, and the data source and AdapterView can be a bridge through Adapter. Using the Adapter, AdatperView can display data sources or process user selection events, such as selecting an item in the list.

 

All AdapterView data sources use adapters as a bridge. Different adapterviews only display different data sources in different ways. ListView is displayed in a list, and Gallery is displayed in a horizontal Gallery, the GridView is displayed in a two-dimensional mesh. By default, the GridView automatically calculates the number of each column based on the displayed View size. You can also specify the number of columns through setNumColumns (int numColumns) in the GridView, or setColumnWidth (int columnWidth) specifies the column width.

In this example, the Development adapter is used to read the icons of all applications in the App Launcher. GetView of the Adapter returns an ImageView:

[Java]
Public View getView (int position, View convertView,
ViewGroup parent ){
ImageView I;
 
If (convertView = null ){
I = new ImageView (Grid1.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 View getView (int position, View convertView,
ViewGroup parent ){
ImageView I;

If (convertView = null ){
I = new ImageView (Grid1.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;
}
The Adapter can return any type of View, such as Button and TextView. The view returned by the GridView or getView of the Adapter is used to display each item.

Use setAdapter to set the data source for the GridView.

[Java]
SetContentView (R. layout. grid_1 );
MGrid = (GridView) findViewById (R. id. myGrid );
MGrid. setAdapter (new container adapter ());

SetContentView (R. layout. grid_1 );
MGrid = (GridView) findViewById (R. id. myGrid );
MGrid. setAdapter (new container adapter ());
For example, if the result is changed to ListView, the application icon is displayed as a list. ListView and GridView use the same data source. The difference is the form (list or grid ). In this way, we can see that the corresponding data source, Android can support a variety of forms.

 

 

 

 

Previously, ListView, Gallery, and Spinner are subclasses of AdapterView. In this example, GridView is also a subclass of AdapterView. The display of AdapterView can be achieved through data binding. The data source can be an array or database record, and the data source and AdapterView can be a bridge through Adapter. Using the Adapter, AdatperView can display data sources or process user selection events, such as selecting an item in the list.

 

All AdapterView data sources use adapters as a bridge. Different adapterviews only display different data sources in different ways. ListView is displayed in a list, and Gallery is displayed in a horizontal Gallery, the GridView is displayed in a two-dimensional mesh. By default, the GridView automatically calculates the number of each column based on the displayed View size. You can also specify the number of columns through setNumColumns (int numColumns) in the GridView, or setColumnWidth (int columnWidth) specifies the column width.

In this example, the Development adapter is used to read the icons of all applications in the App Launcher. GetView of the Adapter returns an ImageView:

[Java]
Public View getView (int position, View convertView,
ViewGroup parent ){
ImageView I;
 
If (convertView = null ){
I = new ImageView (Grid1.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 View getView (int position, View convertView,
ViewGroup parent ){
ImageView I;

If (convertView = null ){
I = new ImageView (Grid1.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;
}
The Adapter can return any type of View, such as Button and TextView. The view returned by the GridView or getView of the Adapter is used to display each item.

Use setAdapter to set the data source for the GridView.

[Java]
SetContentView (R. layout. grid_1 );
MGrid = (GridView) findViewById (R. id. myGrid );
MGrid. setAdapter (new container adapter ());

SetContentView (R. layout. grid_1 );
MGrid = (GridView) findViewById (R. id. myGrid );
MGrid. setAdapter (new container adapter ());
For example, if the result is changed to ListView, the application icon is displayed as a list. ListView and GridView use the same data source. The difference is the form (list or grid ). In this way, we can see that the corresponding data source, Android can support a variety of forms.

 
Author: mapdigit

 

 

 

 



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.