TabLayout有靜态動态兩種實作方法 并實作多個标題或控件實作滑動
廢話不多說
直接上代碼
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="a16.erin.tablayout_demo.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/mytag_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabSelectedTextColor="#f00"
//如果要實作有多個條目标題實作邊距滑出來的效果一定要加這個屬性 否則實作不了
app:tabMode="scrollable"
>
//靜态注冊直接在布局檔案裡添加标題TabItem 動态注冊在代碼裡實作
<!--<android.support.design.widget.TabItem-->
<!--android:id="@+id/item1"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="taba"-->
<!--/>-->
<!--<android.support.design.widget.TabItem-->
<!--android:id="@+id/item2"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="tabb"-->
<!--/>-->
<!--<android.support.design.widget.TabItem-->
<!--android:id="@+id/item3"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="tabc"-->
<!--/>-->
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/mypager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/mytag_layout"
/>
</RelativeLayout>
具體代碼如下:
private TabLayout tabLayout;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout=(TabLayout)findViewById(R.id.mytag_layout);
pager=(ViewPager)findViewById(R.id.mypager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return new Fragment1();
}
@Override
public int getCount() {
// tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
// app:tabMode="scrollable"當條目太多時需要橫向滑動,添加此方法;
return ;
}
@Override
public CharSequence getPageTitle(int position) {
return "tab"+(position+);
}
});
//動态添加tab按鈕 ;;;;還可以布局檔案裡面添加tabitem;
// tabLayout.addTab( tabLayout.newTab().setText("tab1"));
// tabLayout.addTab( tabLayout.newTab().setText("tab2"));
// tabLayout.addTab( tabLayout.newTab().setText("tab3"));
// tabLayout.addTab( tabLayout.newTab().setText("tab4"));
//關聯viewpager與tablayout
tabLayout.setupWithViewPager(pager);
for(int i=;i<;i++){
TabLayout.Tab tabitem=tabLayout.getTabAt(i);
//給單個的tab按鈕添加自定義布局;
tabitem.setCustomView(R.layout.item);
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//擷取目前點選位置;
int index= tab.getPosition();
//擷取對應點選tab的布局view;
View view=tab.getCustomView();
ImageView img=(ImageView)view.findViewById(R.id.myimg);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}