如何在FragmentTabHost中获取当前选定的片段

问题描述 投票:6回答:2

存在一些与此问题有关的问题。例如,this one。但这不起作用。

让我展示我在代码中所做的事情。

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/my_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.zhouhao.test.MyFragment"
        tools:layout="@layout/fragment_my" />

</FrameLayout>

fragment_my.xml(我的主要片段包括FragmentTabHost)

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100"
        android:orientation="horizontal">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="95"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/panel_content"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

fragment_tab1.xml(对于与不同选项卡相对应的片段,它们是相似的,因此我只向您显示一个代码)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.zhouhao.test.TabFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
        android:text="@string/fragment_tab1" />

</FrameLayout>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        FragmentManager fm = getSupportFragmentManager();
        mMyFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
    }
}

公共类MyFragment扩展Fragment实现TabHost.OnTabChangeListener {

FragmentTabHost mFragmentTabHost;


public MyFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my, container, false);
    if (rootView != null) {
        mFragmentTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.panel_content);
        TabFragment1 tab1Fragment = new TabFragment1();
        TabFragment2 tab2Fragment = new TabFragment2();
        TabFragment3 tab3Fragment = new TabFragment3();
        TabHost.TabSpec spec1 = mFragmentTabHost.newTabSpec("1").setIndicator("");
        TabHost.TabSpec spec2 = mFragmentTabHost.newTabSpec("2").setIndicator("");
        TabHost.TabSpec spec3 = mFragmentTabHost.newTabSpec("3").setIndicator("");
        mFragmentTabHost.addTab(spec1,tab1Fragment.getClass(), null);
        mFragmentTabHost.addTab(spec2,tab2Fragment.getClass(), null);
        mFragmentTabHost.addTab(spec3,tab3Fragment.getClass(), null);
        mFragmentTabHost.setOnTabChangedListener(this);
        return rootView;
    } else {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

@Override
public void onTabChanged(String tabId) {

    BaseFragment f = (BaseFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tabId);
    Log.d("Log",tabId);
}

}

我的问题是,BaseFragment在我的onTabChanged中始终为null。有人可以帮忙吗?谢谢。

android android-fragments fragment-tab-host
2个回答
4
投票

如果从未实例化片段,则无法立即获得所选片段。


0
投票

我认为我找到了解决这个问题的正确方法,下面是我的答案。

© www.soinside.com 2019 - 2024. All rights reserved.