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

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

问题描述

我写了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的替代此类。似乎是在我的感觉pcated德$ P $。   - <一个href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html">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可用

  • <一个href="http://developer.android.com/about/versions/android-4.2.html#NestedFragments">http://developer.android.com/about/versions/android-4.2.html#NestedFragments
  • <一个href="http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()">http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
  • http://developer.android.com/about/versions/android-4.2.html#NestedFragments
  • http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()

不幸的是,几乎没有任何很好的例子在那里,显示了碎片用途的最佳做法,而不支持库,即使是在整个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而不是片段等。?) 或者,这将是最好的做法?

So I am wondering: is it simply not possible to write 4.1 apps with nested fragments without using the support library and everything that comes with it? (need to use FragmentActivity instead of Fragment, etc.?) Or what would be the best practice?

这是我目前有在发展中的问题,正是这样的说法:

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

  • <一个href="http://developer.android.com/about/versions/android-4.2.html#NestedFragments">http://developer.android.com/about/versions/android-4.2.html#NestedFragments

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.

注:不能夸大布局成一个片段时布局   包括&LT;片断&gt; 。当加入嵌套的片段只支持   一个片段动态。

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.

(这实际上可能不止一个问题,维基条目,但也许其他人之前已经成功的话)。

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

更新:

一个有用的答案是:片段里面片段

推荐答案

所以筑巢片段另一个片段里是不可能的,XML,无论您使用的版本 FragmentManager 的。

Limitations

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

所以,你必须通过增加code片段,这似乎是一个问题,但是从长远来看,让您的布局超柔。

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 的精髓在于它推迟装载直到previous片段的交易已经完成。当然,这只是自然地在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).

请参阅;这是一个片段的推迟装载,所以片段 S能在其内部被加载。

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

其pretty的简单,处理程序是一个非常非常方便类,有效的处理程序等待一个空间到当前片段事务后,主线程上执行已完成承诺(如片段干扰他们的主线程上运行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);
}

我不认为这是最佳做法,但我在使用这个技巧的现场应用程序,我没有任何问题了。

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(4.2&LT)嵌套碎片最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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