我正在尝试使用最新的Android支持设计库将ViewPager添加到片段中。以下是我的布局XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
对应的Java类如下:
public class Tracks extends Fragment {
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tracks, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
AppCompatActivity activity = (AppCompatActivity) getActivity();
assert activity.getSupportActionBar() != null;
activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.app_theme)));
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new TopTracks(), "Top Tracks");
viewPagerAdapter.addFragment(new WorldCharts(), "World Charts");
viewPagerAdapter.addFragment(new NewMusic(), "New Music");
viewPagerAdapter.addFragment(new AfricaHot(), "Africa Hot");
viewPagerAdapter.addFragment(new Playlists(), "Playlists");
viewPagerAdapter.addFragment(new Recommended(), "Recommended");
viewPager.setAdapter(viewPagerAdapter);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList = new ArrayList<>();
List<String> fragmentTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitles.get(position);
}
public void addFragment(Fragment fragment, String name) {
fragmentList.add(fragment);
fragmentTitles.add(name);
}
}
}
viewpager除了选项卡无法显示之外,其余功能都正常。我只能看到以下屏幕:
![The tablayout is present but tabs are absent](https://istack.dev59.com/JyQ0I.webp)
我已经多次检查代码,但仍然无法确定问题出在哪里。