[英]Android: Multiple views, deep navigation, one Activity. What is the best way to handle?
I'm looking for the the best way to reproduce, in an Android app, the behavior of the iPhone UiNavigationController within an UITabBarController . 我正在寻找在Android应用程序中重现UITabBarController中 iPhone UiNavigationController行为的最佳方法。
I'm working on this Android app where I have a TabActivity and 4 tabs. 我正在使用这个Android应用程序,我有一个TabActivity和4个标签。 I've already gone through a lot of posts regarding the use of activities and tabs and how it's not a good idea to use activities for everything, which seems fair enough.
我已经经历了很多关于活动和标签使用的帖子,以及如何将活动用于所有事情并不是一个好主意,这看起来很公平。 I decided to use one Activity on each tab anyway, since it makes sense in my application.
我决定在每个选项卡上使用一个Activity,因为它在我的应用程序中是有意义的。
However, in one of those activities I have a deep navigation tree with more than one branch and up to 12 different views the user can go through. 但是,在其中一个活动中,我有一个深度导航树,其中包含多个分支以及最多12个不同的视图,用户可以通过这些视图。
The problem is: Android controls the navigation through activities inside an app, if you click the back button it will go to the previous one, but if I'm navigating through views, using one Activity, and I click back, it just finishes it. 问题是:Android控制应用程序内部的活动导航,如果单击后退按钮,它将转到上一个,但如果我浏览视图,使用一个活动,然后单击返回,它只是完成它。 So how can I have a smooth navigation behavior between views in an Activity?
那么如何在Activity中的视图之间获得平滑的导航行为?
I had implemented this using a TabActivity
with FragmentActivity
as each tab. 我使用带有
FragmentActivity
的TabActivity
作为每个选项卡实现了这一点。 Utilizing Fragments
API you can organize the code just like you would be using 12 different activities, still using only 1 for each tab in fact. 利用
Fragments
API,您可以像使用12种不同的活动一样组织代码,实际上每个选项卡仍然只使用1。 Fragment's framework will handle back key press for you to show previous fragment instead of closing the entire activity. Fragment的框架将处理后退按键,以显示先前的片段而不是关闭整个活动。
There are some problems with such approach, for example, there's no MapFragment
, but the workarounds can be found here on SOF. 这种方法存在一些问题,例如,没有
MapFragment
,但可以在SOF上找到解决方法。
You will need Android Support Package if your minimum SDK version is lower than 3.0. 如果您的最低SDK版本低于3.0,则需要Android支持包。
Well I know very little about UiNavigationViewController
, but I guess you want something to navigate between different Views. 我对
UiNavigationViewController
,但我想你想要在不同的视图之间导航。 As you are using TabActivity
, every tab should load into a separate Activity
. 在使用
TabActivity
,每个选项卡都应加载到单独的Activity
。
But since you want to branch it out, using that many Activities is not a perfect solution, neither the ActivityGroup
too. 但是既然你想把它分支出来,使用那么多活动并不是一个完美的解决方案,也不是
ActivityGroup
。 The better solution, as per my opinion(I have run into similar problem once) is to have the main or root tabs loads into separate Activity, but for their branches, use the ViewFlipper, which flips the Views. 根据我的观点(我遇到类似的问题),更好的解决方案是将主要或根选项卡加载到单独的Activity中,但对于它们的分支,使用ViewFlipper,它会翻转Views。 So the whole Layout(Subclass of View) can be flipped.
因此可以翻转整个布局(视图的子类)。
You may run into some problem while flipping more than two Views (as what people say, though I never had any problem). 翻转两个以上的视图时可能遇到一些问题(正如人们所说,虽然我从来没有遇到任何问题)。 So in that case you can use
layout.setVisibility(View.GONE)
to hide the layout and just change it with View.VISIBLE
for next view. 因此,在这种情况下,您可以使用
layout.setVisibility(View.GONE)
来隐藏布局,只需使用View.VISIBLE
将其View.VISIBLE
为下一个视图。
And about the concerns of back button, you need to store the last used View or Activity into a variable, and in the override of onBackPressed()
, just need to call them. 关于后退按钮的问题,您需要将最后使用的View或Activity存储到变量中,并且在
onBackPressed()
的覆盖中,只需要调用它们。
There might be better solution than this, not that I can remember, but yeah it's the easiest solution I can come up with. 可能有比这更好的解决方案,而不是我记得,但是,这是我能想到的最简单的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.