"Android" gallery-style picture browser, using Horizontalscrollview instead of Gallery,onclicklistener parameter pass

Source: Internet
Author: User

Originally, the gallery-style picture browser, using Android Gallery can be easily done, but Google said gallery every time you switch pictures to create a new view, causing too much waste of resources, has been completely discarded after Android API 19, Google recommends using Viewpager or Horizontalscrollview to achieve the gallery effect. The information on the Internet is too miscellaneous, some of the information is too advanced, completely do not understand. I've been doing this for an afternoon before using Horizontalscrollview to complete the gallery-style picture browser, such as:


1, the first is for res\values\strings.xml modification, just modify the name of the app, nothing special. Supplementary imageview_description in the "Android" Image resource access and web format picture browser (click the open link) said that only to solve the eclipse of ImageView in the lack of Android: ContentDescription's warning. After that, copy several images to any drawable-xxx folder in the project directory, and name them in lowercase letters, numbers, and underscores. Here are four images copied to drawable-hdpi, together with the app icon, a total of five photos.

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "app_name" > Gallery-style picture browser </ string>    <string name= "action_settings" >Settings</string>    <string name= "Imageview_ Description "> Big picture </string></resources>
2, after modifying mainactivity layout file, Res\layout\activity_main.xml, layout ideas are as follows:


In addition to the part of the green callout is generated in Java code, the rest are generated in Activity_main.xml, placing a picture view ImageView and the Landscape scrolling view Horizontalscrollview.

Horizontalscrollview android:scrollbars= "Horizontal" represents the scroll bar when scrolling horizontally, and if it is not displayed, set its value to none.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:layout_gravity= "center_horizontal" android:orientation= "vertical" > <imageview android:id= "@+id/imageview1" android:layout_width= "Match_parent" android:layout    _height= "200DP" android:paddingtop= "10DP" android:contentdescription= "@string/imageview_description"/> You can see that Horizontalscrollview is so used, it just has and only a android:orientation= "horizontal", the level of linearlayout, you have to put something into the horizontal scroll bar, All are put into this linearlayout inside. So, even if we are going to manipulate Horizontalscrollview with Java code, we will assign the ID to the horizontal linearlayout inside it.

3, followed by Mainactivity.java, the main purpose of this code is to create a vertical top-down linear layout view in the above mentioned LINEARLAYOUT1, which puts a imageview with a textview. Because it is a vertical top-down linear layout, it is not possible to set the vertical position of the components inside, that is, unable to set the picture view placed in the center, the label file is placed at the bottom. So set all the pictures regardless of their original size, are set to 80DP height, left and right to leave 10 spacing, and then placed a horizontal center font. A vertical linear layout cannot set the vertical position of its components, but the horizontal position is still possible. Conversely, horizontal linear layout is unable to set its horizontal position, only its vertical position can be set. Generate layouts and components with Java code refer to "Android" with Java Code Layout, button add Click event (click to open link).

Package Com.galleryviewer;import Android.os.bundle;import Android.view.gravity;import android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;import Android.widget.TextView; Import Android.app.activity;public class Mainactivity extends Activity {private LinearLayout linearlayout1;private ImageView imageview1;private int[] imageId = new int[] {r.drawable.img1, R.DRAWABLE.IMG2,R.DRAWABLE.IMG3, R.DRAWABLE.IMG4, R.drawable.ic_launcher};//put the picture you want to display in an image ID array for easy operation @overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Gets the component linearLayout1 = (linearlayout) Findviewbyid (R.ID.LINEARLAYOUT1); imageView1 = (ImageView) Findviewbyid ( R.ID.IMAGEVIEW1); for (int i = 0; i < imageid.length; i++) {//Create a new linear layout linearlayout linearlayout = new LinearLayout (this );//Set the parameters for the newly generated linear layout with a width of 100, the height is matched to the parent component, which is the height of the horizontal scrolling view layoutparams layoutparams = new Layoutparams (100,viewgroup.layoutparams.match_parent); Layoutparams.gravity = gravity.center_horizontal;//Sets the horizontal center of the component within the linear layout linearlayout.setorientation ( linearlayout.vertical);//Set the newly generated linear layout android:orientation= "VERTICAL" Linearlayout.setlayoutparams (layoutparams);// Apply to the newly generated linear layout//Create a new picture view imageView ImageView = new ImageView (this), Imageview.setid (i + 20000);//here because the ID cannot be the character's sake, All image IDs are set to 20000,20001,20002,... Easy to click on the image below the listener controls Imageview.setimageresource (Imageid[i]);//The image of the image in the array is placed in the picture view Imageview.setadjustviewbounds (TRUE);// Auto Scale to Imageview.setmaxheight (80);//The height of the picture is 80dpimageview.setpadding (Ten, Ten, ten); Imageview.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (view view) {Imageview1.setimageresource (Imageid[view.getid ()- 20000]);//after the clicked Image ID is removed, minus 20000 is the position of the image to be displayed in the image array. });//load the picture view into the newly generated linear layout linearlayout.addview (ImageView);//New to a label text TextView TextView = new TextView (this); Textview.settext ("picture" + i); textview.settextsizE (18);//Label text centered in horizontal position textview.setgravity (gravity.center);//Added to the newly generated linear layout after Linearlayout.addview (TextView); Linearlayout1.addview (LinearLayout);}}}
Here also shows, how to click on the event listener Onclicklistener parameter pass, Onclicklistener is an interface, its implementation method is only one, this implementation method has a parameter view, this view is equivalent to the page JavaScript programming , add a click event to a node after the onclick= "xxx (This)" in the "this", the whole thing is clicked on to the Onclicklistener Listener, through View.getid () can be removed by the caller ID. It is not advisable to pass a global variable to an anonymous inner class, which is wrong and invalid.

"Android" gallery-style picture browser, using Horizontalscrollview instead of Gallery,onclicklistener parameter pass

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.