TabLayout
来源:互联网 发布:为知笔记注销不能用 编辑:程序博客网 时间:2024/06/06 02:34
public class MainActivity extends AppCompatActivity {
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 9;
}
@Override
public CharSequence getPageTitle(int position) {
return "tab"+(position+1);
}
});
//动态添加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=0;i<9;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) {
}
});
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 9;
}
@Override
public CharSequence getPageTitle(int position) {
return "tab"+(position+1);
}
});
//动态添加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=0;i<9;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) {
}
});
}
public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.item,null);
}
}
阅读全文
0 0
- TabLayout
- tablayout
- Tablayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- tablayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- 评论平台
- linux 命令
- linux下自创网络编程聊天室(1)
- MySQL 获得当前日期时间的函数小结
- 数据库一些操作
- TabLayout
- 浅析C语言中strtol()函数与strtoul()函数的用法
- 配置Spring数据源的4种方法
- Fail2ban 删除策略 使用firewalld
- centos7最小化安装手册
- WordPress发送邮件提示“您的主机禁用了Mail函数”解决方法
- ELK hadoop hbase
- linux matplotlib 库的运用
- 读书笔记_算法第四版(二)