Android learning notes: view the usage of GridView, androidgridview

Source: Internet
Author: User

Android learning notes: view the usage of GridView, androidgridview


(1) The layout creation code is as follows:

<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="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:numColumns="3" >    </GridView></RelativeLayout>

(2) Use the layout in the MainActivity class

Package com. example. layoutstudy; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. adapter; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. adapterViewFlipper; import android. widget. baseAdapter; import android. widget. gridView; import andro Id. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {private GridView; private ImageAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gridView = (GridView) this. findViewById (R. id. gridView); adapter = new ImageAdapter (); // sets an adapter gridView. setAdapter (adap Ter);/** create a click event. When you click an image, its position is displayed */gridView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (MainActivity. this, "-->" + position, 1 ). show () ;}}) ;}// define the image resource private int [] images = {R. drawable. r1, R. drawable. r2, R. drawable. r3, R. drawable. r4, R. drawable. r5, R. drawable. r6, R. drawable. r7, R. drawable. r8};/** adapter definition, which must inherit BaseAdapter */public class ImageAdapter extends BaseAdapter {public ImageAdapter () {}@ Overridepublic int getCount () {return images. length ;}@ Overridepublic Object getItem (int position) {return images [position] ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {/** 1. manually create an object 2. load xml file */ImageView imageView = null; if (convertView = null) {imageView = new ImageView (MainActivity. this);} else {imageView = (ImageView) convertView;} // you can specify the height of the sub-Image view displayed in the GridView. setLayoutParams (new GridView. layoutParams (200,200); imageView. setScaleType (ImageView. scaleType. CENTER_CROP); imageView. setPadding (30, 20, 20, 20); imageView. setImageResource (images [position]); return imageView; }}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


(3) The running effect is as follows:



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.