Android ViewFlipper 与 Intent

发布于 2024-12-09 23:44:36 字数 138 浏览 1 评论 0原文

是否可以在两个活动之间使用 ViewFlipper,并同时在它们之间发送参数?

就像,在第一个视图上,使用两个按钮,根据哪个按钮,它应该切换到总视图(使用 ViewFlipper),并根据按下的按钮使用一些不同的逻辑。

提前致谢。

Is it possible to use ViewFlipper between two activities, and at the same send parameters between them?

Like, on the first view, two buttons are used, depending on what button, it should be switched over to the over view (using ViewFlipper) and using some different logic depending on the button pressed.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-12-16 23:44:36

我只需创建两个类并让它们扩展所需的布局。在每个类中你可以有不同的逻辑。然后,您的 Activity 将仅保留 ViewFlipper 和切换到下一个/上一个的逻辑。

public class ViewFlipperActivity extends Activity {
   public void onCreate( Bundle savedInstanceState ) {
      super( savedInstanceState );
      setContentView( R.layout.my_viewFlipper_lauout );

      flipper = (ViewFlipper) findViewById( R.id.viewFlipper );

      FirstView first = new FirstView( this, flipper ); 
      flipper.addView( first );

      SecondView second = new SecondView( this, flipper );
      flipper.addView( second );

      first.setDataPasser( second );
      second.setDataPasser( first );
   }
}

第一个视图

public class FirstView extends LinearLayout implements DataPasser {
   private ViewFlipper flipper;
   private DataPasser dataPasser;

   public FirstView( Context context, ViewFlipper flipper ) {
      super(context);
      inflate( ... );
      this.flipper = flipper;

      // Do other stuff.
   }

   protected void setDataPasser( DataPasser dataPasser ) {
     this.dataPasser = dataPasser;
   }

   //Implement the other methods required by your interface.
}

第二个视图

public class SecondView extends LinearLayout {
   private ViewFlipper flipper;
   private DataPasser dataPasser;

   public SecondView( Context context, ViewFlipper flipper ) {
      super(context);
      inflate( ... );
      this.flipper = flipper;

      // Do other stuff.
   }

   protected void setDataPasser( DataPasser dataPasser ) {
     this.dataPasser = dataPasser;
   }

   //Implement the other methods required by your interface.
}

然后,当您需要在第一个视图或第二个视图上更改视图时,您可以调用 flipper.showNext()Flipper.showPrevious()

编辑

要在两个视图之间传递数据,您可以为此目的在 Activity 类中实现一些方法,然后让两个视图持有对该 Activity 的引用。另一种选择是创建两个视图必须实现的接口:

interface DataPasser {
  void setDataPasser( DataPasser dataPasser ); //To be able to add each view to the other.
  void passData( Data data ); //Or other methods needed to pass the data.
}

I would just create two classes and have them inflate the desired layout. In each class you could have the different logic. Your activity would then only hold the ViewFlipper and the logic for switching to next/previous.

public class ViewFlipperActivity extends Activity {
   public void onCreate( Bundle savedInstanceState ) {
      super( savedInstanceState );
      setContentView( R.layout.my_viewFlipper_lauout );

      flipper = (ViewFlipper) findViewById( R.id.viewFlipper );

      FirstView first = new FirstView( this, flipper ); 
      flipper.addView( first );

      SecondView second = new SecondView( this, flipper );
      flipper.addView( second );

      first.setDataPasser( second );
      second.setDataPasser( first );
   }
}

First View

public class FirstView extends LinearLayout implements DataPasser {
   private ViewFlipper flipper;
   private DataPasser dataPasser;

   public FirstView( Context context, ViewFlipper flipper ) {
      super(context);
      inflate( ... );
      this.flipper = flipper;

      // Do other stuff.
   }

   protected void setDataPasser( DataPasser dataPasser ) {
     this.dataPasser = dataPasser;
   }

   //Implement the other methods required by your interface.
}

Second View

public class SecondView extends LinearLayout {
   private ViewFlipper flipper;
   private DataPasser dataPasser;

   public SecondView( Context context, ViewFlipper flipper ) {
      super(context);
      inflate( ... );
      this.flipper = flipper;

      // Do other stuff.
   }

   protected void setDataPasser( DataPasser dataPasser ) {
     this.dataPasser = dataPasser;
   }

   //Implement the other methods required by your interface.
}

Then when you need to change view when on either FirstView or SecondView, you call flipper.showNext() or flipper.showPrevious()

EDIT

To pass data between the two views, you can either implement some methods in the activity class for this purpose and then let the two views hold a reference to the activity. Another alternative is to create an interface that the two views must implement:

interface DataPasser {
  void setDataPasser( DataPasser dataPasser ); //To be able to add each view to the other.
  void passData( Data data ); //Or other methods needed to pass the data.
}
池予 2024-12-16 23:44:36

名称应该告诉您。您可以在两个视图之间切换,而不是在两个意图之间切换。因此,最好尝试在两个视图之间切换,而不是在两个活动之间切换。

Name should tell you.You can flip between two views not between two intents.So better try to flip between two views rather than two activities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文