片段设计:通过显示/在一个活动隐藏片段适应多种屏幕布局? [英] Fragment design: Adapting to multiple screen layouts by showing/hiding fragments within a single Activity?
问题描述
我想了解如何使用片段创建很好地适应多屏幕和布局应用程序。我研究了几个例子:
I am trying to understand how to use Fragments to create apps that adapt well to multiple screens and layouts. I have studied a few examples:
- 的片段在Android开发者指南文档。
- 谷歌IO应用
- 从的ActionBar福尔摩斯的碎片样本。
- The Fragments document on Android Developer guide.
- Google IO app
- Fragments sample from ActionBar Sherlock.
所有这些主张的倍数活动
办法:
All of these advocate a multiple Activity
approach:
- 在大屏幕上,显示一个
活动
有多个片段
取值 - 在一个更小的屏幕,拆了
片段
取值在多个活动
秒。
- On a large screen, display a single
Activity
with multipleFragment
s - On a smaller screen, split up the
Fragment
s among multipleActivity
s.
我想到了另一种方法 - 一个活动
之一:
I thought of another approach - a single Activity
one:
- 有一个
活动
所有的片段
S在它。 - 根据屏幕大小和方向,显示/隐藏相应的
片段
(S)(使用FragmentTransaction.show()
/FragmentTransaction.hide()
)。
- Have a single
Activity
with all theFragment
s in it. - Depending on the screen size and orientation, show/hide the appropriate
Fragment
(s) (usingFragmentTransaction.show()
/FragmentTransaction.hide()
) .
要说明具有相同的新闻文章列表/条的内容的例子,Android开发者指南使用:
To illustrate with the same "News article list/article contents" example that Android developer guide uses:
- 有一个包含
新闻
活动既是ArticleListFragment
和ArticleReaderFragment
。 - 在一个选项卡,这两个片段始终显示。
- 在一个电话,
ArticleReaderFragment
最初是隐藏的。当一篇文章被从列表中选择时,ArticleListFragment
隐藏和ArticleReaderFragment
显示。
- Have the
News
activity containing both anArticleListFragment
andArticleReaderFragment
. - On a tab, both fragments are always displayed.
- On a phone, the
ArticleReaderFragment
is initially hidden. When an article is selected from the list, theArticleListFragment
is hidden and theArticleReaderFragment
is shown.
有没有人用类似的方法?是否有任何实际的缺点这种方法可能有哪些?它是否比多活动的方式似乎更好/更差?例如,片段不能正常显示/隐藏在XML - 一个必须使用 FragmentTransaction
此
Has anybody used a similar approach? Are there any practical downsides this method might have? Does it seem better/worse compared to the multiple-activity way? For example, fragments cannot be shown/hidden in XML - one must use FragmentTransaction
for this.
设想一个应用程序,它可以同时显示在屏幕上最多三个窗格。另外,这些都是要考虑的因素:
Imagine an app which can display up to three "panes" at a time on the screen. Further, these are the factors to consider:
- 系统手机可以在同一时间(无论纵向/横向)只显示一个窗格
- 在一个7英寸的平板电脑可以显示2个窗格,在垂直分割的肖像,水平横向模式分裂。
- 系统10+英寸平板电脑可以显示2个窗格,在垂直纵向分割; 3面板横向水平分割。
为了简单起见,让我们保持电视屏幕了讨论。
For simplicity, lets keep TV screens out of the discussion.
现在,把这种以设计:
- 我们有三个片段:Frag1,Frag2和Frag3
- 在简单的情况下,这三个片段是在一个活动(可以称之为ActivityA)。这是10寸的,景观的情况下。
- 其他简单的情况是,当每一个片段是在自己的活动 - ActivityA包含Frag1; ActivityB包含Frag2和ActivityC包含Frag3。
到目前为止,我们还没有考虑什么是显著不同psented在Android开发者指南中的新闻阅读器的例子$ P $。唯一的主要区别是有三的片段,而不是两个。
So far, we have not considered anything which is significantly different from the News Reader example presented in the Android developer guide. The only major difference is having three fragments instead of two.
现在,7寸片的情况下,可容纳只有2片段。如何将这项工作?请注意,有两种可能的组合在这里:
Now, the case of 7-inch tabs which can accommodate only 2 fragments. How would this work? Note that there are two combinations possible here:
- Frag1和Frag2被显示出来。
- Frag2和Frag3被显示出来。
我只是不能环绕此我的头。我该怎么做这一切在ActivityA?难道我只是创造一个全新的ActivityD?我多少布局就需要创建(我数了一下大约8)?是不是太多permuations?
I'm just unable to wrap my head around this. Do I do all of this within ActivityA? Do I just create an altogether new ActivityD? How many layouts would I need to create (I counted around 8)? Isn't it too many permuations?
我不知道,我上面提出的单活动的方法可能也不是非常适合这个场景 - 因为显示/隐藏片段本身是不平凡的。
I do realize that the single-activity approach I proposed above might also not be a good fit for this scenario - since showing/hiding fragments in itself is non-trivial.
在如何处理这没有得到不堪重负的布局和组合有什么建议?
Any suggestions on how to handle this without getting overwhelmed with layouts and combinations?
推荐答案
这通过@Taylor克拉克回答是pretty的信息。但是,这不是经验与使用单一活性的方法,我问我原来的问题实际的共享。我开始改变新闻阅读器的例子来自Android开发者指南使用单一活性的方法,并提出了可行的解决方案。
This answer by @Taylor Clark was pretty informative. However, it wasn't an actual sharing of experience with using the single-activity approach as I asked in my original question. I set out to modify the News Reader example from the Android developer guide to use the single-activity approach and came up with a workable solution.
什么还有待观察是什么样的使用情况下,这种做法是preferable在多重活动方法(或是否有这样的情况的话)。另外,我还没有看详细介绍一下在编辑1描述了我的问题了3窗格的场景。
What remains to be seen is what are the use cases where this approach is preferable over the multiple-activity method (or whether there are any such cases at all). Also, I haven't looked in detail about the 3-pane scenario described in Edit 1 of my question.
我希望我的后整个项目不久,但这里是我如何去它的快速概览:
I hope to post my entire project shortly, but here is a quick overview of how I went about it:
- 单
活动
:NewsActivity - 两个片段:
TitlesListFragment
和DetailsFragment
- 在两个片段始终是美元的
NewsActivity
点$ psent。根据当前的双窗格的烦躁,我显示/隐藏相应的片段。
- Single
Activity
: NewsActivity - Two Fragments:
TitlesListFragment
andDetailsFragment
- Both fragments are always present in
NewsActivity
. Depending on the current dual-pane-ness, I show/hide the appropriate fragment.
若干问题我遇到:
指定一个布局为双窗格与否:
在原来的新闻阅读器为例,双面板布局有一个的FrameLayout
举行的新闻细节。我们计算出我们当前是否在双面板布局通过测试该框架布局的存在。
In the original News Reader example, dual-pane layouts have a FrameLayout
for holding the news Details. We figure out whether we are currently in a dual-pane layout by testing for the existence of this Frame Layout.
不过,在我的解决方案,这两个片段总是present在所有布局。我通过包括黑客攻击这样的查看
id为 dualPane
和安卓知名度=水涨船高
中,我想成为双面板和省略这一观点在单面板布局的布局。然后,它的一个问题
However, in my solution, both fragments are always present in all layouts. I hacked this by including a View
with id dualPane
and android:visibility="gone"
in those layouts that I want to be dual-pane and omitting this view in the single-pane layout. Then, it was a matter of
mDualPane = findViewById(R.id.dualPane)= NULL;!
编辑:
有更好的方法来指定双窗格岬比拥有一个虚拟视图。一个我preFER是创建一个布尔资源。例如,我有一个的config.xml
如下:
There are better ways to designate dual pane-ness than having a dummy view. The one I prefer is to create a boolean resource. For example, I have a config.xml
as follows:
<resources>
<bool name="dual_pane">false</bool>
</resources>
我可以再放置在文件夹的其他的config.xml
文件,如价值观XLARGE陆
,值端口
或价值观sw600dp
等,并调整布尔值真
或假
,我的愿望。
I can then place additional config.xml
files in folders like values-xlarge-land
, values-port
or values-sw600dp
etc and adjust the boolean value to true
or false
as I desire.
那么,在C中,它的$ C $是一个原则问题 getResources()getBoolean(R.bool.dual_pane);
Then, in the code it is a matter of getResources().getBoolean(R.bool.dual_pane);
关闭详细信息片段
这是活动
关闭和片段
接近区分的问题。最后,我不得不重写 onBack pressed()
如下:
This was a problem of differentiating between the Activity
close and the Fragment
close. In the end, I had to override onBackPressed()
as follows:
- 在双窗格模式,只需拨打
super.onBack pressed()
; - 在单窗格模式下,如果我们在
TitlesListFragment
,呼叫super.onBack pressed()
; - 在单窗格模式下,如果我们在
DetailsFragment
,然后把它当作关闭的片段。这意味着隐藏,并显示了TitlesListFragment
。
- In dual-pane mode, just call
super.onBackPressed()
; - In single-pane mode, if we are in
TitlesListFragment
, callsuper.onBackPressed()
; - In single-pane mode, if we are in
DetailsFragment
, then treat it as closing the fragment. This means hiding it and showing theTitlesListFragment
.
这是不理想,但它是最好的,我可以想出。
This is not ideal, but it is the best I could come up with.
修改
根据由@SherifelKhatib的意见建议,有一个更清洁的方法来处理后退按钮presses:只需添加到片段backstack,显示/隐藏的细节片段的交易。这样,当你preSS后退按钮,该片段事务是相反的。您也可以手动弹出backstack如果你想这样做,其他按钮的点击。
Based on the suggestion by @SherifelKhatib in the comments, there is a much cleaner way to handle back-button presses: Simply add to the Fragment backstack, the transaction of showing/hiding the details fragment. That way, when you press the back button, the fragment transaction is reversed. You can also pop the backstack manually if you wish to do so on other button clicks.
这篇关于片段设计:通过显示/在一个活动隐藏片段适应多种屏幕布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!