如何将 ViewFlipper 中的计时器重置为 0
How can I reset the timer in a ViewFlipper back to 0
我正在尝试开发的应用程序有一个 ViewFlipper 和一些与视图相对应的按钮,您单击第三个按钮,ViewFlipper 将视图更改为第三张图像,问题是一旦我单击该按钮计时器不会重置为 0,例如,如果我在第二张图片上停留 4 秒,然后如果我切换到第三张图片,1 秒后 ViewFlipper 将更改为第四张图片,我希望计时器重置回来到 0 这样它就会停留在我更改为 5 秒的视图上。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
好的,我找到了答案,我需要做的是使用处理程序,最初的问题是处理程序会覆盖其他以前的处理程序,当我更改为一个视图然后更改为另一个视图时,两个处理程序仍然会等待,所以我只需要添加这一行 handler.removeCallbacks(runnable);
这是我当前的完整代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
handler.removeCallbacks(runnable);
} catch(Exception e){}
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
viewFlipper.showNext();
viewFlipper.startFlipping();
}
}; handler.postDelayed(runnable, flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
将此添加到您的 image2 点击侦听器中:
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
viewFlipper.setFlipInterval(flippertime);
viewFlipper.startFlipping();
我正在尝试开发的应用程序有一个 ViewFlipper 和一些与视图相对应的按钮,您单击第三个按钮,ViewFlipper 将视图更改为第三张图像,问题是一旦我单击该按钮计时器不会重置为 0,例如,如果我在第二张图片上停留 4 秒,然后如果我切换到第三张图片,1 秒后 ViewFlipper 将更改为第四张图片,我希望计时器重置回来到 0 这样它就会停留在我更改为 5 秒的视图上。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
好的,我找到了答案,我需要做的是使用处理程序,最初的问题是处理程序会覆盖其他以前的处理程序,当我更改为一个视图然后更改为另一个视图时,两个处理程序仍然会等待,所以我只需要添加这一行 handler.removeCallbacks(runnable);
这是我当前的完整代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
handler.removeCallbacks(runnable);
} catch(Exception e){}
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
viewFlipper.showNext();
viewFlipper.startFlipping();
}
}; handler.postDelayed(runnable, flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
将此添加到您的 image2 点击侦听器中:
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
viewFlipper.setFlipInterval(flippertime);
viewFlipper.startFlipping();