How does Android implement the status of the wireless loop slide of the focus graph?

Source: Internet
Author: User

Reference URL: http://my.oschina.net/xsk/blog/119167


The original implementation principle: Here is a simulation way to achieve the "wireless loop", in fact, is not the real sense of the wireless loop. Instead, the set value is large and is used as a wireless loop.


Code:


Package Stu.hades;import Android.content.context;import Android.support.v4.view.viewpager;import Android.util.attributeset;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.relativelayout;import Com.example.hadesviewpagerdemo.r;public class MyViewPager extends ViewPager { protected Context mcontext = null;    protected Viewpager Viewpager = null; 1, define the image resource array, there are several write several, you can also dynamically fill public static int[] Imageresid = new int[] {R.drawable.aaa,r.drawable.aab, R.DRAWABLE.AAC,    R.drawable.aad};        2. Initialize some parameters public Myviewpager (context context, AttributeSet Attrs) {Super (context, attrs);        This.mcontext = context; Viewpager = this;        }//3, configure the adapter after the view is initialized and listen @Override protected void Onfinishinflate () {super.onfinishinflate ();        Viewpager.setadapter (New Myadapter (Mcontext));        Set the revelation location so big so that you can loop 100,000 times left, and no one should slide so often!        Viewpager.setcurrentitem (100000 * imageresid.length); Viewpager.setonpagechangelistener (New MyPagechangelistener ()); }//3, Picture Change listener private class Mypagechangelistener implements Onpagechangelistener {protected int oldposit        ion = 0; Below is a circle showing the current picture position, here is a reference, you can write yourself, even if the comment does not affect the sliding public void onpageselected (int position) {//LinearLayout ima Ge_viewpager_circle = (linearlayout) ((relativelayout) viewpager.getparent ()). Findviewbyid (R.id.main_image_ viewpager_circle);//((ImageView) Image_viewpager_circle.getchildat (oldposition)). Setimageresource (R.DRAWABLE.D Ot_normal);//((ImageView) Image_viewpager_circle.getchildat (position% imageresid.length)). Setimageresource (R.D            rawable.dot_focused);        oldposition = position% Imageresid.length;  } public void onpagescrollstatechanged (int arg0) {} public void onpagescrolled (int arg0, float arg1, int arg2) {}} @Override protected void onmeasure (int arg0, int arg1) {super.onmeasure (arg0, arg    1); }}


2 Adapter

Package Stu.hades;import Android.content.context;import Android.os.parcelable;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.view.View;import Android.view.viewgroup.layoutparams;import Android.widget.imageview;public class Myadapter extends PagerAdapter { Public Context mcontext;public Myadapter (context mcontext) {this.mcontext = Mcontext;} To adjust the maximum number of elements set, you can infinitely right swipe, left slide cannot exceed 100,000 times @overridepublic int GetCount () {return integer.max_value;} @Overridepublic int GetItemPosition (Object object) {//TODO auto-generated method Stubreturn super.getitemposition ( object);} This everybody understands, the usage can Baidu or Oschina, I vernacular: each time initializes the current view and the left and right side view @overridepublic Object instantiateitem (view arg0, int position) {/ /TODO auto-generated method stubint posi = position% myviewpager.imageresid.length;//Each time a new view is added, it is a bit resource-intensive, but there is no conflict. And in the process of view switching, useless views are destroyed. ImageView Ximageview = new ImageView (mcontext); Ximageview.setbackgroundresource (Myviewpager.imageresid[posi]); Android.view.ViewGroup.LayoutParams para = new Android.view.ViewGroup.LayoutParams (layoutparams.wrap_content, layoutparams.wrap_content); Ximageview.setadjustviewbounds (True); Ximageview.setlayoutparams (para);((viewpager) arg0). AddView (Ximageview, 0); return Ximageview;} @Overridepublic void Destroyitem (view arg0, int position, Object arg2) {//Destroy unwanted views ((Viewpager) arg0). Removeview (view) a RG2);} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Restorestate (parcelable arg0, ClassLoader arg1) {} @Overridepublic parcelable saveState () {return null;} @Overridepublic void Startupdate (view arg0) {} @Overridepublic void Finishupdate (view arg0) {}}

Code URL: http://pan.baidu.com/s/1pJMDVMb



How does Android implement the status of the wireless loop slide of the focus graph?

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.