ViewPager與android.support.design.widget.TabLayout雙向互動聯動切換

來源:互聯網
上載者:User

標籤:

通常,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雙向互動聯動切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.