在Android上实现类似于应用内导航系统的Instagram [英] Implementing Instagram like in-app navigation system on Android

查看:121
本文介绍了在Android上实现类似于应用内导航系统的Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现类似于Instagram Android客户端中使用的导航系统。

I have to implement a navigation system similar to the one used in the Instagram Android client.


  1. 屏幕底部应始终有一个永久性标签栏。

  2. 用户在其中一个选项卡中导航得更深,让我们说到详细视图,然后切换到另一个选项卡,然后切换回上一个选项卡,应显示最后显示的(更深)细节视图,并在背面按下时,应该迭代回到所述标签的主视图。

到目前为止我想出的是以下内容:

What I have came up with so far is the following:


  • 我有一个MainAcitvity显示底部的菜单。

  • 在选择菜单点时,appropriet显示片段。

  • 当用户在片段中进一步导航时,它会要求MainActivity按给定的标准更改其内容,从而更改显示的片段。

  • 我通过调用FragmentTransaction的addToBackStack()方法将所有Fragment更改添加到backStack。

  • I have a MainAcitvity showing the menu on the bottom.
  • On selecting a menu point, the appropriet Fragment is shown.
  • When the user navigates further within a Fragment, it then asks the MainActivity to change its content by the given criterias, resulting in changing the Fragment shown.
  • I add all the Fragment changes to the backStack, by calling the FragmentTransaction's addToBackStack() method.

我是卡在这一点上,无法弄清楚如何在背面按下时切换片段,以及如何在显示更深的视图而不是选项卡的主视图时处理标签导航。

I am stuck at this point, and cannot figure out how to switch fragments on back presses, and how to handle tab navigations when deeper views are shown instead the main views of the tabs.

我正在考虑为每个标签使用我自己独立的backstack实现。当用户在选项卡中更深入地导航时,我会生成一个唯一的标记,并在调用addToBackStack()时使用该标记,并将标记放在我实现的backStack中。如果用户再次导航到此选项卡,我可以检查该选项卡的backStack中是否有任何标签,如果是,则在MainActivity的fragmentManager中的真实backStack中查找该条目,然后切换到它。

I am thinking of using my own separate "backstack implementations" for every tab. When the user navigates deeper within a tab, i generate a unique "tag" and use that tag when calling addToBackStack() and also putting the tag in the "backStack" implemented by me. In case the user navigates again to this tab, i can check if i have any tags in the "backStack" for that tab, and if so, then look up that entry in the real backStack in the fragmentManager of the MainActivity, and switch to it.

我还没有想出更好的东西。是否有更好/更简单的方法来实现上述行为?我错过了什么吗? (我知道这在Android世界中是非常糟糕的应用程序设计,但这是另一个问题)

I could not come up with anything better yet. Is there any better/simpler way to attchieve the said behaviour? Am i missing something? (I know this is really bad application design in the Android world, but it is another question)

推荐答案

我发布了一个答案因为这个问题已经过时了,但结论可能对其他人有所帮助。

I am posting an answer since the question is pretty dead, yet the conclusion might be helpful for others.

我们最终坚持使用老式的NavgationDrawer模式,效果很好。但与此同时,我必须实现一个库项目,它为托管应用程序提供了一个Fragment,它有自己的自定义逻辑。然后,此片段使用其ChildFragmentManager在其自身内添加另一个片段。 ChildFragmentManager移植到Android Support v4 lib中,因此您可以在任何地方基本使用它。

We ended up sticking with the old fashioned NavgationDrawer pattern, which worked well. But in the meantime I had to implement a library project which provided a Fragment to the hosting Application which had its own custom logic. This Fragment then used its ChildFragmentManager, to add another Fragments inside itself. ChildFragmentManager is ported back in the Android Support v4 lib, so you can use it basicly everywhere.

因此,假设您想要x菜单点,您可以在其中进行更深入的导航。这些将是片段使用他们自己的ChildFragmentManagers添加其他片段以在该菜单中更深入地导航。 ChildFragmentManagers有自己的后台堆栈,所以你不必担心处理状态。如果选择了另一个菜单,您可以在MainActivitys FragmentManager中查找相应的片段,然后返回到它,如果尚未添加,则添加它。

So lets say you want x Menu points in which you can navigate deeper. Those will be the Fragments using their own ChildFragmentManagers to add other Fragments to navigate deeper within that Menu. ChildFragmentManagers have their own back stack, so you dont have to worry that much about handling states. If another Menu is selected, you can look up the corresponting Fragment in the MainActivitys FragmentManager, and change back to it, or add it if it has not yet been added.

请注意,您必须自己实现后退功能,因为ChildFragmentManagers不会自动获取backPressed事件。您可以通过处理MainActivity中的onBackPressed事件来完成此操作。

Be careful, you have to implement back functionality yourself, since ChildFragmentManagers will not get the backPressed event automatically. You can do this by handling the onBackPressed event in your MainActivity.

@Override
public void onBackPressed() {
  boolean handled = false;
  if(getFragmentManager().getBackStackEntryCount() == 0){
    // No menu added
    return super.onBackPressed();
  }

    Fragment frag =    getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount() - 1);

  if (frag instanceof XMenuFragment && frag.isVisible()) {
    FragmentManager childFm = frag.getChildFragmentManager();
    if (childFm.getBackStackEntryCount() > 0) {
      // pop the last menu sub-Fragment
      childFm.popBackStack();
      handled = true
    }
  }

  if(!handled){
    super.onBackPressed();
  }
}

我使用了不同的代码,因此可能包含错误,但我希望概念的重点是明确的。

I used a different code, so it might contain errors, but i hope that the point of the concept is clear.

这篇关于在Android上实现类似于应用内导航系统的Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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