我有TabLayout和ViewPager片段在里面:手柄TabLayout选项卡中单击
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="layout.LinksFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.view.ViewPager
android:id="@+id/pager_info"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</FrameLayout>
在我的片段文件,里面onCreateView()方法,我设置了两个ViewPager和TabLayout:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_links, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout_info);
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager_info);
PagerAdapterLinks adapter = new PagerAdapterLinks(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
return view;
}
在android 5及更高版本上,一切正常,我可以使用滑动或通过单击选项卡名称在选项卡之间切换。然而,在较低的android版本上,单击选项卡不会做任何事情,并且在ViewPager中更改页面的唯一方法是通过滑动。我如何让我的TabLayout在包括4.1和4.4在内的Android上工作?
感谢您的帮助!
我不是在寻找一个完整的教程中,我想知道如何使这个方法与API16兼容。你粘贴的内容与我的代码相同,在Android 4.4下不起作用 – TheTechGuy96