FlycoTabLayout使用

来源:互联网 发布:国家质检总局网络培训 编辑:程序博客网 时间:2024/06/05 05:58
step1:在module.gradle中添加
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. dependencies{  
  2.     compile 'com.android.support:support-v4:23.1.1'  
  3.     compile 'com.nineoldandroids:library:2.4.0'  
  4.     compile 'com.flyco.roundview:FlycoRoundView_Lib:1.1.2@aar'  
  5.     compile 'com.flyco.tablayout:FlycoTabLayout_Lib:1.5.0@aar'  
  6. }  
  7.   
  8. After v2.0.0  
  9. dependencies{  
  10.     compile 'com.android.support:support-v4:23.1.1'  
  11.     compile 'com.nineoldandroids:library:2.4.0'  
  12.     compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'  
  13. }  

step 2:建立TabEntity这里是tab的标题、选中的图标、未选中的图标

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.example.maoyh.myapplication.app.entity;  
  2.   
  3. import com.flyco.tablayout.listener.CustomTabEntity;  
  4.   
  5. public class TabEntity implements CustomTabEntity {  
  6.     public String title;  
  7.     public int selectedIcon;  
  8.     public int unSelectedIcon;  
  9.   
  10.     public TabEntity(String title, int selectedIcon, int unSelectedIcon) {  
  11.         this.title = title;  
  12.         this.selectedIcon = selectedIcon;  
  13.         this.unSelectedIcon = unSelectedIcon;  
  14.     }  
  15.   
  16.     @Override  
  17.     public String getTabTitle() {  
  18.         return title;  
  19.     }  
  20.   
  21.     @Override  
  22.     public int getTabSelectedIcon() {  
  23.         return selectedIcon;  
  24.     }  
  25.   
  26.     @Override  
  27.     public int getTabUnselectedIcon() {  
  28.         return unSelectedIcon;  
  29.     }  
  30. }  
step 3:建立要关联的fragment
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.example.maoyh.myapplication.app.fragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. import com.example.maoyh.myapplication.app.R;  
  11.   
  12.   
  13. /** 
  14.  * Created by MAOYH on 2016/3/2. 
  15.  */  
  16. public class SimpleCardFragment extends Fragment {  
  17.     private String mTitle;  
  18.   
  19.     public static SimpleCardFragment getInstance(String title) {  
  20.         SimpleCardFragment sf = new SimpleCardFragment();  
  21.         sf.mTitle = title;  
  22.         return sf;  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.     }  
  29.   
  30.     @Override  
  31.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  32.         View v = inflater.inflate(R.layout.layout_fragment, null);  
  33.         TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);  
  34.         card_title_tv.setText(mTitle);  
  35.   
  36.         return v;  
  37.     }  
  38. }  
step 4:设置activity的layout
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     xmlns:tl="http://schemas.android.com/apk/res-auto"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  10.   
  11.     <FrameLayout  
  12.         android:id="@+id/fl_change"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="400dp">  
  15.   
  16.     </FrameLayout>  
  17.   
  18.     <com.flyco.tablayout.CommonTabLayout  
  19.         android:layout_alignBottom="@+id/fl_change"  
  20.         android:id="@+id/tl"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="48dp"  
  23.         android:background="#ffffff"  
  24.         android:layout_alignParentBottom="true"  
  25.         tl:tl_iconGravity="LEFT"  
  26.         tl:tl_iconHeight="18dp"  
  27.         tl:tl_iconMargin="5dp"  
  28.         tl:tl_iconWidth="18dp"  
  29.         tl:tl_indicator_bounce_enable="true"  
  30.         tl:tl_indicator_color="#2C97DE"  
  31.         tl:tl_indicator_gravity="BOTTOM"  
  32.         tl:tl_indicator_height="0dp"  
  33.         tl:tl_textSelectColor="#2C97DE"  
  34.         tl:tl_textUnselectColor="#66000000"  
  35.         tl:tl_textsize="15sp"  
  36.         tl:tl_underline_color="#DDDDDD"  
  37.         tl:tl_underline_gravity="TOP"  
  38.         tl:tl_underline_height="1dp"/>  
  39. </RelativeLayout>  
step 5:fragment的layout

step 6:编码activity

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.example.maoyh.myapplication.app.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v7.app.AppCompatActivity;  
  6.   
  7. import com.example.maoyh.myapplication.app.R;  
  8. import com.example.maoyh.myapplication.app.entity.TabEntity;  
  9. import com.example.maoyh.myapplication.app.fragment.SimpleCardFragment;  
  10. import com.flyco.tablayout.CommonTabLayout;  
  11. import com.flyco.tablayout.listener.CustomTabEntity;  
  12.   
  13. import java.util.ArrayList;  
  14.   
  15. public class MainActivity extends AppCompatActivity {  
  16.     private ArrayList<Fragment> mFragments = new ArrayList<>();  
  17.     private CommonTabLayout mTabLayout;  
  18.     private String[] mTitles = {"首页""消息""联系人""我的"};  
  19.     private int[] mIconUnselectIds = {  
  20.             R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,  
  21.             R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};  
  22.     private int[] mIconSelectIds = {  
  23.             R.mipmap.tab_home_select, R.mipmap.tab_speech_select,  
  24.             R.mipmap.tab_contact_select, R.mipmap.tab_more_select};  
  25.     //tab的标题、选中图标、未选中图标  
  26.     private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();  
  27.   
  28.   
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.        setContentView(R.layout.activity_main);  
  33.         initData();  
  34.         initView();  
  35.         //给tab设置数据和关联的fragment  
  36.         mTabLayout.setTabData(mTabEntities, this, R.id.fl_change, mFragments);  
  37.         //设置红点  
  38.         mTabLayout.showDot(1);  
  39.   
  40.     }  
  41.   
  42.     private void initView() {  
  43.         mTabLayout = (CommonTabLayout) findViewById(R.id.tl);  
  44.     }  
  45.   
  46.     private void initData() {  
  47.         for (String title : mTitles) {  
  48.             mFragments.add(SimpleCardFragment.getInstance( title));  
  49.         }  
  50.         //设置tab的标题、选中图标、未选中图标  
  51.         for (int i = 0; i < mTitles.length; i++) {  
  52.             mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));  
  53.         }  
  54.     }  
  55. }  







0 0
原创粉丝点击