0

我目前有这个标签视图,这是我从教程中遵循的。一切正常,直到我注意到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;
}
}

三分之一的片段类 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);
}
}
4

4 回答 4

7

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

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;
于 2017-04-26T06:29:33.927 回答
2

公共 PrefAdapter(FragmentManager fm, int NumOfTabs)

您必须声明 Count No NumOfTabs

int getCount()-> 这个Adapter代表的数据集中有多少项。

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

不要'

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

return view ;
于 2017-04-26T06:28:56.573 回答
1

您的getCount()方法返回 0;

于 2017-04-26T06:29:08.387 回答
0

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

首先,在您的PrefAdapter类中,覆盖该getPageTitle()方法并从那里返回相应选项卡位置的选项卡标题。

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

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

于 2017-04-26T06:39:28.317 回答