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