onCreateView 未调用选项卡片段

onCreateView not called tab fragment

我目前有这个选项卡视图,这是我在 this 教程中遵循的。一切正常,直到我注意到 onCreateView 没有在任何选项卡上调用。我四处寻找解决方案,但我仍然无法解决。 这是我的代码:

activity_pref_main_container.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/main_layout"
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PrefActivityMain">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

PrefMainActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pref_main_container);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(R.string.preference_title);
    setSupportActionBar(toolbar);

    new PrefActivityListAsync(this,this,specialization).execute();
    new PrefActivityListAsync(this,this,position).execute();
    new PrefActivityListAsync(this,this,type).execute();

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Specialization"));
    tabLayout.addTab(tabLayout.newTab().setText("Position"));
    tabLayout.addTab(tabLayout.newTab().setText("Type"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PrefAdapter adapter = new PrefAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setOffscreenPageLimit(2);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

PrefAdapder.class

 public class PrefAdapter extends FragmentPagerAdapter {

int mNumOfTabs;

public PrefAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            PrefActivitySpecialization tab1 = new PrefActivitySpecialization();
            return tab1;
        case 1:
            PrefActivityPosition tab2 = new PrefActivityPosition();
            return tab2;
        case 2:
            PrefActivityType tab3 = new PrefActivityType();
            return tab3;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return 0;
}
}

三分之一片段class PrefActivitySpecialization.class

public class PrefActivitySpecialization extends 
android.support.v4.app.Fragment {

private ArrayList<DataPrefSpecialization> dataPrefSpecializationArrayList;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {
    View view = 
inflater.inflate(R.layout.activity_pref_specialization_container, container, 
false);

    dataPrefSpecializationArrayList = ((PrefActivityMain) 
getActivity()).getDataPrefSpecialization();

    for(int i = 0; i < dataPrefSpecializationArrayList.size(); i++){
       Log.d("Check Specialization 
",dataPrefSpecializationArrayList.get(i).getName());
    }

    return inflater.inflate(R.layout.activity_pref_specialization_container, container, false);
}
}

public PrefAdapter(FragmentManager fm, int NumOfTabs)

您必须声明计数编号 NumOfTabs

int getCount ()-> How many items are in the data set represented by this Adapter.

 @Override
public int getCount() 
{
    return mNumOfTabs;
}

不要'

return inflater.inflate(R.layout.activity_pref_specialization_container, container, false);

return view ;

您的 getCount() 方法返回 0;

您应该在两个地方进行更改:

1) 更改

@Override
public int getCount() {
return 0;
}

@Override
public int getCount() {
return mNumOfTabs;
}

2) 改变

return inflater.inflate(R.layout.activity_pref_specialization_container, container, false);

return view;

还有另一种(可能更简单)的方法将视图寻呼机与选项卡布局连接起来-

首先,在您的 PrefAdapter class 中,重写 getPageTitle() 方法和 return 您的标签标题,用于相应的标签位置。

在您的 PrefMainActivity class 中,删除所有行,例如- tabLayout.addTab(...)

改为添加以下行 - tabLayout.setupWithViewPager(viewPager);