Android & lt; GridView grid view from scratch. Twenty-eight. & gt;, androidgridview

Source: Internet
Author: User

Learn android <GridView grid view. Twenty-eight ..> from scratch, androidgridview
The GridView component displays all components in a grid. For example, when creating an album, all the images are displayed in different grids of the same size, the inheritance structure of this component is as follows: java. lang. object upload android. view. view every android. view. viewGroup uses android. widget. adapterView <T extends android. widget. adapter> logging android. widget. absListView uses android. widget. common Methods of GridView

1 Public GridView (Context context) Structure

 

Create a GridView object
2 Public void setStretchMode (int stretchMode) Normal Android: stretchMode Scaling Mode
3 Public void setVerticalSpacing (int verticalSpacing) Normal Android: verticalSpacing Set Vertical spacing
4 Public void setHorizontalSpacing (int horizontalSpacing) Normal Android: horizontalSpacing Set Horizontal spacing
5 Public void setNumColumns (int numColumns) Normal Android: numColumns Sets the data volume displayed in each column. If it is set to auto_fit, it is automatically set.
6 Public void setSelection (int position) Normal

 

Set the selected items by default
7 Public void setGravity (int gravity) Normal Android: gravity Sets the alignment mode, which is specified by the Gravity class.
8 Public void setAdapter (ListAdapter adapter) Normal

 

Set the display image set


Of course, to use the GridView, you must use the Adapter dataset class to configure image data.
You can use SimpleAdapter and BaseAdapter to configure the adapter. Today, I use BaseAdapter to configure the Adapter data.
XML file
<span style="font-size:18px;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <GridView        android:id="@+id/gridview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:stretchMode="columnWidth"        android:numColumns="4"         /></RelativeLayout></span>

JAVA files
<Span style = "font-size: 18px;"> package com. example. gridview; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. view. viewGroup. layoutParams; import android. widget. adapterView; import android. widget. adapterVie W. onItemClickListener; import android. widget. baseAdapter; import android. widget. gridView; import android. widget. imageView; public class MainActivity extends Activity {private GridView gridView; // initialize GridViewprivate int images [] = {R. drawable. a1, R. drawable. a2, R. drawable. a3, R. drawable. a4, R. drawable. a5, R. drawable. a6, R. drawable. a7, R. drawable. a8, R. drawable. a9, R. drawable. a10, R. drawable. a11, R. drawab Le. a12, R. drawable. a13, R. drawable. a14, R. drawable. a15, R. drawable. a16}; // image data @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the GridView object gridView = (GridView) this. findViewById (R. id. gridview); // set AdptergridView. setAdapter (new MyAdapter (); // sets the listener event gridView. setOnItemClickListener (new OnItemClickListener (){ @ Overridepublic void onItemClick (AdapterView <?> AdapterView, View view, int position, long id) {// TODO Auto-generated method stub // set the view space ImageView image = new ImageView (MainActivity. this); // sets the image information. setImageResource (images [position]); // you can specify the image size alignment mode. setScaleType (ImageView. scaleType. CENTER); // set the image size. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); // create dialog box AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this ). setTitle ("view image "). setView (image ). setIcon (android. r. drawable. btn_star_big_on ). setNegativeButton ("off", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub }}); // create a dialog box builder. create (); // display the dialog box builder. show () ;}});} class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn images. length ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position;} @ Overridepublic View getView (int position, View myView, ViewGroup parent) {// TODO Auto-generated method stub // return imageView object ImageView imageView = new ImageView (MainActivity. this); imageView. setImageResource (images [position]); imageView. setScaleType (ImageView. scaleType. CENTER); return imageView ;}}</span>

Effect
You can use the GridView component to achieve a colorful menu effect.

Next prediction: TabHost tag component


Android uses the GridView mesh view (such as the nine cells). Can the custom view be used to fill the view?
Android GridView grid layout. You can add or delete a grid,

Mainly data records. You can re-load the gridview data once in the trigger event returned by adding the data to implement the Add function.

Other operations, such as clicking a button to enter the second-level page, and sliding left and right to display hidden content, are all operations triggered by page layout and layout.

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.