通常,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 @Override 18 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 @Override 41 public CharSequence getPageTitle(int position) { 42 43 return "选项" + position; 44 } 45 46 @Override 47 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 @Override 58 public void destroyItem(View container, int position, Object object) { 59 ((ViewPager) container).removeView((View) object); 60 61 } 62 63 @Override 64 public int getCount() { 65 return COUNT; 66 } 67 68 @Override 69 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.TabLayout 13 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.ViewPager 21 android:id="@+id/viewPager" 22 android:layout_weight="1" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" /> 25 26 </LinearLayout>
时间: 2024-10-13 15:56:11