BaseAdapter for Android GridView lattice View

Source: Internet
Author: User

① Create a project
② Images named a.png---p.png In the res/drawabledirectory
③ Modify the main. xml layout and add a GridView and an ImageView.
<? Xml version = "1.0" encoding = "UTF-8"?>
<AbsoluteLayout
Android: id = "@ + id/widget0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<GridView
Android: id = "@ + id/grid"
Android: layout_width = "fill_parent"
Android: padding = "30dip"
Android: columnWidth = "52px"
Android: layout_height = "210px"
Android: numColumns = "5">
<! -- Set GridView to 30pid with the Five-column margin -->
</GridView>
<ImageView
Android: id = "@ + id/ImageView_Big"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_x = "95px"
Android: layout_y = "250px">
</ImageView>
</AbsoluteLayout>
Copy code

④ Definition and instantiation of GridView
/* Define class object */
Private GridView my_gridview;
/* Get the UI resource object from xml */
My_gridview = (GridView) findViewById (R. id. grid );
Copy code

⑤ Set the image content in the GridView and ImageAdapter

/* Create a custom ImageAdapter */
MyImageViewAdapter = new ImageAdapter (this );
/* Set an ImageAdapter for the GridView object */
My_gridview.setAdapter (myImageViewAdapter );
Copy code
6 internal class ImageAdapter, implements BaseAdapter
Private class myImageAdapter extends BaseAdapter {
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return 0;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return 0;
}
@ Override
Public View getView (int position,
View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
Return null;
}
}
Copy code

7. process the Items Click Event of the image in the GridView
/* Add an Items Click Event listener for the GridView */
My_gridview.setOnItemClickListener (this );
@ Override
Public void onItemClick (AdapterView <?> Arg0,
View arg1, int arg2, long arg3 ){
// TODO Auto-generated method stub
/* Click Items event processing for images in the GridView */
}
Copy code


Processing of the selected image Items event after moving the processed GridView
/* Add an image Items to move the selected event listener for the GridView */
My_gridview.setOnItemSelectedListener (this );
@ Override
Public void onItemSelected (AdapterView <?> Arg0,
View arg1, int arg2, long arg3 ){
// TODO Auto-generated method stub
/* Process the selected current event in the move focus of the image in the GridView */
}
/* Image Items event processing in the GridView is not selected */
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
----------------------------- The code is as follows --------------------------------
Copy code

Modify mainActivity. java to achieve the effects of image clicking and Image Moving.
Package zyf. GridViewTest;
/* Import the package to be used */
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. content. DialogInterface. OnClickListener;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AdapterView;
Import android. widget. BaseAdapter;
Import android. widget. GridView;
Import android. widget. ImageView;
Public class GridViewTest extends Activity implements
GridView. OnItemClickListener,
GridView. OnItemSelectedListener {
/** Called when the activity is first created .*/
/* Define class object */
Private GridView my_gridview;
Private ImageView big_imageView;
Private ImageAdapter myImageViewAdapter;
/* Internal class to implement an image adapter */
Public class ImageAdapter extends BaseAdapter {
/* MyContext is the context */
Private Context myContext;
/* The ImageView used by the GridView to load the image */
Private ImageView the_imageView;
// This is an array of image resource IDs.
Private Integer [] mImageIds = {
R. drawable. a, R. drawable. B, R. drawable. c, R. drawable. d,
R. drawable. e, R. drawable. f, R. drawable. g, R. drawable. h,
R. drawable. I, R. drawable. j, R. drawable. k, R. drawable. l,
R. drawable. m, R. drawable. n, R. drawable. o, R. drawable. p
};
/* Constructor */
Public ImageAdapter (Context myContext ){
// TODO Auto-generated constructor stub
This. myContext = myContext;
/* Input a Context. In this example, the input is GridViewTest */
}
/* Return the length of the resource ID array */
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return mImageIds. length;
}
/* Get Item */
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
/* Get the Items ID */
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}

 
// Process each item in the gridview
/* Get the View object to be displayed */
@ Override
Public View getView (int position,
View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
/* Create an ImageView */
The_imageView = new ImageView (myContext );
// Set the image source from the resource ID.
The_imageView.setImageResource (mImageIds [position]);
/* Adapt the ImageView to the boundary */
The_imageView.setAdjustViewBounds (true );
/* Set the style of the background image */
The_imageView.setBackgroundResource (
Android. R. drawable. picture_frame );
/* Return the ImageView with multiple image IDs */
Return the_imageView;
}
/* Obtain the image ID at the specified position */
Public Integer getcheckedImageIDPostion (int theindex ){
Return mImageIds [theindex];
}
}
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
/* Set the homepage Layout */
SetContentView (R. layout. main );
/* Get the UI resource object from xml */
My_gridview = (GridView) findViewById (R. id. grid );
Big_imageView =
(ImageView) findViewById (R. id. ImageView_Big );
/* Create a custom ImageAdapter */
MyImageViewAdapter = new ImageAdapter (this );
/* Set an ImageAdapter for the GridView object */
My_gridview.setAdapter (myImageViewAdapter );
/* Add an Items Click Event listener for the GridView */
My_gridview.setOnItemClickListener (this );
/* Add an image Items to move the selected event listener for the GridView */
My_gridview.setOnItemSelectedListener (this );
}
@ Override
Public void onItemClick (AdapterView <?> Arg0,
View arg1, int arg2, long arg3 ){
/* Click the Items icon in the GridView and an AlterDialog prompt box is displayed */
New AlertDialog. Builder (this)
. SetTitle ("Image Browsing ")
/* Obtain the corresponding image and display it */
. SetIcon (myImageViewAdapter. getcheckedImageIDPostion (arg2 ))
/* Add a button */
. SetPositiveButton ("return", new OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog,
Int which ){
}
/* Display prompt box */
}). Show ();
}
@ Override
Public void onItemSelected (AdapterView <?> Arg0,
View arg1, int arg2, long arg3 ){
// TODO Auto-generated method stub
/* When the move focus of an image in the GridView is selected,
* The Image view in the big image below shows the corresponding large image */
Big_imageView.setImageResource (myImageViewAdapter
. GetcheckedImageIDPostion (arg2 ));
}
/* Image Items event processing in the GridView is not selected */
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
}
Copy code

Explain result

Author: t80t90s

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.