Tablayout + Viewpager + Fragment 聯動顯示
阿新 • • 發佈:2019-02-13
效果圖:
上面是tablayout,下面是viewpager(巢狀fragment)
首先匯入依賴
- compile 'com.android.support:design:25.3.1'
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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:orientation="vertical">
- <android.support.design.widget.TabLayout
- android:id="@+id/tab_layout"
- android:layout_width="match_parent"
- android:layout_height="60dp"
-
app:tabGravity
- app:tabIndicatorColor="#FF995B"
- app:tabMode="scrollable"
- app:tabSelectedTextColor="#FF995B"
- app:tabTextAppearance="@style/tablayout_ziti"
- app:tabTextColor="#9A9A9A"/>
- <android.support.v4.view.ViewPager
- android:id="@+id/view_pager"
-
android:layout_width
- android:layout_height="0dp"
- android:layout_weight="11"/>
- </LinearLayout>
放上styles.xml,標紅部分是新增的樣式
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="notitle" parent="Theme.AppCompat.Light.NoActionBar"> </style> <style name="tablayout_ziti"> <item name="android:textSize">17sp</item> </style> </resources>接著是ManageLeftFragment.java的程式碼,我這裡只展示了兩個fragment,除了第一個tablayout標籤不一樣外,其餘的tablayout標籤都用的一個fragment顯示
- public class ManageLeftFragment extends Fragment {
- @BindView(R.id.tab_layout)
- TabLayout tabLayout;
- @BindView(R.id.view_pager)
- ViewPager viewPager;
- Unbinder unbinder;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_manageleft, container, false);
- unbinder = ButterKnife.bind(this, view);
- final List<String>list = new ArrayList<>();
- //新增4條資料,作為tablayout標籤
- list.add("待稽核");
- list.add("待支付");
- list.add("待參加");
- list.add("已完成");
- //設定viewpager可以預載入全部的頁數,,不會銷燬其他的頁面
- viewPager.setOffscreenPageLimit(list.size());
- viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
- private ZongYiFragment zongYiFragment;
- private QuanbuFragment quanbuFragment;
- @Override
- public CharSequence getPageTitle(int position) {
- return list.get(position);
- }
- @Override
- public Fragment getItem(int position) {
- if (position == 0) {
- //當選中第一個tablayout標籤時候,展示這個fragment
- quanbuFragment = new QuanbuFragment();
- return quanbuFragment;
- } else {
- //選中其他tablayou標籤時候,展示這個fragment
- zongYiFragment = new ZongYiFragment();
- return zongYiFragment;
- }
- }
- @Override
- public int getCount() {
- return list.size();
- }
- });
- //設定tablayout和viewpager聯動
- tabLayout.setupWithViewPager(viewPager);
- return view;
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- unbinder.unbind();
- }
- }