Android Normal and cyclic gallery

Source: Internet
Author: User

:

Code

Main.java

Package Com.example.gallerydemo;import Android.app.activity;import Android.content.context;import Android.os.bundle;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;import Android.widget.textview;import Android.widget.toast;import Android.widget.imageview.scaletype;public class Main Extends Activity {private Gallery Gallery = null;private Gallery galleryloop = null;private TextView tvnum = null; @Overrid eprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); tvnum = (TextView) Findviewbyid (r.id.num); gallery = (gallery) Findviewbyid (r.id.gallery); Gallery.setadapter (New Imageadapter (this)); gallery.setspacing (5); Gallery.setonitemclicklistener (new Onitemclicklistener () {public void ONITEMCLIck (adapterview<?> arg0, View arg1, int arg2,long arg3) {int num = arg2 + 1;//toast.maketext (Main.this, "clicked" + num , Toast.length_short)//. Show (); Tvnum.settext ("General gallery: clicked" + num);}); Galleryloop = (Gallery) Findviewbyid (R.id.galleryloop); Galleryloop.setadapter (new Loopimageadapter (this)); Galleryloop.setspacing (Galleryloop.setonitemclicklistener) (new Onitemclicklistener () {public void OnItemClick ( Adapterview<?> arg0, View arg1, int arg2,long arg3) {int num = (arg2 + 1)% 9;if (num = 0) {num = 9;} Toast.maketext (Main.this, "clicked" + num, Toast.length_short)//. Show (); Tvnum.settext ("Loop gallery: Clicked" + num);});} General Gallery Adapterclass Imageadapter extends Baseadapter {private context Context;public Imageadapter (context context {this.context = context;} Private integer[] Imageinteger = {r.drawable.pic_1, r.drawable.pic_2,r.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,r.drawable.pic_6, r.drawable.pic_7, r.drawable.pic_8,r.drawable.pic_9};p ublic Object getItem (iNT position) {return position;} public long getitemid (int position) {return position;} public int GetCount () {return imageinteger.length;} Public View GetView (int position, View Convertview, ViewGroup parent) {ImageView ImageView = new ImageView (context); Imagev Iew.setimageresource (Imageinteger[position]); Imageview.setscaletype (ImageView.ScaleType.FIT_XY); Imageview.setlayoutparams (new Gallery.layoutparams); return imageView;}} Loop Gallery Adapterclass Loopimageadapter extends Baseadapter {private Context context;public loopimageadapter ( Context context) {This.context = context;} Private integer[] Imageinteger = {r.drawable.pic_1, r.drawable.pic_2,r.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,r.drawable.pic_6, r.drawable.pic_7, r.drawable.pic_8,r.drawable.pic_9};p ublic Object getItem (int Position) {return position;} public long getitemid (int position) {return position;} public int GetCount () {return integer.max_value;} Public View GetView (int position, view Convertview, ViewgroUp parent) {ImageView ImageView = new ImageView (context); Imageview.setimageresource (imageinteger[position% Imageinteger.length]); Imageview.setscaletype (ImageView.ScaleType.FIT_XY); Imageview.setlayoutparams (new Gallery.layoutparams (+)); return ImageView;}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic 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 and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}

Main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" tools:context= "Com.example.gallerydemo.Main" > <textview android:id= "@+id/num" a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/ > <imageview android:layout_width= "50dp" android:layout_height= "50DP" android:layout_gravity = "Center" android:src= "@drawable/arrow_down"/> <gallery android:id= "@+id/gallery" android:l         Ayout_width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "Bottom"/> <Gallery Android:id= "@+id/galleryloop" android:layout_width= "fill_parent" android:layout_height= "wrap_content "android:gravity=" Bottom "/> <ImageView android:layout_width= "50DP" android:layout_height= "50DP" android:layout_gravity= "center" android:src= "@drawable/arrow_up"/></linearlayout>


Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui

My github: Zhou Mushi's GitHub Https://github.com/zhoumushui


Android Normal and cyclic gallery

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.