从头开始查看寻呼机图像(片段)滑块

时间:2017-12-26 07:26:00

标签: android android-fragments android-viewpager

我在ViewPager中实现了滑块,单击播放按钮时工作正常,但现在我正在尝试从最后一个项目按钮实现滑块"播放表单再次启动",但我卡住了我该如何实现这个我已经实现了滑块来播放当前项目,但是我不能在最后一项中再次播放我正在使用Executors这是我的滑动代码

  /**
 * starts the slide show with 2 seconds frequency
 */
public void startTimer(final boolean isStartFromCurrentItem) {
    isTimerPlaying = true;
    btn_pause_play.setImageResource(R.drawable.ic_action_pause);
    scheduler =
            Executors.newSingleThreadScheduledExecutor();

    scheduler.scheduleAtFixedRate
            (new Runnable() {
                public void run() {
                    // call service


                    Message msg = new Message();

                    Log.i("change", "change");

                    hanleMessage(msg, isStartFromCurrentItem);


                }
            }, 0, 2, TimeUnit.SECONDS);


}
/**
 * UI Handler of background thread, this handler called after every two seconds to show next item as slide view
 *
 * @param message
 * @param isStartFromCurrentItem
 */
private void hanleMessage(Message message, final boolean isStartFromCurrentItem) {
    Handler msg_handler = new Handler(getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (viewpager != null) {
                if (viewpager.getCurrentItem() == cursor.getCount() - 1) {
                    //Toast.makeText(getApplicationContext(), getString(R.string.slide_show_comp), Toast.LENGTH_LONG).show();
                    stopSchedueler();
                    btn_pause_play.setImageResource(R.drawable.ic_action_play);

                } else {

                    if (isStartFromCurrentItem) {
                        int old_pos = viewpager.getCurrentItem();
                        viewpager.setCurrentItem(old_pos + 1, true);
                    } else {

                        Log.e("getCurrentItem", viewpager.getCurrentItem() + "");
                        viewpager.setCurrentItem(0, true);


                        //for starting slide show from beginning


                    }
                }
            }


        }
    };

    msg_handler.sendMessage(message);


}

如果我想播放当前项目的幻灯片,只需拨打startTimer(true),第二种方法处理工作正常,当我从上一个项目调用此方法时,按钮"再次播放"我打电话给tartTimer(false)如何在通过false之后通过更改此方法从头开始滑块,有人可以给我优化的解决方案谢谢。

1 个答案:

答案 0 :(得分:0)

你可以设置viewPager.setcurrentitem(0),以便在按下播放表单再次启动按钮时从头开始启动viewpager