标题条指示所选选项卡在一次滑动时不会自动移动到下一个选项卡。它相应地移动到滑动手势,例如,如果你只是滑动一点,即使它指示相同的标签,它也会移动一点。
我想在片段中实现制表符布局,因为用户将从导航抽屉在此页面上导航。
Xml代码
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/aboutTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/aboutViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
托管xml的代码
public class AboutFragment extends Fragment{
@BindView(R.id.aboutTabs)
public TabLayout tabLayout;
@BindView(R.id.aboutViewPager)
public ViewPager viewPager;
private View rootView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public AboutFragment() {
}
public static Fragment newInstance() {
Fragment fragment = new AboutFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_about, container, false);
ButterKnife.bind(this, rootView);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
private void setupViewPager(ViewPager viewPager) {
AboutPagerAdapter adapter = new AboutPagerAdapter(getChildFragmentManager());
adapter.addFragment(new AboutUsFragment(), getResources().getString(R.string.str_about_us));
adapter.addFragment(new MissionFragment(), getResources().getString(R.string.str_about_mission));
adapter.addFragment(new ValuesFragment(), getResources().getString(R.string.str_about_values));
viewPager.setAdapter(adapter);
}
}
适配器
public class AboutPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public AboutPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
导致此问题的原因是什么?我在哪里弄错了? 提前致谢
答案 0 :(得分:0)
如果片段为空,则会观察到此行为。所以只需在各自的片段中添加您的布局,它就可以解决问题。