標籤:
通常,android.support.design.widget.TabLayout與Android的ViewPager聯合使用,實現與ViewPager的切換與聯動。
(1)比如,當使用者手指觸控選取TabLayout中的某一項時候,ViewPager應當自動切換跳轉到相應的Page頁面;
(2)同樣,當使用者在翻動ViewPager時候,TabLayout也會自動的滾動到與當前ViewPager頁面像對應的子item tab並使該子item tab處於選中狀態。
測試用的主Activity MainActivity.java :
1 package com.zzw.testtablayout; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.support.design.widget.TabLayout; 6 import android.support.v4.view.PagerAdapter; 7 import android.support.v4.view.ViewPager; 8 import android.view.Gravity; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.TextView;12 13 public class MainActivity extends Activity {14 15 private final int COUNT = 10;16 17 @Override18 protected void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 setContentView(R.layout.activity_main);21 22 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);23 24 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);25 26 ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);27 viewPager.setAdapter(new MyAdapter(this));28 29 // TabLayout和ViewPager雙向、互動聯動。30 tabLayout.setupWithViewPager(viewPager);31 }32 33 private class MyAdapter extends PagerAdapter {34 private Activity activity;35 36 public MyAdapter(Activity activity) {37 this.activity = activity;38 }39 40 @Override41 public CharSequence getPageTitle(int position) {42 43 return "選項" + position;44 }45 46 @Override47 public Object instantiateItem(View container, int position) {48 TextView tv = new TextView(activity);49 tv.setText("ViewPager" + position);50 tv.setTextSize(30.0f);51 tv.setGravity(Gravity.CENTER);52 53 ((ViewGroup) container).addView(tv);54 return tv;55 }56 57 @Override58 public void destroyItem(View container, int position, Object object) {59 ((ViewPager) container).removeView((View) object);60 61 }62 63 @Override64 public int getCount() {65 return COUNT;66 }67 68 @Override69 public boolean isViewFromObject(View arg0, Object arg1) {70 return arg0 == arg1;71 }72 73 }74 }
MainActivity.java需要的布局檔案:activity_main.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" > 7 8 <!-- app:tabIndicatorColor 指標(字型下面的那一個橫)顯示顏色 --> 9 <!-- app:tabSelectedTextColor 選中的字型顏色 -->10 <!-- app:tabTextColor 未選中的字型顏色 -->11 12 <android.support.design.widget.TabLayout13 android:id="@+id/tabLayout"14 android:layout_width="match_parent"15 android:layout_height="wrap_content"16 app:tabIndicatorColor="#26c6da"17 app:tabSelectedTextColor="#f44336"18 app:tabTextColor="#bdbdbd" />19 20 <android.support.v4.view.ViewPager21 android:id="@+id/viewPager"22 android:layout_weight="1"23 android:layout_width="match_parent"24 android:layout_height="wrap_content" />25 26 </LinearLayout>
ViewPager與android.support.design.widget.TabLayout雙向互動聯動切換