我正在尝试使用新的Android支持设计库将一个viewpager添加到一个片段.我有布局的xml代码,如下所示:
相应的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
viewpager工作正常,除了我的标签不会显示的事实.我得到的是以下屏幕:
最佳答案
@H_502_13@这是与ViewCompat相关的设计支持库的常见错误.
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViewPager(viewPager);
// after you set the adapter you have to check if view is laid out,i did a custom method for it
if (ViewCompat.isLaidOut(tabLayout)) {
setViewPagerListener();
} else {
tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v,int left,int top,int right,int bottom,int oldLeft,int oldTop,int oldRight,int oldBottom) {
setViewPagerListener();
tabLayout.removeOnLayoutChangeListener(this);
}
});
}
}
private void setViewPagerListener() {
tabLayout.setupWithViewPager(viewPager);
// use class TabLayout.ViewPagerOnTabSelectedListener
// note that it's a class not an interface as OnTabSelectedListener,so you can't implement it in your activity/fragment
// methods are optional,so if you don't use them,you can not override them (e.g. onTabUnselected)
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
}
});
}