Android advanced 2 gallery Infinite Loop

Source: Internet
Author: User

Before that, I have published a regular Gallery. If you have not learned this before, you can view the blog: The Gallery of the android learning notes.

This article details what gallery is and how to use it. This document does not need to be discussed here. It mainly describes the loop usage of gallery.

The reuse of gallery is actually very simple. I can search a lot on the Internet, but I still want to write it. One is to record your learning achievements, and the other is to share with you.

The implementation of the gallery loop mainly involves inheriting the imageadapter of the baseadapter.

// Obtain the number of images public int getcount () {return integer. max_value ;}

Public View getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (context); // set the imageview resource for imageview. setimageresource (image [position % image. length]); // sets the display ratio type imageview. setscaletype (imageview. scaletype. fit_xy); // set the layout image to 120 * 80imageview. setlayoutparams (new gallery. layoutparams (60, 60); Return imageview ;}

Compared with previous articles, you will find that there are two changes: one is that the getcount () method returns the maximum integer value instead of the number of images;

The other part is imageview. setimageresource (image [position % image. Length]);

The gallery component is displayed to the last image, and the position parameter value is exactly getcount ()-1.

To achieve a loop, a large number must be returned, that is, the click can always go down, and the remainder of the position can achieve the loop 0, 1 of the image, 2 ............ image. length ()-, 1, 2 .............

Specific implementation:

Package xiaosi. loopgallery; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemselectedlistener; import android. widget. baseadapter; import android. widget. gallery; import android. widget. imageview; public class loopgalleryactivity extends activit Y {private gallery Gallery; private int [] image = new int [] {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 ,}; public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery = (Gallery) findviewbyid (R. id. gallery); Gallery. setadapter (New imageadapter (thi S); gallery. setspacing (1); gallery. setselection (4); gallery. setonitemselectedlistener (New onitemselectedlistener () {public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {} public void onnothingselected (adapterview <?> Arg0) {}});} class imageadapter extends baseadapter {private context; Public imageadapter (context c) {context = C ;}// obtain the number of images public int getcount () {return integer. max_value;} // obtain the position of the image in the library public object getitem (INT position) {return position;} // obtain the image idpublic long getitemid (INT position) {return position ;} public View getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (context); // set the imageview resource for imageview. setimageresource (image [position % image. length]); // sets the display ratio type imageview. setscaletype (imageview. scaletype. fit_xy); // set the layout image to 120 * 80imageview. setlayoutparams (new gallery. layoutparams (60, 60); Return imageview ;}}}

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="30dp"         android:background="?android:galleryItemBackground"/></LinearLayout>

Let's witness the departure of cainiao ......

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.