我有一项活动需要view pager
。所以我定义了5个片段,并按照谷歌文档中显示的视图寻呼机。但我无法看到为TabLayout定义的页面标题。
活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kannada);
viewPager = (ViewPager) findViewById(R.id.viewPager);
//Adapter to populate ViewPager
CategoryAdapter adapter = new CategoryAdapter(this,getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
适配器代码:
public class CategoryAdapter extends FragmentPagerAdapter {
//Number of pages in the ViewPager
private final int NUM_PAGES = 5;
//Context of the app
private Context mContext;
public CategoryAdapter(Context context,FragmentManager fm){
super(fm);
mContext = context;
Toast.makeText(mContext,"Inside Custom Adapter",Toast.LENGTH_SHORT).show();
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new ChatFragment();
} else if (position == 1) {
return new FamilyFragment();
} else if (position == 2) {
return new ColorsFragment();
} else if (position == 3){
return new PhrasesFragment();
}
else {
return new NumbersFragment();
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return mContext.getString(R.string.category_numbers);
} else if (position == 1) {
return mContext.getString(R.string.category_family);
} else if (position == 2) {
return mContext.getString(R.string.category_colors);
} else if(position == 3) {
return mContext.getString(R.string.category_phrases);
}
else{
return mContext.getString(R.string.category_chat);
}
}
}
片段:
public class NumbersFragment extends Fragment {
public NumbersFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout,container,false);
String[] languages = {"Kannada","Tamil","Telugu","Malayalam","Bengali"}; //String array with language name
ListAdapter arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,languages);
ListView listView = (ListView)rootView.findViewById(R.id.list);
listView.setAdapter(arrayAdapter);
Toast.makeText(getActivity(),"Inside Numbers Fragment",Toast.LENGTH_SHORT).show();
return rootView;
}
}
activity_kannada:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.arqamahmad.languageslearnandtalk.Kannada">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</LinearLayout>
那么为什么会出现这个问题,因为我无法看到其他任何方法。我只是将片段作为普通页面而没有TabLayout在顶部。