Android uses Viewpager loop ad bit to achieve _android

Source: Internet
Author: User

How to implement loop playback

Now online loop playback is done in the adapter GetCount () method returns a larger value and Instantiateitem (ViewGroup container, int position) through the fetch (position/ Datas.size ()) The way, let Viewpager continue to play down.

Here we implement this by modifying the data source and setting the CurrentItem.

To modify a data source:

Final list<integer> datas = new arraylist<> ();
Add test data here, the actual project can be from the network to obtain data
    Datas.add (r.mipmap.welcome_page_01);
    Datas.add (r.mipmap.welcome_page_02);
    Datas.add (r.mipmap.welcome_page_03);
    Processing data source
    Datas.add (0,datas.get (Datas.size ()-1));
    Datas.add (Datas.get (1));

Setting CurrentItem needs to be done in the Onpagechangelistener object:

Mviewpager.setonpagechangelistener (New Viewpager.onpagechangelistener () {
      @Override public
      Void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {
      }
      @Override public
      Void onpageselected (int position) {
        if (position==0) {
          Mviewpager.setcurrentitem (datas.size () -2,false);
        } else if (position==datas.size ()-1) {
          mviewpager.setcurrentitem (1,false);
        }
      }
      @Override public
      void onpagescrollstatechanged (int state) {
      }
    });

Adapter is the same as the general Viewpager adapter.

How to implement an automatic loop:

Automatic loop implementation, more methods, such as Timer,handler,alarm and so on. Here we use the handle way

The custom handler class is as follows:

  public class Bannerhander extends handler{
    @Override public
    void Handlemessage (msg) {
      if (msg.what= =1) {
        Mviewpager.setcurrentitem (Mviewpager.getcurrentitem () +1);
        Sendemptymessagedelayed (1,3000);}}
  

To get the data, you can start the automatic loop after you set the adapter to Viewpager:

if (!) ( Mhandler!=null && mhandler.get ()!=null)) {
Mhandler = new Weakreference<bannerhander> (new Bannerhander ());
}
Mhandler.get (). sendemptymessagedelayed (1, 300);

Here we use a weak reference, mainly to prevent memory overflow.

Attention:

When you set up CurrentItem, you must do it in Onpagechangelistener, and the adapter finishupdate (view view) is set to display a white screen. If any great God knows, please teach.

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.