Java Android FragmentTabHost-尚未完全烘焙?

Java Android FragmentTabHost-尚未完全烘焙?,java,android,android-fragments,android-tabhost,Java,Android,Android Fragments,Android Tabhost,我想看看是否有人使用新的Android API级别17附带的FragmentTabHost成功定制了标签 我很高兴能够在我的ViewPager SherlockFragments中嵌套一个tabHost,但是我在做一些简单的事情时遇到了困难,比如将选项卡移动到底部或更改选项卡的布局 有人见过使用此功能的好例子吗 这是我在Android文档中能找到的唯一一个例子,几乎没有描述它的用途。它似乎也忽略了布局中为R.id.fragment1定义的任何内容 我想我的问题是,是否有人遇到过一个关于:Frag

我想看看是否有人使用新的Android API级别17附带的FragmentTabHost成功定制了标签

我很高兴能够在我的ViewPager SherlockFragments中嵌套一个tabHost,但是我在做一些简单的事情时遇到了困难,比如将选项卡移动到底部或更改选项卡的布局

有人见过使用此功能的好例子吗

这是我在Android文档中能找到的唯一一个例子,几乎没有描述它的用途。它似乎也忽略了布局中为
R.id.fragment1
定义的任何内容

我想我的问题是,是否有人遇到过一个关于:FragmentTabHost的好教程 或者,如果他们知道如何a)将嵌套的选项卡放在底部或b)更改所述选项卡的布局

我尝试了所有常用的方法,但由于XML布局文件似乎被覆盖了,所以我运气不太好

private FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    setContentView(R.layout.fragment_tabs);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FragmentStackSupport.CountingFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            LoaderCustomSupport.AppListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);

    return mTabHost;
}
在做了一些研究之后,似乎在初始化支持库中的FragmentTabHost时出现了故障。用户对此提出了建议:

FragmentTabHost.java

private void initFragmentTabHost(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs,
            new int[] { android.R.attr.inflatedId }, 0, 0);
    mContainerId = a.getResourceId(0, 0);
    a.recycle();

    super.setOnTabChangedListener(this);

    // If owner hasn't made its own view hierarchy, then as a convenience
    // we will construct a standard one here.


/***** HERE COMMENT CODE BECAUSE findViewById(android.R.id.tabs) EVERY TIME IS NULL WE HAVE OWN         LAYOUT ******//


//        if (findViewById(android.R.id.tabs) == null) {
//            LinearLayout ll = new LinearLayout(context);
//            ll.setOrientation(LinearLayout.VERTICAL);
//            addView(ll, new FrameLayout.LayoutParams(
//                    ViewGroup.LayoutParams.FILL_PARENT,
//                    ViewGroup.LayoutParams.FILL_PARENT));
//
//            TabWidget tw = new TabWidget(context);
//            tw.setId(android.R.id.tabs);
//            tw.setOrientation(TabWidget.HORIZONTAL);
//            ll.addView(tw, new LinearLayout.LayoutParams(
//                    ViewGroup.LayoutParams.FILL_PARENT,
//                    ViewGroup.LayoutParams.WRAP_CONTENT, 0));
//
//            FrameLayout fl = new FrameLayout(context);
//            fl.setId(android.R.id.tabcontent);
//            ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0));
//
//            mRealTabContent = fl = new FrameLayout(context);
//            mRealTabContent.setId(mContainerId);
//            ll.addView(fl, new LinearLayout.LayoutParams(
//                    LinearLayout.LayoutParams.FILL_PARENT, 0, 1));
//        }
}
片段的XML布局:

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">  
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

我认为,将方法
initFragmentTabHost()
设置为构造函数是一个错误。在那个时候,他不想让他的孩子们知道,这是事后发生的<例如,code>LinearLayout,在
onMeasure()
method()中与他的孩子一起工作<构造函数中的code>ViewGroup只需初始化变量,并设置
mChildrenCount=0
()

我所能做的一切,都是成本化
FragmentTabHost

<android.support.v4.app.FragmentTabHost xmlns:a="http://schemas.android.com/apk/res/android"
    a:id="@android:id/tabhost"
    style="@style/Widget.TabHost"
    a:inflatedId="@+id/content" />
代码:

    tabSpec = mTabHost.newTabSpec(tag).setIndicator(createTab(caption));

...

    private View createTab(CharSequence title) {
        final View v = View.inflate(getActivity(), LAYOUT_TAB, null);
        ((TextView) v.findViewById(android.R.id.title)).setText(title);
        return v;
    }
我认为使用
TabWidget
进行其他定制时,我们只能通过编程方式进行操作,如下所示:

    final View tabs = (TabWidget) mTabHost.findViewById(android.R.id.tabs);
    final ViewGroup parent = (ViewGroup) mTabHost.getChildAt(0); 
    parent.removeView(tabs);
    parent.addView(tabs);

哎哟,这不好。

我终于弄清了真相。FragmentTabHost.java存在一个问题,它将始终为您创建一个TabHost元素,而不管您在XML中定义了什么,并预先膨胀

因此,我在编写自己版本的FragmentTabHost.java时注释掉了这部分代码


确保在XML布局中使用新版本,就我所测试的解决方案而言,
。不要用MyFragmentTabHost的构造函数初始化MyFragmentTabHost,这一点很重要。至少如果持有MyFragmentTabHost的类是片段。我还没有测试过零碎的活动

我想提一下关于FragmentTabHost的更多问题。我使用的是一个ViewPager,其中每个页面(视图)都包含一个片段,我必须克服几个问题:

