最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述Android学习笔记--TabLayout的简单使用相关的知识,希望能为你提供帮助。
我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合。达到很漂亮的效果。但是TabPageIndicator是第三方的,而且比较老了,当然了现在很多大神都已经开始自己写TabPageIndicator来满足自己的需求,在2015年的google大会上,google发布了新的Android
Support Design
库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。下面我就举一个简单的例子来使用它
fragment_item.xml
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4android:orientation="vertical" 5android:layout_width="match_parent" 6android:layout_height="match_parent"> 7 8< !--app:tabMode - fixed: 固定tab的位置,当tab数量过多时,超出屏幕范围后。无法显示更多。 9scrollable: tab数量超出屏幕范围,可以滚动tab,显示更多的tab--> 10< !-- app:tabTextAppearance="@style/design_widget_TabLayout_TextStyle"--> 11< !-- app:tabIndicatorColor="@color/white"// 下方滚动的下划线颜色--> 12< !--app:tabSelectedTextColor="@color/gray"// tab被选中后,文字的颜色--> 13< !--app:tabTextColor="@color/white"// tab默认的文字颜色--> 14< android.support.design.widget.TabLayout 15android:id="@+id/tab_find_fragment_title" 16android:layout_width="match_parent" android:layout_height="35dp" 17android:background="#e4e4e4" 18app:tabIndicatorColor="?attr/colorPrimary" 19app:tabSelectedTextColor="?attr/colorPrimary" 20app:tabTextColor="@android:color/black" app:tabGravity="fill" 21android:fillViewport="false" /> 22< !--app:tabMode="scrollable"--> 23 24< android.support.v4.view.ViewPager 25android:id="@+id/tab_find_fragment_find_fragment" 26android:layout_width="match_parent" 27android:layout_height="match_parent"/> 28 29 < /LinearLayout>
FindAdapter.java
1 package com.wuxianedu.ademo; 2 3 import android.support.v4.app.Fragment; 4 import android.support.v4.app.FragmentManager; 5 import android.support.v4.app.FragmentPagerAdapter; 6 7 import java.util.List; 8 9 /** 10* Created by Administrator on 2016/10/18. 11* 这个Adapter我也不知道是怎么回事,但是还是要照着写出出来。。。 12*/ 13 14 public class FindAdapter extends FragmentPagerAdapter { 15//这是标题的第一个就显示第一个 16private List< String> titleList; 17//这个是fragmen,把集合中的fragmen都展示出来 18private List< Fragment> fragmentList; 19 20public FindAdapter(FragmentManager fm, List< String> titleList, List< Fragment> fragmentList) { 21super(fm); 22this.titleList = titleList; 23this.fragmentList = fragmentList; 24} 25 26@Override 27public android.support.v4.app.Fragment getItem(int position) { 28return fragmentList.get(position); 29} 30 31@Override 32public int getCount() { 33return titleList.size(); 34} 35/** 36* 获取标题 37* @param position 38* @return 39*/ 40@Override 41public CharSequence getPageTitle(int position) { 42return titleList.get(position); 43} 44 }
FindAFragment.java
1 package com.wuxianedu.ademo; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.design.widget.TabLayout; 6 import android.support.v4.app.Fragment; 7 import android.support.v4.view.ViewPager; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 /** 16* Created by Administrator on 2016/10/18. 17*/ 18 public class FindAFragment extends Fragment { 19 20 21@Nullable 22@Override 23public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 24View view = inflater.inflate(R.layout.fragment_item,null); 25return view; 26} 27 28@Override 29public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 30TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_find_fragment_title); 31ViewPager viewPager = (ViewPager) view.findViewById(R.id.tab_find_fragment_find_fragment); 32List< String> titellist = new ArrayList< > (); 33titellist.add("推举项目"); 34titellist.add("热门项目"); 35titellist.add("最近更新"); 36List< Fragment> fragme ntList = new ArrayList< > ();
37fragmentList.add(new OneFragment()); //这里写你自己Fragment的名字 我是三个, 38fragmentList.add(new TwoFragment()); 39fragmentList.add(new OneFragment()); 40tabLayout.setTabMode(TabLayout.MODE_FIXED); 41FindAAdapter findAAdapter = new FindAAdapter(getActivity().getSupportFragmentManager(),titellist,fragmentList); 42viewPager.setAdapter(findAAdapter); 43tabLayout.setupWithViewPager(viewPager); 44super.onViewCreated(view, savedInstanceState); 45} 46 }
MainActivity.java
1 package com.wuxianedu.ademo; 2 3 import android.support.v4.app.FragmentPagerAdapter; 4 import android.support.v4.app.FragmentTransaction; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 8 public class MainActivity extends AppCompatActivity { 9 10@Override 11protected void onCreate(Bundle savedInstanceState) { 12super.onCreate(savedInstanceState); 13setContentView(R.layout.activity_main); 14//往Fragment添加东西 15FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 16//往Fragment添加东西 17fragmentTransaction.add(R.id.frame_id,new FindFragment()); 18//往Fragment添加东西 19fragmentTransaction.commit(); 20} 21 }
【Android学习笔记--TabLayout的简单使用】OnFragmetn.java 1 package com.wuxianedu.ademo;
2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.Fragment; 6 import android.support.v4.view.ViewPager; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.TableLayout; 11 import android.widget.TextView; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 /** 17* Created by terry-song on 2016/10/18. 18*/ 19 20 public class OneFragment extends Fragment { 21 22@Nullable 23@Override 24public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 25View view = inflater.inflate(R.layout.fragment_test,null); 26return view; 27} 28 29@Override 30public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 31TextView textView = (TextView) view.findViewById(R.id.tv_show_id); 32textView.setText("第1个页面"); 33super.onViewCreated(view, savedInstanceState); 34} 35 }
其他
两个也一样,
推荐阅读
- android的线性布局
- android 6.0权限检测和请求
- Android入门--实现选择并编辑图片设置成头像
- android继承TextView的高度宽度计算问题
- android Toast大全(五种情形)建立属于你自己的Toast
- android 5.0 imageButton自带阴影解决方案
- Android官方开发文档Training系列课程中文版(Android的JNI相关)
- android 6.0权限判断音频 拍照 相册
- Android 手机卫士7--黑名单拦截