我创建了一个底部导航Apk,一切都好,但我有问题。在底部导航时,当我选中Item第一次显示Tabhost内容时,但当我再次选中此项时,Tabhost内容未显示。请帮我。谢谢你。
//我的家庭活动
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)
{}
}
答案 0 :(得分:0)
我解决了我的问题。我希望这会对很多人有所帮助。我添加了我的解决方案:
我添加'@SuppressLint(“StaticFieldLeak”) private static View view;' Complate代码是这样的:
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;
}
}