1) FragmentTabHost假定它是其父FragmentManager中唯一的FragmentTabHost(FragmentTabHost.setup()的第二个参数
)。这导致了其余的问题

2) 调用
addTab()
时提供的“标记”将直接传递给FragmentManager,因此,如果您对所有页面使用硬编码标记(这是一件非常合理的事情),您的第一页将创建选项卡片段,而每隔一页将重用这些选项卡。是的,第2页控制第1页

解决方案是生成唯一的标记名。我将页码附加到硬编码字符串:

public Object instantiateItem( ViewGroup container, int position )
{
    ...
    tabHost.addTab( tabHost.newTabSpec( "tab1_" + position ) ...);
    tabHost.addTab( tabHost.newTabSpec( "tab2_" + position ) ...);
    tabHost.addTab( tabHost.newTabSpec( "tab3_" + position ) ...);
    ...
}
3) 所有选项卡片段都放置在仅由“视图id”(FragmentTabHost.setup()
的第三个参数)标识的容器中。这意味着,当FragmentManager将viewId解析为视图时,它始终会找到第一个实例(从第一页)。所有其他页面都将被忽略

解决方案是为“选项卡内容”视图分配唯一ID,例如:

public Object instantiateItem( ViewGroup container, int position )
{
    View view = m_inflater.inflate(R.layout.page, null);

    View tabContent = view.findViewById(R.id.realtabcontent);
    tabContent.setId(m_nextViewId);
    m_nextViewId++;

    MyFragmentTabHost tabHost = (MyFragmentTabHost) view.findViewById(android.R.id.tabhost);
    tabHost.setup(m_activity, m_activity.getSupportFragmentManager(), tabContent.getId());
    ...
}
4) 销毁时不会删除选项卡碎片。当您刷卡时,ViewPager会销毁未使用的视图,但这些视图中包含的FragmentTabHosts会“泄漏”选项卡片段。当ViewPager重新实例化以前看到的页面(使用以前使用的标记)时,FragmentTabHost会注意到这些选项卡的片段已经存在,并简单地重新连接它们。这是因为片段指向已被ViewPager破坏的视图

解决方案是在FragmentTabHost被销毁时删除片段。您需要将此代码添加到FragmentTabHost.java本地副本中的
onDetachedFromWindow()

class MyFragmentTabHost
{
    ...

    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
        mAttached = false;

        boolean removeFragments = false;
        if( mContext instanceof Activity )
        {
            Activity activity = (Activity)mContext;
            removeFragments = !activity.isDestroyed();
        }

        if( removeFragments )
        {
            FragmentTransaction ft = null;
            for (int i = 0; i < mTabs.size(); i++)
            {
                TabInfo tab = mTabs.get(i);
                if (tab.fragment != null)
                {
                    if (ft == null)
                    {
                        ft = mFragmentManager.beginTransaction();
                    }
                    ft.remove(tab.fragment);
                }
            }

            if (ft != null)
            {
                ft.commit();
                mFragmentManager.executePendingTransactions();
            }
        }
    }
类MyFragmentTabHost
{
...
受保护的无效onDetachedFromWindow()
{
super.onDetachedFromWindow();
mAttached=假;
布尔removeFragments=false;
if(活动的mContext实例)
{
活动活动=(活动)mContext;
removeFragments=!activity.isDestroyed();
}
如果(删除碎片)
{
FragmentTransaction ft=null;
对于(int i=0;i


您可能还可以通过使用FragmentPagerAdapter或FragmentStatePagerAdapter(生成片段)而不是标准PagerAdapter(生成视图)来解决这些问题。然后调用
FragmentTabHost.setup(…fragment.getChildFragmentManager()…)

如果你否决我的答案,请留下评论供讨论!那么,你是否碰巧找到了findViewById(android.R.id.tabs)返回null的原因?在一种非常普遍的意义上,该id存在并且应该被找到…@jamis0n如果你在FragmentActivity中使用它,而不是在Fragment相同的堆栈跟踪rece中使用它,则该id不起作用
public Object instantiateItem( ViewGroup container, int position )
{
    ...
    tabHost.addTab( tabHost.newTabSpec( "tab1_" + position ) ...);
    tabHost.addTab( tabHost.newTabSpec( "tab2_" + position ) ...);
    tabHost.addTab( tabHost.newTabSpec( "tab3_" + position ) ...);
    ...
}
public Object instantiateItem( ViewGroup container, int position )
{
    View view = m_inflater.inflate(R.layout.page, null);

    View tabContent = view.findViewById(R.id.realtabcontent);
    tabContent.setId(m_nextViewId);
    m_nextViewId++;

    MyFragmentTabHost tabHost = (MyFragmentTabHost) view.findViewById(android.R.id.tabhost);
    tabHost.setup(m_activity, m_activity.getSupportFragmentManager(), tabContent.getId());
    ...
}
class MyFragmentTabHost
{
    ...

    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
        mAttached = false;

        boolean removeFragments = false;
        if( mContext instanceof Activity )
        {
            Activity activity = (Activity)mContext;
            removeFragments = !activity.isDestroyed();
        }

        if( removeFragments )
        {
            FragmentTransaction ft = null;
            for (int i = 0; i < mTabs.size(); i++)
            {
                TabInfo tab = mTabs.get(i);
                if (tab.fragment != null)
                {
                    if (ft == null)
                    {
                        ft = mFragmentManager.beginTransaction();
                    }
                    ft.remove(tab.fragment);
                }
            }

            if (ft != null)
            {
                ft.commit();
                mFragmentManager.executePendingTransactions();
            }
        }
    }