如何让 Viewpager 在计时器结束后转到下一页?
How can I make the Viewpager go to the next page after the timer is finished?
我目前正在制作一个锻炼应用程序,我制作了一个有 5 页的 Viewpager,有上一个和下一个按钮可以正常工作,还有一个在 activity 打开后开始的一分钟倒计时计时器。我期待在计时器结束后让 Viewpager 转到下一页,在打开页面时恢复计时器,无论页面是下一页还是上一页,都执行相同的操作。我是编码新手,我不知道该怎么做。如果有人告诉我要更改代码的内容或告诉我工作代码,那就太棒了。
activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morning);
findViewById(R.id.imageBackMorning).setOnClickListener(v -> onBackPressed());
countdownText = findViewById(R.id.countdownText);
countdownButton = findViewById(R.id.countdownButton);
countdownButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStop();
}
});
startTimer();
btnPrev = findViewById(R.id.prev);
btnNext = findViewById(R.id.next);
btnPrev.setVisibility(View.GONE);
setupViewPager();
pageChange();
}
private void startStop() {
if (timerRunning) {
stopTimer();
} else {
startTimer();
}
}
private void startTimer() {
countdownTimer = new CountDownTimer(timeLeft, 1000) {
@Override
public void onTick(long l) {
timeLeft = l;
update();
}
@Override
public void onFinish() {
}
}.start();
countdownButton.setText("Pause");
timerRunning = true;
}
private void stopTimer() {
countdownTimer.cancel();
countdownButton.setText("Resume");
timerRunning = false;
}
private void update() {
int minutes = (int) timeLeft / 60000;
int seconds = (int) timeLeft % 60000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if (seconds < 10) timeLeftText += "0";
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
private void pageChange() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
page = position;
switch (position) {
case 0:
btnPrev.setVisibility(View.GONE);
btnNext.setVisibility(View.VISIBLE);
break;
case 1:
case 2:
case 3:
btnPrev.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.VISIBLE);
break;
case 4:
btnPrev.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.GONE);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager() {
adapter = new Adapter(this);
viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
}
public void prev(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
}
public void next(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
}
private class Adapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
public Adapter(Context context) {
this.context = context;
}
// list img
int[] list_img = {
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic
};
// list judul
int[] list_judul = {
R.string.judul_1,
R.string.judul_2,
R.string.judul_3,
R.string.judul_4,
R.string.judul_5
};
// list deskripsi
int[] list_desk = {
R.string.desk_1,
R.string.desk_2,
R.string.desk_3,
R.string.desk_4,
R.string.desk_5
};
// list color bg
int[] list_bg = {
getResources().getColor(R.color.blue),
getResources().getColor(R.color.white),
getResources().getColor(R.color.blue),
getResources().getColor(R.color.white),
getResources().getColor(R.color.blue)
};
@Override
public int getCount() {
return list_judul.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_layout,container,false);
LinearLayout linearLayout = view.findViewById(R.id.item_layout);
ImageView imageView = view.findViewById(R.id.img);
TextView judul = view.findViewById(R.id.judul);
TextView desk = view.findViewById(R.id.deskripsi);
linearLayout.setBackgroundColor(list_bg[position]);
imageView.setImageResource(list_img[position]);
judul.setText(list_judul[position]);
desk.setText(list_desk[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
xml:
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/white"
android:backgroundTint="@color/remain_black"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageBackMorning"
android:layout_width="50dp"
android:layout_height="45dp"
android:paddingLeft="0sp"
android:paddingTop="10sp"
android:scaleX="0.9"
android:scaleY="0.9"
android:src="@drawable/white_arrow_back_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/_40sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"></androidx.viewpager.widget.ViewPager>
<TextView
android:id="@+id/countdownText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="01:00"
android:textColor="@color/red"
android:textSize="40sp"
android:layout_marginBottom="@dimen/_8sdp"
app:layout_constraintBottom_toTopOf="@+id/countdownButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/countdownButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleX="1.2"
android:scaleY="1.2"
android:backgroundTint="@color/red"
android:text="Start"
android:src="@drawable/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="@dimen/_50sdp"/>
<Button
android:id="@+id/prev"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="prev"
android:text="prev"
android:backgroundTint="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:onClick="next"
android:text="next"
android:backgroundTint="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
截图:
提前致谢。
making the Viewpager go to the next page after the timer is finished
当你的倒数计时器启动时,它会调用 onFinish()
,所以你可以在那里调用设置下一个索引。
private void startTimer() {
countdownTimer = new CountDownTimer(timeLeft, 1000) {
@Override
public void onTick(long l) {
timeLeft = l;
update();
}
@Override
public void onFinish() {
// do something in here, like
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
// think about what would happen if currently on the last page?
// go back to 0, stop? reverse maybe?
}
}.start();
countdownButton.setText("Pause");
timerRunning = true;
}
resume the timer when the page is opened
您的意思是每个页面都有一个单独的计时器吗?在这种情况下,您需要考虑如何单独保留每个页面的计时器值。 (想想页面更改时会发生什么,以及如何为每个页面保留资源和字符串。)然后当每个页面更改时,为所选页面提取正确的值并从那里恢复计时器。
我目前正在制作一个锻炼应用程序,我制作了一个有 5 页的 Viewpager,有上一个和下一个按钮可以正常工作,还有一个在 activity 打开后开始的一分钟倒计时计时器。我期待在计时器结束后让 Viewpager 转到下一页,在打开页面时恢复计时器,无论页面是下一页还是上一页,都执行相同的操作。我是编码新手,我不知道该怎么做。如果有人告诉我要更改代码的内容或告诉我工作代码,那就太棒了。
activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morning);
findViewById(R.id.imageBackMorning).setOnClickListener(v -> onBackPressed());
countdownText = findViewById(R.id.countdownText);
countdownButton = findViewById(R.id.countdownButton);
countdownButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStop();
}
});
startTimer();
btnPrev = findViewById(R.id.prev);
btnNext = findViewById(R.id.next);
btnPrev.setVisibility(View.GONE);
setupViewPager();
pageChange();
}
private void startStop() {
if (timerRunning) {
stopTimer();
} else {
startTimer();
}
}
private void startTimer() {
countdownTimer = new CountDownTimer(timeLeft, 1000) {
@Override
public void onTick(long l) {
timeLeft = l;
update();
}
@Override
public void onFinish() {
}
}.start();
countdownButton.setText("Pause");
timerRunning = true;
}
private void stopTimer() {
countdownTimer.cancel();
countdownButton.setText("Resume");
timerRunning = false;
}
private void update() {
int minutes = (int) timeLeft / 60000;
int seconds = (int) timeLeft % 60000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if (seconds < 10) timeLeftText += "0";
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
private void pageChange() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
page = position;
switch (position) {
case 0:
btnPrev.setVisibility(View.GONE);
btnNext.setVisibility(View.VISIBLE);
break;
case 1:
case 2:
case 3:
btnPrev.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.VISIBLE);
break;
case 4:
btnPrev.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.GONE);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager() {
adapter = new Adapter(this);
viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
}
public void prev(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
}
public void next(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
}
private class Adapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
public Adapter(Context context) {
this.context = context;
}
// list img
int[] list_img = {
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic,
R.drawable.email_pic
};
// list judul
int[] list_judul = {
R.string.judul_1,
R.string.judul_2,
R.string.judul_3,
R.string.judul_4,
R.string.judul_5
};
// list deskripsi
int[] list_desk = {
R.string.desk_1,
R.string.desk_2,
R.string.desk_3,
R.string.desk_4,
R.string.desk_5
};
// list color bg
int[] list_bg = {
getResources().getColor(R.color.blue),
getResources().getColor(R.color.white),
getResources().getColor(R.color.blue),
getResources().getColor(R.color.white),
getResources().getColor(R.color.blue)
};
@Override
public int getCount() {
return list_judul.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_layout,container,false);
LinearLayout linearLayout = view.findViewById(R.id.item_layout);
ImageView imageView = view.findViewById(R.id.img);
TextView judul = view.findViewById(R.id.judul);
TextView desk = view.findViewById(R.id.deskripsi);
linearLayout.setBackgroundColor(list_bg[position]);
imageView.setImageResource(list_img[position]);
judul.setText(list_judul[position]);
desk.setText(list_desk[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
xml:
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/white"
android:backgroundTint="@color/remain_black"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageBackMorning"
android:layout_width="50dp"
android:layout_height="45dp"
android:paddingLeft="0sp"
android:paddingTop="10sp"
android:scaleX="0.9"
android:scaleY="0.9"
android:src="@drawable/white_arrow_back_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/_40sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"></androidx.viewpager.widget.ViewPager>
<TextView
android:id="@+id/countdownText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="01:00"
android:textColor="@color/red"
android:textSize="40sp"
android:layout_marginBottom="@dimen/_8sdp"
app:layout_constraintBottom_toTopOf="@+id/countdownButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/countdownButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleX="1.2"
android:scaleY="1.2"
android:backgroundTint="@color/red"
android:text="Start"
android:src="@drawable/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="@dimen/_50sdp"/>
<Button
android:id="@+id/prev"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="prev"
android:text="prev"
android:backgroundTint="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:onClick="next"
android:text="next"
android:backgroundTint="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
截图:
提前致谢。
making the Viewpager go to the next page after the timer is finished
当你的倒数计时器启动时,它会调用 onFinish()
,所以你可以在那里调用设置下一个索引。
private void startTimer() {
countdownTimer = new CountDownTimer(timeLeft, 1000) {
@Override
public void onTick(long l) {
timeLeft = l;
update();
}
@Override
public void onFinish() {
// do something in here, like
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
// think about what would happen if currently on the last page?
// go back to 0, stop? reverse maybe?
}
}.start();
countdownButton.setText("Pause");
timerRunning = true;
}
resume the timer when the page is opened
您的意思是每个页面都有一个单独的计时器吗?在这种情况下,您需要考虑如何单独保留每个页面的计时器值。 (想想页面更改时会发生什么,以及如何为每个页面保留资源和字符串。)然后当每个页面更改时,为所选页面提取正确的值并从那里恢复计时器。