我有2个标签和2个片段。我为本指南做了所有
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
但现在我看到了标签,但没有看到我的碎片。问题是什么? ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
Direct tab1 = new Direct();
return tab1;
case 1:
AdventuresFragment tab2 = new AdventuresFragment();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
使用此适配器的活动:
public class TabsActivity extends Activity {
Fragment directFragmentTab = new Direct();
Fragment adventuresFragmentTab = new AdventuresFragment();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
() {
@Override
public int getCount() {
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
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) {
}
});
}
}
答案 0 :(得分:1)
在您的活动中,您正在使用PagerAdapter类,而您的片段处理类名为ViewPagerAdapter。您想要使用ViewPagerAdapter类。