8

I have problem with android viewpager slideshow. I want to show the viewpager layout after a minimum time period. Here is my code sample. My main class:

public class MainActivity extends Activity {
    private ViewPager mViewPager;
    private SwipeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        // set the adapter
        adapter = new SwipeAdapter(MainActivity.this);
        mViewPager.setAdapter(adapter);

        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

}

My Adapter class:

public class SwipeAdapter extends PagerAdapter {

    private LayoutInflater mInflater;
    private static int[] mLayouts = { R.layout.view_layout1,
            R.layout.view_layout2, R.layout.view_layout3, R.layout.view_layout4 };

    SwipeAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewGroup pageView = (ViewGroup) mInflater.inflate(mLayouts[position],
                container, false);
        container.addView(pageView);
        getItemPosition(pageView);
        return pageView;
    }

    @Override
    public int getCount() {
        return mLayouts.length;
    }
    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }
}

suppose that I want to make the viewpager layout auto sliding after 10 seconds. How can it possible.

androidcodehunter
  • 21,567
  • 19
  • 47
  • 70

2 Answers2

30

Use Handler to make it auto slide. Here is some code

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
    public void run() {
            if( position >= 4){
                position = 0;
            }else{
                position = position+1;
            }
            toBarPager.setCurrentItem(position, true);
            handler.postDelayed(runnable, 10000);
    }
};

And don't forget to remove callback onPause()

@Override
public void onPause() {
    super.onPause();
    if (handler!= null) {
        handler.removeCallbacks(runnable);
    }
}

Also, don't forget to rerun onResume()

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first
    handler.postDelayed(runnable, 10000);
}
Hein
  • 2,675
  • 22
  • 32
  • Where to get the positionPagerMoved – androidcodehunter May 11 '13 at 07:45
  • It is not necessary for your case, I edited my code. Sorry for confusion. – Hein May 11 '13 at 07:49
  • And also position always started from zero and edit position>=3. Thanks. Your code works fine. – androidcodehunter May 11 '13 at 08:22
  • where we have to place this handle in main class? if so then in which method – ask4solutions Feb 16 '15 at 09:24
  • If user swipes current image with swipe gesture in view pager, how to handle the auto slide show position at that time – Dory Mar 26 '15 at 17:47
  • I have implemented the same code. When I switch to next activity and press back then the code crashes. Any idea what must be wrong. – Sagar Devanga Jan 20 '16 at 15:55
  • "Always call the superclass method first". Not Always. For Example, in onDestroy() method.You should call supper class after you've done your things.Look at this link: https://stackoverflow.com/questions/18821481/what-is-the-correct-order-of-calling-superclass-methods-in-onpause-onstop-and-o – Arash Jul 25 '17 at 16:01
  • its working.but when position becomes 3 and when loop continues,before showing the first item previous items are showing rapidly.anyone know the reson for this? – anju jo Jan 23 '19 at 05:51
1

use Handler and send[Empty]MessageDelayed/postDelayed and call setCurrentItem on your ViewPager

pskink
  • 23,874
  • 6
  • 66
  • 77