We usually inherit baseadapter to write our own adapter class, because baseadapter class is the base class of other adapter classes. In the use of the World, we generally need to rewrite some of these methods:
Getcount (); get the number of items of the current Adapter
Getitem (INT position); obtains the item of the corresponding position
Getitemid (INT position); obtains the row ID of the item with the corresponding position in the list.
Getview (INT positon, view convertview, viewgroup parent); get the view of the data to be displayed in the specified position
The getview () method is very important. It mainly returns the view component after obtaining data, for example, you can read the attributes and data passed by imageview in each row of the defined gridview in this method.
The following is an example:
XML file:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: Tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical"
Tools: context = "com. example. gsllery. mainactivity">
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
<Gallery
Android: Id = "@ + ID/gallery1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: spacing = "5px"/>
<Imageview Android: Id = "@ + ID/imageview1"
Android: layout_gravity = "center_vertical"
Android: layout_margintop = "20px"
Android: layout_width = "320px"
Android: layout_height = "320px"
/>
</Linearlayout>
Main program file:
Package com. example. gsllery;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. Support. v7.app. actionbaractivity;
Import Android. util. log;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. adapterview;
Import Android. widget. adapterview. onitemclicklistener;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;
// How to use the adapter
Public class mainactivity extends actionbaractivity {
// Define components of gallery and imageview
Private gallery Gallery;
Private imageview imgview;
// Define the data source
Private int [] IMGs = {
R. drawable. F1,
R. drawable. F2,
R. drawable. F3,
R. drawable. F4,
R. drawable. F5,
R. drawable. F6,
R. drawable. F7,
R. drawable. F8,
R. drawable. F9,
};
// Called when the activity responds for the first time
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set an activity display interface in layout of the res folder.
Setcontentview (R. layout. activity_main );
Imgview = (imageview) findviewbyid (R. Id. imageview1 );
Gallery = (Gallery) findviewbyid (R. Id. gallery1 );
Myimgadapter adapter = new myimgadapter (this );
Gallery. setadapter (adapter );
// Send the image ID to the image View
Gallery. setonitemclicklistener (New onitemclicklistener (){
Public void onitemclick (adapterview <?> Arg0, view, int position, long arg3 ){
Imgview. setimageresource (IMGs [position]);
}
});
}
// The custom image adapter contains a category in mainactivity.
// Easy access to various variables in mainactivity, especially the IMGs Array
Class myimgadapter extends baseadapter {
// Define the context. In this example, only the main program is used.
Private context;
Public myimgadapter (context ){
Super ();
This. Context = context;
}
// Obtain the number of images
Public int getcount (){
Return IMGs. length;
}
// Obtain the image location in the library
Public object getitem (INT position ){
Return position;
}
// Obtain the image ID
Public long getitemid (INT position ){
Return position;
}
// When each item in the list is displayed on the page
// The getview method of the adapter is called to return a view.
Public View getview (INT position, view convertview, viewgroup parent ){
// Create an imageview instance for each data (that is, each image ID)
Imageview = new imageview (context );
// Set the image data source
Imageview. setimageresource (IMGs [position]);
// Write logs
Log. I ("magc", String. valueof (IMGs [position]);
// Set the size of each image in Gallery to 80x80.
Imageview. setlayoutparams (new gallery. layoutparams (80, 80 ));
// Set the display proportion type
Imageview. setscaletype (imageview. scaletype. fit_xy );
Return imageview;
}
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R. Menu. Main, menu );
Return true;
}
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the home/Up button, so long
// As you specify a parent activity in androidmanifest. xml.
Int id = item. getitemid ();
If (ID = R. Id. action_settings ){
Return true;
}
Return super. onoptionsitemselected (item );
}
}
Details about gallery instances in the adapter class