Two days ago the teacher came to me to help her write an Android demo. Say is to achieve a picture left and right sliding effect, and click on the picture will make a sound.
For a long time did not write Android, so took a while to review and study. The process and implementation are now briefly documented below. (The project refers to a part of the Csdn Bo Master Android General)
Implementation is using the Viewpager
The code is as follows: In the TAP listening section, add touch snooping by getting the controls in list<>. Of course you can add multiple controls on each page, then in the control that gets the interface, and then each add listener.
And where the code is optimized ... But just for demonstration, there is not much time to do ... Forgive me for not being responsible ...
Package Com.example.viewpagerdemo;import Java.util.arraylist;import Android.media.mediaplayer;import Android.os.bundle;import Android.os.parcelable;import Android.app.activity;import Android.util.Log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.widget.imageview;import Android.support.v4.view.PagerAdapter; Import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;public Class Twoactivity extends Activity implements Onpagechangelistener {private Viewpager viewpager; Private arraylist<view> list; Private ImageView ImageView; Private imageview[] imageviews; Private MediaPlayer MediaPlayer = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Layoutinflater inflater = Getlayoutinflater (); List = new ARRAylist<view> (); List.add (inflater.inflate (R.LAYOUT.ITEM01, null)); List.add (Inflater.inflate ( R.LAYOUT.ITEM02, NULL)), List.add (inflater.inflate (r.layout.item03, null)), List.add (Inflater.inflate ( R.LAYOUT.ITEM04, null)); List.add (inflater.inflate (r.layout.item05, null)); Imageviews = new Imageview[list.size ()]; ViewGroup Group = (viewgroup) Findviewbyid (R.id.viewgroup); Viewpager = (Viewpager) Findviewbyid (R.id.viewpager); for (int i=0; I<list.size (), i++) {ImageView = new ImageView (this); Imageview.setlayoutparams (New Layoutparams (12,12)); Imageviews[i] = ImageView; if (i = = 0) {imageview.setbackgroundresource (r.drawable.page_indicator_focused); }else{Imageview.setbackgroundresource (r.drawable.page_indicator_unfocused); } group.addview (ImageView); } viewpager.setadapter (New Myadapter ()); Viewpager.setonpagechangelistener (this); Viewpager.setcurrentitem (0); } @Override public void onpagescrollstatechanged (int. arg0) {} @Override public void onpagescrolled (int arg 0, float arg1, int arg2) {} @Override public void onpageselected (int arg0) {log.e ("Log", "You are currently selected" + A Rg0%list.size ()); Switch to the next page when the music played on the previous page is stopped and set to NULL, the IF (mediaplayer!=null) {if (mediaplayer.isplaying ()) is reinitialized {mediaplayer.stop (); MediaPlayer = null; }} setimagebackground (Arg0%list.size ()); } private void Setimagebackground (int selectitems) {for (int i=0; i<imageviews.length; i++) {if (i = = Selectitems) {imageviews[i].setbackgroundresource (r.drawable.page_indicator_focused); }else{Imageviews[i].setbackgroundresource (r.drawable.page_indicator_unfocused); }}} public class Myadapter extends pageradapter{@Override public int GetCount () {return integer.max_value; } @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1; } @Override public int getitemposition (Object object) {return Super.getitemposition (object); } @Override Public Object instantiateitem (ViewGroup container, final int position) {//per page Add tap Listen, initialize music and play when clicked, and make sure you can replay switch (position) {case 0:list on each click) . Get (0). Setonclicklistener (New View.onclicklistener () {@Override public voi D OnClick (view view) {LOG.E ("Log", "Your current selection is dog"); if (mediaplayer!=null) {if (mediaplayer.isplaying ()) {mediaplayer.stop (); MediaPlayer = null; }} initmediaplayer (0); Mediaplayer.start (); } }); Break Case 1:list.get (1). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {LOG.E ("Log", "You are currently selecting Cat"); if (mediaplayer!=null) {if (Mediaplayer.isplayi Ng ()) {mediaplayer.stop (); MediaPlayer = null; }} initmediaplayer (1); Mediaplayer.start (); } }); Break Case 2:list.get (2). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {LOG.E ("Log", "You are currently selecting Cat"); if (mediaplayer!=null) {if (Mediaplayer.isplayi Ng ()) {mediaplayer.stop (); MediaPlayer = null; }} initmediaplayer (2); Mediaplayer.start (); } }); Break Case 3:list.get (3). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { LOG.E ("Log", "You are currently choosing cat"); if (mediaplayer!=null) {if (mediaplayer.isplaying ()) {mediaplayer.stop (); MediaPlayer = null; }} initmediaplayer (3); Mediaplayer.start (); } }); Break Case 4:list.get (4). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {LOG.E ("Log", "You are currently selecting Cat"); if (mediaplayer!=null) {if (Mediaplayer.isplayi Ng ()) { Mediaplayer.stop (); MediaPlayer = null; }} initmediaplayer (4); Mediaplayer.start (); } }); Break Default:break; } ((Viewpager) container). AddView (List.get (Position%list.size ()), 0); Return List.get (Position%list.size ()); } @Override public void Restorestate (parcelable arg0, ClassLoader arg1) {} @Override P Ublic parcelable saveState () {return null; } @Override public void Startupdate (View arg0) {} @Override public void Finishupdate (V Iew arg0) {} @Override public void Destroyitem (View container, int position, object object) { ((Viewpager) container). Removeview (List.get (position%lisT.size ())); }} private void Initmediaplayer (int i) {//Initialize music for page try {switch (i) { Case 0:mediaplayer = Mediaplayer.create (this, r.raw.bgm1); Mediaplayer.prepare (); Break Case 1:mediaplayer = Mediaplayer.create (this, r.raw.bgm2); Mediaplayer.prepare (); Break Case 2:mediaplayer = Mediaplayer.create (this, r.raw.bgm3); Mediaplayer.prepare (); Break Case 3:mediaplayer = Mediaplayer.create (this, R.RAW.BGM4); Mediaplayer.prepare (); Break Case 4:mediaplayer = Mediaplayer.create (this, R.RAW.BGM5); Mediaplayer.prepare (); Break Default:break; }} catch (Exception e) {e.printstacktrace (); } }}
Layout file:
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <android.support.v4.view.viewpager Android:id= "@+id/viewpager" android:layout_width= "fill_parent" android:layout_height= "wrap_content "/> <relativelayout android:layout_width=" fill_parent "android:layout_height=" Wrap_co Ntent "android:orientation=" vertical "> <linearlayout android:id=" @+id/viewgroup " Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layou T_alignparentbottom= "true" android:layout_marginbottom= "30DP" android:gravity= "Center_horizontal" android:orientation= "Horizontal" > </LinearLayout> </RelativeLayout> </fram Elayout>
Page layout file:
<?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:o rientation= "vertical" > <imageview android:id= "@+id/imageview1" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:scaletype=" Centercrop " android:src=" @drawable/ Dog "/></linearlayout>
In the page layout file above I just added a imageview and filled the entire screen, of course, if you have the need you can add text or other controls here, to add listening, just get and then add as above.
If you need to swipe automatically, you should add a timer or you can do this.
Other animation transitions, these extensions are all looking at how to implement.
However, the feeling of viewpager flexibility is not high, can be customized to achieve a similar with more interface and flexibility to choose the space out. Of course it depends on the demand.
OK, here's the record.
"Android" Viewpager realize the picture left and right slide play and add click event