[英]Android CountDownTimer on Fragment with ViewPager
I'm new on android development. 我是android开发的新手。
I've develop a ViewPager to show a different image when user click on next button. 我开发了一个ViewPager,以在用户单击下一步按钮时显示不同的图像。
But I want to start a CountDownTimer when the new image is displayed. 但是我想在显示新图像时启动CountDownTimer。
How can I do this? 我怎样才能做到这一点?
My code is for the moment : 我的代码目前是:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int imgNumber = mPageNumber + 1;
String strImg = "ss0" + imgNumber;
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_ssstretch4_split, container, false);
// Set the title view to show the page number.
((TextView) rootView.findViewById(R.id.text1)).setText(
getString(R.string.title_activity_ssstretch4_split, mPageNumber + 1));
//Mettre l'image
int idImg = getResources().getIdentifier("com.trainwithcoachminiil.app:drawable/" + strImg, null, null);
((ImageView) rootView.findViewById(R.id.imgSS)).setImageResource(idImg);
//Lancer le timer
textActivity = (TextView) rootView.findViewById(R.id.textActivity);
textCountDown = (TextView) rootView.findViewById(R.id.textCountDown);
countDownTimerPrepare = new CountDownTimerActivityPrepare(startTimePrepare, interval);
textActivity.setText(R.string.activityPrepare);
textCountDown.setText(String.valueOf(startTimePrepare/1000));
countDownTimerPrepare.start();
return rootView;
}
But when I go to the next page my countdowntimer still running so I get strange behavior. 但是,当我转到下一页时,我的倒数计时器仍在运行,因此出现奇怪的现象。
Thanks a lot if you could tell me how to do. 非常感谢您能告诉我该怎么做。
在Fragment
onDestroy
上调用yourCountDownTimer.cancel()
Viewpager loads atleast two
pages/Fragment
at a time.Viewpager一次至少加载两个
pages/Fragment
。 And it will start your second Fragment/page count timer .它将启动您的第二个Fragment / page count计时器 。
So you should start timer when your fragment is visible
. 因此,当您的
fragment is visible
时,您应该启动计时器。 First handle fragment Visibility using setUserVisible
or setMenuVisibility
delegate's of Fragment. 首先使用
setUserVisible
或setMenuVisibility
委托处理片段可见性。 then start your countDown Timer. 然后启动您的countDown计时器。 and cancel timer in on
DestroyView
并在
DestroyView
上取消计时器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.