Category: C #, Android, VS2015;
Date Created: 2016-02-19 I. INTRODUCTION
The grid view (GridView) is a view that adds scrolling functionality on top of GridLayout: The GridView is used to display items with a large number of cells in a scrollable 2D grid space (each item is a view), for example, each cell displays a picture, You can also scroll up and down to view.
To use the GridView, you need to write a view adapter (typically implemented with an adapter class that inherits from Baseadapter<t>), and then use it to populate each cell sequentially, for example, by inserting the picture into each cell in the first column (or after a column).
Ii. Example 1-gridview basic usage
This example shows how to create a grid of thumbnails using the GridView. When an item is selected, a toast message prompt showing the location of the image pops up. If you have more images, you can scroll up and down.
1. Operation
2. Design steps
(1) Add Ch1001_gridviewdemo.axml file
Add the file under the Resources/layout folder.
<?XML version= "1.0" encoding= "Utf-8"?><GridViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/gridview1"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:columnwidth= "90DP"Android:numcolumns= "Auto_fit"android:verticalspacing= "10DP"android:horizontalspacing= "10DP"Android:stretchmode= "ColumnWidth"android:gravity= "Center" />
The GridView created will automatically fill the entire screen.
(2) Add Ch1001MyImageAdapter.cs file
Add the file under the Srcdemos folder.
Here again, after typing the following code, click the triangle symbol on the right side of the small bulb, and then click "Implement Interface", it will automatically add the corresponding method, instead of your own 1.1-point knocking in. This is done wherever the interface needs to be implemented.
Class ch1001myimageadapte:baseadapter<int>
{
}
The complete code for this file is as follows:
usingandroid.content;usingandroid.views;usingAndroid.widget;namespacemydemos.srcdemos{classch1001myimageadapte:baseadapter<int>{context context; int[] Thumbids ={Resource.Drawable.ch05sample_2, Resource.Drawable.ch05sample_3, Resource.Drawable.ch0 5sample_4, Resource.Drawable.ch05sample_5, Resource.Drawable.ch05sample_6, Resource.Drawable.ch05sample_7, Resource.Drawable.ch05sample_0, Resource.Drawable.ch05sample_1, Resource.Drawable.ch05sample_2, Resource . Drawable.ch05sample_3, Resource.Drawable.ch05sample_4, Resource.Drawable.ch05sample_5, Resource.draw Able.ch05sample_6, Resource.Drawable.ch05sample_7, Resource.Drawable.ch05sample_0, Resource.Drawable.ch05sample _1, Resource.Drawable.ch05sample_2, Resource.Drawable.ch05sample_3, Resource.Drawable.ch05sample_4, Resource.Drawable.ch05sample_5, Resource.Drawable.ch05sample_6, Resource.Drawable.ch05sample_7}; Publicch1001myimageadapte (context C) {context=C; } Public Override int This[intposition] { Get{return 0; } } Public Override intCount {Get{returnThumbids.length;} } Public Override LongGetitemid (intposition) { return 0; } Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {ImageView ImageView; if(Convertview = =NULL) {ImageView=NewImageView (context); Imageview.layoutparameters=NewGridview.layoutparams ( $, $); Imageview.setscaletype (ImageView.ScaleType.CenterCrop); Imageview.setpadding (8,8,8,8); } Else{ImageView=(ImageView) Convertview; } imageview.setimageresource (Thumbids[position]); returnImageView; } }}
Code Description:
Typically, GetItem (int) should return a real object at the specified position in the adapter, and getitemid (int) should return the component's true number, but it is not available in this example, so it doesn't matter what value is returned.
Setlayoutparams (viewgroup.layoutparams) sets the height and width of the view, which ensures that the size and cut are properly resized regardless of the size of the original image.
Setscaletype (Imageview.scaletype) declares that the image will be cut in accordance with the center (if necessary).
setpadding (int, int, int, int) defines how the padding is filled. (Note that if the image has a different aspect ratio, when the image does not match the imageview given size, the smaller padding will result in more cuts in the image).
If the view passed to GetView () is not empty, ImageView is initialized with a reusable view.
At the end of the GetView () method, the passed-in position parameter is used to select an image from the mthumbids array that is used as the ImageView resource.
The rest is the thumbids array that defines the drawable resource.
You can also use Imageview.setadjustviewbounds (Boolean) to automatically adapt to the size of the image and use it simply to replace the Setlayoutparams () method directly with the statement.
(3) Add Ch1001GridViewDemo.cs file
Add the file under the Srcdemos folder.
usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespacemydemos.srcdemos{[Activity (Label="basic usage of "Example 10-1" Grid view")] Public classch1001gridviewdemo:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch1001_GridViewDemo); varGridView = findviewbyid<gridview>(RESOURCE.ID.GRIDVIEW1); Gridview. Adapter=NewCh1001myimageadapte ( This); Gridview. ItemClick+ = (s, e) = ={Toast.maketext ( This, E.position.tostring (), Toastlength.short). Show (); }; } }}
When you click an item in the grid, only the location index number (zero-based) of the selected grid is displayed in the Toast message box. In the actual program, the full-size image can be obtained from the location index number for other purposes.
Android 10.3 grid view (GridView)