gpt4 book ai didi

android - fragment 中的 Tablayout 在抽屉导航中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:10 26 4
gpt4 key购买 nike

我正在创建一个选项卡式布局作为抽屉导航 Activity 的主页 fragment ,

所以我在主要 Activity 中将名为“test”的 fragment 称为默认 fragment 。喜欢

        test home_fragment=new test();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout, home_fragment,home_fragment.getTag()).commit();

如果我再次调用这个 fragment 。喜欢

if (id == R.id.nav_home) {


test home_fragment=new test();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout, home_fragment,home_fragment.getTag()).commit();


}

现在结果是tab1和tab2都没有了

更多引用请看截图

When opening or calling for the first time When opening or calling for the second time time

这是我的测试类(标签式布局)

 package com.hackerinside.jaisonjoseph.testapp;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import static android.R.attr.x;


/**
* A simple {@link Fragment} subclass.
*/
public class test extends Fragment {




public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;





public test() {


}



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


View rootView = inflater.inflate(R.layout.fragment_test, container, false);
// View x = inflater.inflate(R.layout.fragment_test,null);
mSectionsPagerAdapter = new SectionsPagerAdapter (getFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Tab Title 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab Title 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(mViewPager);

return rootView;

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

private SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {


switch (position)
{
case 0:

tab1 tab1=new tab1();

return tab1;
case 1:

tab2 tab2=new tab2();

return tab2;


default:return null;
}



}

@Override
public int getCount() {
// Show 3 total pages.
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";

}
return null;
}
}



}

这是我的标签布局xml代码

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.hackerinside.jaisonjoseph.testapp.MainActivity"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</LinearLayout>

最佳答案

尝试这两种解决方案:

解决方案一

代替

public class SectionsPagerAdapter extends FragmentPagerAdapter {

使用

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

方案二

使用 getChildFragmentManager() 而不是 getFragmentManager()

mSectionsPagerAdapter = new  SectionsPagerAdapter (getChildFragmentManager());

关于android - fragment 中的 Tablayout 在抽屉导航中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42872163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com