"Turn" Pro Android Learning Note (21): User interface and Control (9): Gallery and Simpleadapter

Source: Internet
Author: User

Gallery Gallery-style controls, but at API level 16, also known as Android 4.1, are deprecated and can use Horizontablescroolview and Viewpager. But the latter are not adapterview, all of us still quickly skip gallery, also incidentally learn simpleadapter.

The XML layout file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<gallery xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/ui_gallery"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
/>

For child view, there is a pre-built Android. R.layout.simple_gallery_item, but that is TextView, does not meet our requirements, we need is imageview, so we set the sub-view layout, Ui_imageitem.xml file as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<imageview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/ui_imageitem"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:ellipsize= "marquee"/> <!--when the character content is too long to display, the ellipsis can be substituted for the non-displayed character; the ellipsis can be displayed in the beginning of the display area, in the middle, at the end of the position, or in the way of a marquee display text,  Show is the marquee way. -

Simpleadapter are often used for static resources. Suitable for use with gallery. The following Java code:

public class Uigallerytest extends activity{
/* int[] pics corresponding to our res/drawable/under the image resources, picsname corresponding to their key, we specifically set the second resource key to a different. In the final result heavy, we can see that the second picture is not shown in gallery */
Private int[] Pics = {R.drawable.sunflower01, r.drawable.sky01,r.drawable.sky02};
Private string[] Picsname = {"Image", "Image1", "image"};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.ui_gallery);
@SuppressWarnings ("deprecation")
Gallery Gallery = (Gallery) Findviewbyid (r.id.ui_gallery);
The format of the//simpleadapter data source is List<?extends map<string? >> This format is very special, it is a list of MAP, each entry is a data unit.
arraylistImages = new arraylistfor (int i = 0; i < pics.length; i + +) {
hashmap<string,object> one = new hashmap<string, object> ();
One.put (Picsname[i], integer.valueof (Pics[i]));
Images.add (one);
}
Simpleadapter is similar to other adapter, the parameters are easy to understand.
Simpleadapter adapter = new Simpleadapter (this/*context*/, images/* Data source */,
R.layout.ui_imageitem,/* Layout of child view, with pre-built Android. R.layout.simple_gallery_item, but that is TextView, does not meet our requirements * *
New string[]{"Image"},//Select data from map, this example is which key, we choose "image", so the second figure is not shown
New Int[]{r.id.ui_imageitem}); Control ID in Child view
Gallery.setadapter (adapter);
}
}

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (21): User interface and Control (9): Gallery and Simpleadapter

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.