我有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