未显示 Tabhost 内容

Tabhost content not shown

我创建了一个带底部导航的 Apk,一切正常,但我遇到了问题。在底部导航中,当我第一次选择项目时显示 Tabhost 内容,但当我再次选择此项目时 Tabhost 内容未显示。请帮我。提前致谢。

//我的家Activity

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    private BottomNavigationView.OnNavigationItemSelectedListener 
    mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                    case R.id.ic_public:
                    // code of fragment
                    setTitle("Internet");
                    Packages fragment3 = new Packages();
                    FragmentTransaction fragmentTransaction3 = 
                    getSupportFragmentManager().beginTransaction();
                    fragmentTransaction3.replace(R.id.content, fragment3, 
                    "FragmentName");
                    fragmentTransaction3.commit();
                    return true;  
                    }
                    return false;
                    }
                    };

/// 这是我的片段:

public class Packages extends Fragment {

    View view;

    public Packages() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             android.os.Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_packages, container, false);

            TabHost PackagesTabHost = view.findViewById(R.id.packagestab);
            PackagesTabHost.setup();

            ViewPager PackagesViewPager = view.findViewById(R.id.packagespager);
            PackagesViewPager.setOffscreenPageLimit(2);
            PackagesTabsAdapter TabsAdapter = new PackagesTabsAdapter(getActivity(), PackagesTabHost, PackagesViewPager);

            TabsAdapter.addTab(PackagesTabHost.newTabSpec("one").setIndicator("50 MB"), TurbooneFragment.class, null);
            TabsAdapter.addTab(PackagesTabHost.newTabSpec("two").setIndicator("300 MB"), TurbotwoFragment.class, null);

        }
        return view;
    }

    }

// 这是我的片段 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/packagestab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="56dp"
    tools:context=".Packages"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="@color/cyan_200"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/packagespager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

// 我的 TabsAdapter

    public class PackagesTabsAdapter extends FragmentPagerAdapter
        implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<>();

    /**
     * Container for some information about one tab.
     */
    static final class TabInfo
    {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args)
        {
            clss = _class;
            args = _args;
        }
    }


    static class DummyTabFactory implements TabHost.TabContentFactory
    {
        private final Context mContext;

        DummyTabFactory(Context context)
        {
            mContext = context;
        }

        public View createTabContent(String tag)
        {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }


    public PackagesTabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        //noinspection deprecation // Needed for min API level
        mViewPager.setOnPageChangeListener(this);
    }


    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
    {
        tabSpec.setContent(new DummyTabFactory(mContext));
        tabSpec.getTag();

        TabInfo info = new TabInfo(clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }


    @Override
    public int getCount()
    {
        return mTabs.size();
    }


    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);

        Log.d("HelpFragment", "Creating Fragment " + position);

        return Fragment.instantiate(mContext, info.clss.getName(), info.args);

    }


    @Override
    public void onTabChanged(String tabId)
    {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
    }


    @Override
    public void onPageSelected(int position)
    {
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    public void onPageScrollStateChanged(int state)
    {}
}

我解决了我的问题。我希望这会帮助很多人。我添加我的解决方案:

我添加'@SuppressLint("StaticFieldLeak") 私有静态视图;'完整代码是这样的:

public class Packages extends Fragment {

** @SuppressLint("StaticFieldLeak")
private static View view; **

public Packages() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}


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

    if (view == null) {

        view = inflater.inflate(R.layout.fragment_packages, container, false);

        TabHost PackagesTabHost = view.findViewById(R.id.packagestab);
        PackagesTabHost.setup();

        ViewPager PackagesViewPager = view.findViewById(R.id.packagespager);
        PackagesViewPager.setOffscreenPageLimit(2);
        TabsAdapter PackagesTabsAdapter = new TabsAdapter(getActivity(), PackagesTabHost, PackagesViewPager);

        PackagesTabsAdapter.addTab(PackagesTabHost.newTabSpec("one").setIndicator("50 MB"), TurbooneFragment.class, null);
        PackagesTabsAdapter.addTab(PackagesTabHost.newTabSpec("two").setIndicator("300 MB"), TurbotwoFragment.class, null);

    }
    return view;
}

}