Вкладка всегда выбрана в TabHost

У меня проблема с моим TabHost, внутри фрагмента первая выбранная вкладка всегда будет выделена, поэтому у меня часто выбираются две вкладки, как показано здесь

первое изображение, вкладки уведомлений выделены, когда не должны

Здесь уведомления не должны выделяться!

Но проблема только для вкладки «Уведомления», как вы можете видеть здесь, вкладка «Друзья» ведет себя нормально, потому что она добавлена ​​​​второй.

Вторая картинка здесь, друзья работают нормально

Вот мой код для фрагмента, содержащего мой TabHost

public class ProfileContentFragment extends Fragment {
private FragmentTabHost mTabHost;
public ProfileContentFragment() {
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    View rootView = inflater.inflate(R.layout.profile_content_fragment,container, false);


    mTabHost = (FragmentTabHost)     rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getFragmentManager(),      android.R.id.tabcontent);

              mTabHost.addTab(mTabHost.newTabSpec("notifications").setIndicator("Notifications    "),
            NotificationsFragment.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("friends").setIndicator("Friends"),
            FriendsFragment.class, null);

    return rootView;
}

@Override
public void onDestroyView(){
    super.onDestroyView();;
    mTabHost=null;
}
}

А вот XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent">
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </FrameLayout>

        </LinearLayout>


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

person Gabriel Hurtado    schedule 01.04.2016    source источник
comment
Если вы решили свой собственный вопрос, опубликуйте ответ с решением и примите его, а не редактируйте вопрос.   -  person user    schedule 02.04.2016


Ответы (1)


РЕШЕНО:

Решилось заменой

 mTabHost.setup(getActivity(), getFragmentManager(),     android.R.id.tabcontent);

To :

mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

person Gabriel Hurtado    schedule 02.04.2016