不使用支持库的 Android 4.0、4.1 (<4.2) 中嵌套片段的最佳实践 [英] Best practice for nested fragments in Android 4.0, 4.1 (<4.2) without using the support library

查看:21
本文介绍了不使用支持库的 Android 4.0、4.1 (<4.2) 中嵌套片段的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 4.0 和 4.1 平板电脑编写应用程序,我不想使用支持库(如果不需要),但仅因此而使用 4.x api.

I'm writing an app for 4.0 and 4.1 tablets, for which I do not want to use the support libraries (if not needed) but the 4.x api only therefore.

所以我的目标平台被很好地定义为:>= 4.0 和 <= 4.1

So my target platform is very well defined as: >= 4.0 and <= 4.1

该应用具有多窗格布局(两个片段,左侧一个小片段,右侧一个内容片段)和一个带选项卡的操作栏.

The app has a multi-pane layout (two fragments, one small on the left, one content fragment on the right) and an action bar with tabs.

类似:

单击操作栏上的选项卡会更改外部"片段,然后内部片段是具有两个嵌套片段的片段(1. 左侧小列表片段,2. 宽内容片段).

Clicking a tab on the action bar changes the 'outer' fragment, and the inner fragment then is a fragment with two nested fragments (1. small left list fragment, 2. wide content fragment).

我现在想知道替换片段尤其是嵌套片段的最佳实践是什么.ViewPager 是支持库的一部分,此类没有本机 4.x 替代品.在我看来似乎是弃用".- http://developer.android.com/reference/android/support/v4/view/ViewPager.html

I am now wondering what's the best practice to replace fragments and especially nested fragments. The ViewPager is part of the support library, there's no native 4.x alternative for this class. Appear to be 'deprecated' in my sense. - http://developer.android.com/reference/android/support/v4/view/ViewPager.html

然后我阅读了 Android 4.2 的发行说明,关于 ChildFragmentManager,这很合适,但我的目标是 4.0 和 4.1,所以这也不能使用.

Then I read the release notes for Android 4.2, regarding ChildFragmentManager, which would be a good fit, but I am targeting 4.0 and 4.1, so this can't be used either.

ChildFragmentManager 仅在 4.2 中可用

ChildFragmentManager is only available in 4.2

不幸的是,即使在整个 Android 开发者指南中,也几乎没有任何好的示例可以展示在没有支持库的情况下片段使用的最佳实践;尤其是关于嵌套片段的内容.

Unfortunately, there are hardly any good examples out there that show best practices for fragments usages without the support library, even in the entire Android developer guides; and especially nothing regarding nested fragments.

所以我想知道:在不使用支持库及其附带的所有内容的情况下,是否根本不可能编写带有嵌套片段的 4.1 应用程序?(需要使用 FragmentActivity 而不是 Fragment 等?)或者最佳做法是什么?

我目前在开发中遇到的问题正是这个声明:

The problem that I am currently having in the development is exactly this statement:

Android 支持库现在也支持嵌套片段,因此您可以在 Android 1.6 及更高版本上实现嵌套片段设计.

The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

注意:当布局出现时,您不能将布局膨胀为片段包括 .嵌套片段仅在添加时支持动态到一个片段.

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.

因为我在 XML 中定义了嵌套的片段,这显然会导致如下错误:

Because I put define the nested fragments in XML, which apparently causes an error like:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #15: Duplicate id 0x7f090009, tag frgCustomerList, or parent id 0x7f090008 with another fragment for de.xyz.is.android.fragment.CustomerListFragment_

目前,我自己得出结论:即使在 4.1 上,当我什至不想针对 2.x 平台时,如果没有支持库,则无法实现如屏幕截图所示的嵌套片段.

At the moment, I conclude for myself: even on 4.1, when I don't even want to target the 2.x platform, nested fragments as shown in the screenshot are not possible without the support library.

(这实际上可能更像是一个 wiki 条目而不是一个问题,但也许其他人之前已经管理过它).

(This might actually be more of a wiki entry than a question, but maybe somebody else has managed it before).

更新:

一个有用的答案位于:Fragment Inside Fragment

推荐答案

限制

因此,无论您使用哪个版本的 FragmentManager,xml 都不可能将片段嵌套在另一个片段中.

Limitations

So nesting fragments inside another fragment is not possible with xml regardless of which version of FragmentManager you use.

所以你必须通过代码添加片段,这似乎是一个问题,但从长远来看,会让你的布局变得非常灵活.

So you have to add fragments via code, this might seem like a problem, but in the long run makes your layouts superflexible.

所以嵌套而不使用 getChildFragmentManger?childFragmentManager 背后的本质是它推迟加载,直到前一个片段事务完成.当然,只有在 4.2 或支持库中才自然支持.

So nesting without using getChildFragmentManger? The essence behind childFragmentManager is that it defers loading until the previous fragment transaction has finished. And of course it was only naturally supported in 4.2 or the support library.

解决方案,当然!我已经这样做了很长时间了(自从 ViewPager 宣布).

Solution, Sure! I have been doing this for a long time now, (since the ViewPager was announced).

见下文;这是一个延迟加载的 Fragment,所以 Fragment 可以在其中加载.

See below; This is a Fragment that defers loading, so Fragments can be loaded inside of it.

它非常简单,Handler 是一个非常方便的类,有效地处理程序在当前片段事务完成提交后等待一个空间在主线程上执行(因为片段会干扰它们在主线程上运行的 UI).

Its pretty simple, the Handler is a really really handy class, effectively the handler waits for a space to execute on the main thread after the current fragment transaction has finished committing (as fragments interfere with the UI they run on the main thread).

// Remember this is an example, you will need to modify to work with your code
private final Handler handler = new Handler();
private Runnable runPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    return inflater.inflate(R.layout.frag_layout, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    runPager = new Runnable() {

        @Override
        public void run()
        {
          getFragmentManager().beginTransaction().addFragment(R.id.frag_container, MyFragment.newInstance()).commit();
        }
    };
    handler.post(runPager);
}

/**
 * @see android.support.v4.app.Fragment#onPause()
 */
@Override
public void onPause()
{
    super.onPause();
    handler.removeCallbacks(runPager);
}

我不认为这是最佳实践",但我有使用此 hack 的实时应用程序,但我还没有遇到任何问题.

I wouldn't consider it 'best practice', but I have live apps using this hack and I am yet to have any issues with it.

我也使用这种方法来嵌入视图寻呼机 - https://gist.github.com/chrisjenx/3405429

I also use this method for embedding view pagers - https://gist.github.com/chrisjenx/3405429

这篇关于不使用支持库的 Android 4.0、4.1 (&lt;4.2) 中嵌套片段的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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