Android Viewpager realize infinite loop effect _android

Source: Internet
Author: User
Tags addall int size stub

Recently the project has been useful to Viewpager to advertise operation bit display, see now many app advertisement operation bit is infinite loop, so have studied this function realization.

First look at the effect


From One Direction has been sliding, there is a slide to the end of the feeling, the specific how to achieve it? Look at the following ideas.

Realize the idea

Here is a picture to express the idea of an infinite loop, that is, insert the last data before the start of the data, insert the first data after the last data, and when sliding here, update the index position of the page OK.

Code implementation

This method is used for data processing, where medialist is the raw data, newmedialist is the processed data, and mmedialist is the data for the page display.

public void Inititemlist (list<mediaintro> medialist) {
      list<mediaintro> newmedialist = new ArrayList <MediaIntro> ();
      Newmedialist.addall (medialist);
      if (Newmedialist.size () > 1) {
        //No. 0 bit last, pull to the left, you can achieve direct sliding to the last one, the last is the No. 0 one, you can move to the right when you jump directly to the No. 0
        Newmedialist.add (0,medialist.get (Medialist.size ()-1));
        Newmedialist.add (medialist.get (0));
      }
      Mmedialist = newmedialist;
    }

Complete Adapter Code:

public class Adimagepageradapter extends recyclingpageradapter{private layoutinflater minflater;

    Private list<mediaintro> mmedialist = new arraylist<mediaintro> (); Private Boolean Isinfiniteloop;

    whether infinite loop int size;
      Public Adimagepageradapter (Layoutinflater inflater,list<mediaintro> medialist) {minflater = Inflater;
      Isinfiniteloop = false;
      Inititemlist (medialist);
    Size = Mmedialist.size (); } public void Inititemlist (list<mediaintro> medialist) {list<mediaintro> newmedialist = new ArrayLi
      St<mediaintro> ();
      Newmedialist.addall (medialist); if (Newmedialist.size () > 1) {//No. 0 bit last, pull to the left, you can achieve direct sliding to the last one, the last is the No. 0 one, you can move to the right when you jump directly to the No. 0 newmedialist.add
        (0,medialist.get (Medialist.size ()-1));
      Newmedialist.add (medialist.get (0));
    } mmedialist = Newmedialist;
    Public Mediaintro getitem (int position) {return mmedialist.get (position);
  }  public int getPosition (int position) {return isinfiniteloop? position%size:position; @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO auto-generated m
      Ethod stub final Viewholder holder;
      Final Mediaintro media = getitem (position);
        if (Convertview = = null) {holder = new Viewholder ();
        Convertview = Minflater.inflate (R.layout.home_ad_item, Parent,false);
        Holder.mimageview = (ImageView) Convertview.findviewbyid (r.id.homeaditemimg);
        Holder.mtextview = (TextView) Convertview.findviewbyid (r.id.homeaditemtxt);
      Convertview.settag (holder);
      }else{holder= (Viewholder) Convertview.gettag (); } if (Media.source.equals (Magicsource.source_out_ad)) {Imageloader.displayimage (Adimageurl,holder.mimageview,
        Adimageoptions,null,null);
        Holder.mTextView.setText (""); Holder.mImageView.setOnClickListener (New Onclicklistener () {@OverriDe public void OnClick (View v) {//TODO auto-generated Method Stub//Ad Click event}
      });
        }else {imageloader.displayimage (Media.imgurl, holder.mimageview,adimageoptions,null,null);
        Holder.mTextView.setText (media.desc+ "");
            Holder.mImageView.setOnClickListener (New Onclicklistener () {@Override public void OnClick (View v) {
          TODO auto-generated Method Stub startmediadetail (media);
      }
        });
    return convertview; @Override public int GetCount () {//TODO auto-generated method stub return Isinfiniteloop?
    Integer.MAX_VALUE:mMediaList.size ();
      Private class viewholder{ImageView mimageview = null;
    TextView Mtextview = null;
    public Boolean Isinfiniteloop () {return isinfiniteloop; } Public Adimagepageradapter Setinfiniteloop (Boolean isinfiniteloop) {This.isinfiniteloop=isinfiniteloop;
    return this;
 }
  }

To update the page index code:

@Override public
    void onpageselected (int position) {
      //TODO auto-generated method stub

      if ( Adimageadapter.getcount () >1) { 
        if (position<1) {
          position=adimageadapter.getcount ()-2;
          Adviewpager.setcurrentitem (Adimageadapter.getcount () -2,false);
        else if (Position > Adimageadapter.getcount ()-2) {
          position = 1;
          Adviewpager.setcurrentitem (1, false);
        Setadpagepointselected (position-1);
      }
    

Complete Slide Listener Code

public class Onadpagechangelistener implements onpagechangelistener{

    @Override public
    Void onpagescrollstatechanged (int arg0) {
      //TODO auto-generated method stub

    }

    @Override public
    Void onpagescrolled (int arg0, float arg1, int arg2) {
      //TODO auto-generated method stub

    }

    @Override public
    V OID onpageselected (int position) {
      //TODO auto-generated method Stub

      if (Adimageadapter.getcount () >1) { 
        if (position<1) {
          position=adimageadapter.getcount ()-2;
          Adviewpager.setcurrentitem (Adimageadapter.getcount () -2,false);
        else if (Position > Adimageadapter.getcount ()-2) {
          position = 1;
          Adviewpager.setcurrentitem (1, false);
        Setadpagepointselected (position-1);}}

  

This is the entire content of this article, I hope to learn more about Android software programming help.

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.