Two-way interactive switchover between ViewPager and android. support. design. widget. TabLayout and tablayoutviewpager
Generally, android. support. design. widget. TabLayout is used together with Android ViewPager to implement switching and linkage with ViewPager.
(1) For example, when a user selects one item in TabLayout through a finger touch, ViewPager should automatically switch to the corresponding Page;
(2) Similarly, when you flip the ViewPager, TabLayout automatically rolls to the Child item tab corresponding to the current ViewPager page and makes the child item tab in the selected state.
Main Activity MainActivity. java used for testing:
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 Ctivity {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. s EtAdapter (new MyAdapter (this); 28 29 // two-way interaction between TabLayout and 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 "option" + 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 (30366f); 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}
Layout file required by 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 indicator (the horizontal color under the font) display color --> 9 <! -- App: tabSelectedTextColor selected font color --> 10 <! -- App: The font color that is not selected in 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>