里面ViewPager Android的主/从流 [英] Android Master/Detail flow inside ViewPager

查看:146
本文介绍了里面ViewPager Android的主/从流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也遇到过,我很无力解决一个问题,我用Google搜索了很多,但无法找到一个解决方案。

I have encountered a problem which i'm unable to solve, I googled a lot but couldn't find a solution.

在我的应用程序我有一个ViewPager其中包含3个页面。这些页面片段,因为他们应该的。这工作好了。然而其中的两个页面必须是主/从流量(一个好友列表,一个是新闻提要)。当我创建一个主/详细信息流与Eclipse 2片段和2个活动被创建。所述ItemListActivity(一个需要被示出)实现了片段的回调,这是一种活动,但ViewPager只接受内部本身片段

In my app I have a ViewPager which contains 3 pages. These pages are fragments as they should be. This works alright. However two of these pages must be Master/Detail flows (one for friends list, one for news feed). When I create a master/detail flow with eclipse 2 fragments and 2 activities are created. The ItemListActivity (the one that needs to be shown) implements the fragment's Callbacks, and this is an Activity, but ViewPager only accepts Fragments inside itself.

所以,问题是:我怎样才能把里面的一个ViewPager主/流量

So the question is: How can I place Master/Detail Flow inside a ViewPager?

所有帮助是非常AP preciated! 我所有离开这里的想法。

All help is much appreciated! I'm all out of ideas here.

感谢

推荐答案

主/从流量只是在相同的内容来看,一个在左边,一个在右边被用于2片段的绰号。这是一般的想法。现在,主/详细流程可以在活动内使用(如Eclipse为您做介绍的模板)或一个片段里,幸运的是由于Android 4.2内的其他片段引入片段(这也是旧版本支持Android的兼容库Android系统)。

Master/Detail Flow is just a nickname for 2 fragments being used in the same content view, one on the left and one on the right. That's the general idea. Now, the Master/Detail Flow can be used inside an Activity (like Eclipse does for the template you described) or inside a Fragment, fortunately since Android 4.2 introduced Fragments inside other Fragments (this is also supported by the Android Compatibility Library for older version of Android).

Eclipse生成的模板被包含在一个活动(平板电脑),或2个活动(电话),因为Eclipse不知道你需要一个片段内主/细节。所以,你不能依赖于Eclipse的模板。但是,这并不难,只要把2片段在另一个片段。

The template generated by Eclipse is contained in an Activity (on tablets), or 2 Activities (for phones), because Eclipse doesn't know you need master/detail inside a fragment. So, you can't rely on Eclipse's template. But it's not that hard to just put 2 Fragments inside another Fragment.

您只需要忘记ViewPager了一会儿,做片段在你的工作,将包含主/详细信息流,我们称之为MasterFragment。

You just need to forget about the ViewPager for a while and do your work inside the Fragment that will contain the Master/Detail Flow, let's call it MasterFragment.

  1. 在声明2个片段:InnerListFragment,InnerDetailFragment。另外一个InnerDetailActivity手机。
  2. 声明2 XML布局文件MasterFragment:

  1. Declare 2 more Fragments: InnerListFragment, InnerDetailFragment. Also a InnerDetailActivity for phones.
  2. Declare 2 XML layout files for MasterFragment:

  • 布局/ fragment_master.xml - 添加此只有InnerListFragment内<片断> 标记
  • 布局sw600dp / fragment_master.xml(用于平板电脑> 7) - 同时添加InnerListFragment和InnerDetailFragment <片断> 标记
  • layout/fragment_master.xml - add inside this only the InnerListFragment <fragment> tag
  • layout-sw600dp/fragment_master.xml (for tablets >7") - add both InnerListFragment and InnerDetailFragment <fragment> tags.

现在,我不会详细说明所有的Java code在这里,但里面MasterFragment.java,你需要检查,如果该设备是一个平板电脑,让你知道其中2片段是活跃在在屏幕上。

Now, I won't detail all your java code here, but inside MasterFragment.java, you need to check if the device is a tablet so that you know which of the 2 fragments are active on the screen.

  • 如果它是一个电话,只InnerListFragment是可见的。因此,只需添加列表,onClickListener将开始InnerDetailActivity中,你需要把InnerDetailFragment。

  • if it's a phone, only InnerListFragment is visible. So just add the list and onClickListener will start InnerDetailActivity in which you need to put InnerDetailFragment.

如果它是一个平板电脑,无论是内部片段可见。不要一开始就一个列表项一下,当一个新的活动,只是有一个参考的内部MasterFragment两个片段,所以你可以在它们之间进行通信。

if it's a tablet, both Inner fragments are visible. Don't start a new activity when clicking on a list item, just have a reference to both fragments inside MasterFragment so you can communicate between them.

这是一般的想法。如果您还有疑问,请询问。

That's the general idea. If you still have questions, do ask.

这篇关于里面ViewPager Android的主/从流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