简体   繁体   中英

how to stop viewpager auto slide in android

I have made an android app in that i have implemented play pause function,But I only knows about play ,I don't know how to stop sliding on the same button click,I have used two functions for it as below,play is working but pause is not.! onClick

    if (!flag) {
        play();
        new Thread(ViewPagerVisibleScroll).start();
        ivPlay.setBackgroundResource(R.drawable.pause);
        flag = true;

    } else {
        pause();
        new Thread(ViewPagerVisibleScroll).start();
        ivPlay.setBackgroundResource(R.drawable.play_btn);
        flag = false;
    }

play and pause

void play() {

        pos = viewPager.getCurrentItem();
        ViewPagerVisibleScroll = new Runnable() {
            @Override
            public void run() {
                try {
                    if (pos <= adapter.getCount() - 1) {
                        viewPager.setCurrentItem(pos, true);

                        viewPager.setScrollDurationFactor(6);
                        handler.postDelayed(this, 3000);
                        pos++;
                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        };
    }

    void pause() {

        pos = viewPager.getCurrentItem();
        ViewPagerVisibleScroll = new Runnable() {
            @Override
            public void run() {
                try {
                    if (pos <= adapter.getCount() - 1) {
                        viewPager.setCurrentItem(pos, true);

                        viewPager.setScrollDurationFactor(6);
                        handler.postDelayed(this, 300000);
                        pos++;
                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        };
    }

in pause button click :

handler.removeCallbacksAndMessages(ViewPagerVisibleScroll);

Try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM