简体   繁体   English

简单的android应用程序,我应该使用什么结构?

[英]Simple android app, what structure should I use?

I have read about different layouts but still can't understand how to structure my simple app. 我已经阅读了不同的布局,但仍然不明白如何构建简单的应用程序。

All I want is a screen with an image and a set of buttons. 我想要的只是一个带有图像和一组按钮的屏幕。 When you press a button another screen slides in from the side. 当您按下按钮时,另一个屏幕从侧面滑入。 This second screen has an image and its own set of buttons. 第二个屏幕具有图像和自己的按钮组。

Sure for this I could use two separate Activities. 为此,我可以使用两个单独的活动。 BUT I want both screens to have access to some variables I've declared. 但是我希望两个屏幕都可以访问我声明的一些变量。 After reading about activities, it seems using a parent and child activity is not the right solution, since the parent activity can be dropped from memory so my variables would be lost. 在阅读了有关活动的内容之后,使用父子活动似乎不是正确的解决方案,因为可以从内存中删除父活动,因此我的变量将丢失。

So if I have one Activity and want two screens content to interact what is the best way, what structure should I use for this? 因此,如果我有一个“活动”并希望两个屏幕内容进行交互,什么是最好的方法,我应该使用哪种结构?

Thats what Fragments are for. 那就是片段的用途。 See the api guide or just google for a tutorial. 请参阅api指南或仅谷歌以获取教程。 You would be able to communicate between Fragments with callbacks, but you don't necessarily need to, since you could just call static classes on button click. 您将能够在带有回调的Fragments之间进行通信,但是您不必这样做,因为您可以在单击按钮时调用静态类。 That way you wouldn't need to communicate too much. 这样,您就不需要太多交流。 The communication is kind of hard to describe since we don't know what the buttons will do though. 这种沟通很难描述,因为我们不知道按钮会做什么。 But still: Consider using Fragments. 但仍然:考虑使用片段。 They are designed for doing exactly that and there are a ton of tutorials showing how to do this exact thing. 它们是专门为实现这一目的而设计的,并且有大量的教程展示了如何做到这一点。 The transitions will be way more smoth than between Activities. 过渡将比活动之间的过渡更平滑。

With Fragments you would have one Activity that hosts several Fragments that host Imageviews and Buttons. 使用片段,您将拥有一个活动,可以承载多个片段,这些片段承载Imageviews和Button。 You could implement swipe or onclick or whatever you want to use to switch, then do a Callback on that, returning data with it if you want to. 您可以实现滑动或onclick或任何您想用来进行切换的方法,然后对其进行回调,并根据需要返回数据。 Through that Callback the coresponding function in your Activity is called, where you could create the next Fragment, add data to it and replace the old Fragment with it. 通过该回调,将调用Activity中的coresponding函数,您可以在其中创建下一个Fragment,向其中添加数据,并用它替换旧的Fragment。
It would be a bit too far to explain that in deepth because how Fragments work would be a different question. 深入地解释这一点可能有点太过分了,因为Fragments如何工作将是一个不同的问题。 But thats essentially what you would do with them. 但这实际上就是您要对它们进行的操作。

If you decide to use Activities, you should go with the putExtra to intent way. 如果决定使用“活动”,则应将putExtra设置为意向方式。

You can pass data between activities using an Intent. 您可以使用Intent在活动之间传递数据。 Add your desired data to the Intent and start the new activity. 将所需的数据添加到Intent并开始新的活动。

Intent i = new Intent(this, SecondActivity.class); i.putExtra("one", "someValue"); i.putExtra("two", "anotherValue"); startActivity(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM