├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── tab_home_select.png
│ │ │ ├── tab_more_select.png
│ │ │ ├── tab_contact_select.png
│ │ │ ├── tab_home_unselect.png
│ │ │ ├── tab_more_unselect.png
│ │ │ ├── tab_speech_select.png
│ │ │ ├── tab_speech_unselect.png
│ │ │ └── tab_contact_unselect.png
│ │ ├── drawable
│ │ │ └── background_card.9.png
│ │ └── layout
│ │ │ ├── fr_simple_card.xml
│ │ │ ├── activity_segment_tab.xml
│ │ │ ├── activity_common_tab.xml
│ │ │ └── activity_sliding_tab.xml
│ │ ├── java
│ │ └── com
│ │ │ └── flyco
│ │ │ └── tablayoutsamples
│ │ │ ├── entity
│ │ │ └── TabEntity.java
│ │ │ ├── ui
│ │ │ ├── SimpleCardFragment.java
│ │ │ ├── SimpleHomeActivity.java
│ │ │ ├── SegmentTabActivity.java
│ │ │ ├── SlidingTabActivity.java
│ │ │ └── CommonTabActivity.java
│ │ │ ├── utils
│ │ │ └── ViewFindUtils.java
│ │ │ └── adapter
│ │ │ └── SimpleHomeAdapter.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── FlycoTabLayout_Lib
├── .gitignore
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── flyco
│ │ │ └── tablayout
│ │ │ ├── listener
│ │ │ ├── OnTabSelectListener.java
│ │ │ └── CustomTabEntity.java
│ │ │ ├── utils
│ │ │ ├── FragmentChangeManager.java
│ │ │ └── UnreadMsgUtils.java
│ │ │ ├── widget
│ │ │ └── MsgView.java
│ │ │ ├── SegmentTabLayout.java
│ │ │ ├── SlidingTabLayout.java
│ │ │ └── CommonTabLayout.java
│ │ ├── AndroidManifest.xml
│ │ └── res
│ │ ├── layout
│ │ ├── layout_tab.xml
│ │ ├── layout_tab_segment.xml
│ │ ├── layout_tab_top.xml
│ │ ├── layout_tab_left.xml
│ │ ├── layout_tab_bottom.xml
│ │ └── layout_tab_right.xml
│ │ └── values
│ │ └── attrs.xml
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── preview_1.gif
├── preview_2.gif
├── preview_3.gif
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── LICENSE
├── CHNAGELOG.md
├── gradlew.bat
├── README_CN.md
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':FlycoTabLayout_Lib'
2 |
--------------------------------------------------------------------------------
/preview_1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/preview_1.gif
--------------------------------------------------------------------------------
/preview_2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/preview_2.gif
--------------------------------------------------------------------------------
/preview_3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/preview_3.gif
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | build
4 | .idea
5 | *.iml
6 | .DS_Store
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FlycoTabLayout
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/background_card.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/drawable/background_card.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_home_select.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_home_select.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_more_select.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_more_select.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_contact_select.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_contact_select.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_home_unselect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_home_unselect.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_more_unselect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_more_unselect.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_speech_select.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_speech_select.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_speech_unselect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_speech_unselect.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/tab_contact_unselect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RDFS/FlycoTabLayout/master/app/src/main/res/mipmap-xhdpi/tab_contact_unselect.png
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/listener/OnTabSelectListener.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout.listener;
2 |
3 | public interface OnTabSelectListener {
4 | void onTabSelect(int position);
5 | void onTabReselect(int position);
6 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Oct 10 13:13:42 CST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #8BC34A
4 | #689F38
5 | #FF4081
6 | #FFFFFF
7 |
8 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/listener/CustomTabEntity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout.listener;
2 |
3 | import android.support.annotation.DrawableRes;
4 |
5 | public interface CustomTabEntity {
6 | String getTabTitle();
7 |
8 | @DrawableRes
9 | int getTabSelectedIcon();
10 |
11 | @DrawableRes
12 | int getTabUnselectedIcon();
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fr_simple_card.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
14 |
15 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/lihui/work/AndroidStudio/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/lihui/work/AndroidStudio/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/entity/TabEntity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.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 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.flyco.tablayoutsamples"
9 | minSdkVersion 11
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:23.4.0'
25 | compile project(':FlycoTabLayout_Lib')
26 |
27 | //--->support 3.0+
28 | // compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.8@aar'
29 | //--->support 2.2+
30 | // compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'
31 | // compile 'com.nineoldandroids:library:2.4.0'
32 | }
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 H07000223
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/ui/SimpleCardFragment.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.ui;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 |
11 | import com.flyco.tablayoutsamples.R;
12 |
13 | @SuppressLint("ValidFragment")
14 | public class SimpleCardFragment extends Fragment {
15 | private String mTitle;
16 |
17 | public static SimpleCardFragment getInstance(String title) {
18 | SimpleCardFragment sf = new SimpleCardFragment();
19 | sf.mTitle = title;
20 | return sf;
21 | }
22 |
23 | @Override
24 | public void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | }
27 |
28 | @Override
29 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
30 | View v = inflater.inflate(R.layout.fr_simple_card, null);
31 | TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
32 | card_title_tv.setText(mTitle);
33 |
34 | return v;
35 | }
36 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
12 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
25 |
28 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/utils/ViewFindUtils.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.utils;
2 |
3 | import android.util.SparseArray;
4 | import android.view.View;
5 |
6 | @SuppressWarnings({ "unchecked" })
7 | public class ViewFindUtils
8 | {
9 | /**
10 | * ViewHolder简洁写法,避免适配器中重复定义ViewHolder,减少代码量 用法:
11 | *
12 | *
13 | * if (convertView == null)
14 | * {
15 | * convertView = View.inflate(context, R.layout.ad_demo, null);
16 | * }
17 | * TextView tv_demo = ViewHolderUtils.get(convertView, R.id.tv_demo);
18 | * ImageView iv_demo = ViewHolderUtils.get(convertView, R.id.iv_demo);
19 | *
20 | */
21 | public static T hold(View view, int id)
22 | {
23 | SparseArray viewHolder = (SparseArray) view.getTag();
24 |
25 | if (viewHolder == null)
26 | {
27 | viewHolder = new SparseArray();
28 | view.setTag(viewHolder);
29 | }
30 |
31 | View childView = viewHolder.get(id);
32 |
33 | if (childView == null)
34 | {
35 | childView = view.findViewById(id);
36 | viewHolder.put(id, childView);
37 | }
38 |
39 | return (T) childView;
40 | }
41 |
42 | /**
43 | * 替代findviewById方法
44 | */
45 | public static T find(View view, int id)
46 | {
47 | return (T) view.findViewById(id);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab_segment.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
22 |
23 |
24 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/ui/SimpleHomeActivity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.ui;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.graphics.Color;
6 | import android.os.Bundle;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.AdapterView;
10 | import android.widget.ListView;
11 |
12 | import com.flyco.tablayoutsamples.adapter.SimpleHomeAdapter;
13 |
14 | public class SimpleHomeActivity extends AppCompatActivity {
15 | private Context mContext = this;
16 | private final String[] mItems = {"SlidingTabLayout", "CommonTabLayout", "SegmentTabLayout"};
17 | private final Class>[] mClasses = {SlidingTabActivity.class, CommonTabActivity.class,
18 | SegmentTabActivity.class};
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | ListView lv = new ListView(mContext);
24 | lv.setCacheColorHint(Color.TRANSPARENT);
25 | lv.setFadingEdgeLength(0);
26 | lv.setAdapter(new SimpleHomeAdapter(mContext, mItems));
27 |
28 | lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
29 | @Override
30 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
31 | Intent intent = new Intent(mContext, mClasses[position]);
32 | startActivity(intent);
33 | }
34 | });
35 |
36 | setContentView(lv);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab_top.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
21 |
22 |
27 |
28 |
29 |
43 |
44 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab_left.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
21 |
22 |
27 |
28 |
29 |
43 |
44 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab_bottom.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
22 |
23 |
27 |
28 |
29 |
30 |
44 |
45 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/layout/layout_tab_right.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
22 |
23 |
27 |
28 |
29 |
30 |
44 |
45 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/utils/FragmentChangeManager.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout.utils;
2 |
3 | import android.support.v4.app.Fragment;
4 | import android.support.v4.app.FragmentManager;
5 | import android.support.v4.app.FragmentTransaction;
6 |
7 | import java.util.ArrayList;
8 |
9 | public class FragmentChangeManager {
10 | private FragmentManager mFragmentManager;
11 | private int mContainerViewId;
12 | /** Fragment切换数组 */
13 | private ArrayList mFragments;
14 | /** 当前选中的Tab */
15 | private int mCurrentTab;
16 |
17 | public FragmentChangeManager(FragmentManager fm, int containerViewId, ArrayList fragments) {
18 | this.mFragmentManager = fm;
19 | this.mContainerViewId = containerViewId;
20 | this.mFragments = fragments;
21 | initFragments();
22 | }
23 |
24 | /** 初始化fragments */
25 | private void initFragments() {
26 | for (Fragment fragment : mFragments) {
27 | mFragmentManager.beginTransaction().add(mContainerViewId, fragment).hide(fragment).commit();
28 | }
29 |
30 | setFragments(0);
31 | }
32 |
33 | /** 界面切换控制 */
34 | public void setFragments(int index) {
35 | for (int i = 0; i < mFragments.size(); i++) {
36 | FragmentTransaction ft = mFragmentManager.beginTransaction();
37 | Fragment fragment = mFragments.get(i);
38 | if (i == index) {
39 | ft.show(fragment);
40 | } else {
41 | ft.hide(fragment);
42 | }
43 | ft.commit();
44 | }
45 | mCurrentTab = index;
46 | }
47 |
48 | public int getCurrentTab() {
49 | return mCurrentTab;
50 | }
51 |
52 | public Fragment getCurrentFragment() {
53 | return mFragments.get(mCurrentTab);
54 | }
55 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/adapter/SimpleHomeAdapter.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.adapter;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.graphics.Color;
6 | import android.util.DisplayMetrics;
7 | import android.util.TypedValue;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.AbsListView;
11 | import android.widget.BaseAdapter;
12 | import android.widget.TextView;
13 |
14 | public class SimpleHomeAdapter extends BaseAdapter {
15 | private Context mContext;
16 | private String[] mItems;
17 | private DisplayMetrics mDisplayMetrics;
18 |
19 | public SimpleHomeAdapter(Context context, String[] items) {
20 | this.mContext = context;
21 | this.mItems = items;
22 | mDisplayMetrics = new DisplayMetrics();
23 | ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
24 | }
25 |
26 | @Override
27 | public int getCount() {
28 | return mItems.length;
29 | }
30 |
31 | @Override
32 | public Object getItem(int position) {
33 | return null;
34 | }
35 |
36 | @Override
37 | public long getItemId(int position) {
38 | return position;
39 | }
40 |
41 | @Override
42 | public View getView(int position, View convertView, ViewGroup parent) {
43 |
44 | int padding = (int) (mDisplayMetrics.density * 10);
45 |
46 | TextView tv = new TextView(mContext);
47 | tv.setText(mItems[position]);
48 | tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
49 | tv.setTextColor(Color.parseColor("#468ED0"));
50 | // tv.setGravity(Gravity.CENTER);
51 | tv.setPadding(padding, padding, padding, padding);
52 | AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
53 | AbsListView.LayoutParams.WRAP_CONTENT);
54 | tv.setLayoutParams(lp);
55 | return tv;
56 | }
57 | }
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/utils/UnreadMsgUtils.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout.utils;
2 |
3 |
4 | import android.util.DisplayMetrics;
5 | import android.view.View;
6 | import android.widget.RelativeLayout;
7 |
8 | import com.flyco.tablayout.widget.MsgView;
9 |
10 | /**
11 | * 未读消息提示View,显示小红点或者带有数字的红点:
12 | * 数字一位,圆
13 | * 数字两位,圆角矩形,圆角是高度的一半
14 | * 数字超过两位,显示99+
15 | */
16 | public class UnreadMsgUtils {
17 | public static void show(MsgView msgView, int num) {
18 | if (msgView == null) {
19 | return;
20 | }
21 | RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) msgView.getLayoutParams();
22 | DisplayMetrics dm = msgView.getResources().getDisplayMetrics();
23 | msgView.setVisibility(View.VISIBLE);
24 | if (num <= 0) {//圆点,设置默认宽高
25 | msgView.setStrokeWidth(0);
26 | msgView.setText("");
27 |
28 | lp.width = (int) (5 * dm.density);
29 | lp.height = (int) (5 * dm.density);
30 | msgView.setLayoutParams(lp);
31 | } else {
32 | lp.height = (int) (18 * dm.density);
33 | if (num > 0 && num < 10) {//圆
34 | lp.width = (int) (18 * dm.density);
35 | msgView.setText(num + "");
36 | } else if (num > 9 && num < 100) {//圆角矩形,圆角是高度的一半,设置默认padding
37 | lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
38 | msgView.setPadding((int) (6 * dm.density), 0, (int) (6 * dm.density), 0);
39 | msgView.setText(num + "");
40 | } else {//数字超过两位,显示99+
41 | lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
42 | msgView.setPadding((int) (6 * dm.density), 0, (int) (6 * dm.density), 0);
43 | msgView.setText("99+");
44 | }
45 | msgView.setLayoutParams(lp);
46 | }
47 | }
48 |
49 | public static void setSize(MsgView rtv, int size) {
50 | if (rtv == null) {
51 | return;
52 | }
53 | RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rtv.getLayoutParams();
54 | lp.width = size;
55 | lp.height = size;
56 | rtv.setLayoutParams(lp);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/CHNAGELOG.md:
--------------------------------------------------------------------------------
1 | #Change Log
2 | Version 1.1.2 *(2015-10-14)*
3 | ----------------------------
4 | * add method setViewPager(ViewPager vp, String[] titles) for the condition that you do not want set titles in page adapter
5 |
6 | Version 1.1.4 *(2015-10-16)*
7 | ----------------------------
8 | * fix bug: indicator not show if you do not call viewpager's setCurrentItem method during initialization.
9 |
10 | Version 1.1.6 *(2015-10-18)*
11 | ----------------------------
12 | * add method setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments)
13 | for the condition that you even do not want to instantiate page adapter by yourself
14 | * add listener OnTabSelectedListener
15 |
16 | Version 1.1.8 *(2015-10-19)*
17 | ----------------------------
18 | * add block indicator
19 |
20 | Version 1.2.0 *(2015-10-20)*
21 | ----------------------------
22 | * add unread msg dot (TipView)
23 |
24 | Version 1.3.0 *(2015-10-22)*
25 | ----------------------------
26 | * new added View: CommonTabLayout is a tablayout without dependence of ViewPager
27 |
28 | Version 1.3.2 *(2015-10-28)*
29 | ----------------------------
30 | * extract common attributes
31 |
32 | Version 1.3.4 *(2015-11-5)*
33 | ----------------------------
34 | * replace TipView with RoundTextView
35 |
36 | Version 1.3.6 *(2015-11-10)*
37 | ----------------------------
38 | * new add attr tl_indicator_width_equal_title
39 |
40 | Version 1.4.0 *(2015-11-11)*
41 | ----------------------------
42 | * fix bug: attr tl_indicator_width_equal_title sometime invalid
43 |
44 | Version 1.4.2 *(2015-12-9)*
45 | ----------------------------
46 | * new added tablayout
47 |
48 | Version 1.4.4 *(2015-12-11)*
49 | ----------------------------
50 | * fix bug: CommonTabLayout first setCurrentTab() cause indicator not show
51 |
52 | Version 1.4.6 *(2015-12-11)*
53 | ----------------------------
54 | * fix bug: CommonTabLayout first setCurrentTab() cause indicator not show
55 |
56 | Version 1.5.0 *(2015-12-11)*
57 | ----------------------------
58 | * change code style
59 |
60 | Version 2.0.0 *(2016.3.1)*
61 | ----------------------------
62 | * remove the dependence of FlycoRoundView
63 | * new added method getIconView and getTitleView
64 |
65 | Version 2.0.2 *(2016.4.23)*
66 | ----------------------------
67 | * remove the dependence of NineOldAnimation(only support 3.0+)
68 |
69 | Version 2.0.6 *(2016.5.21)*
70 | ----------------------------
71 | * remove CustomTabProvider in SlidingTabLayout
72 | * new added method 'addNewTab(String title)' for SlidingTabLayout
73 |
74 | Version 2.0.8 *(2016.7.26)*
75 | ---------------------------
76 | * Fix #27,#31(new added method 'setCurrentTab(int currentTab, boolean smoothScroll)' for SlidingTabLayout and redefine attr 'tl_textBold')
77 |
78 |
79 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_segment_tab.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
14 |
24 |
25 |
37 |
38 |
53 |
54 |
58 |
59 |
74 |
75 |
79 |
80 |
94 |
95 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | //apply plugin: 'com.github.dcendents.android-maven'
3 | //apply plugin: 'com.jfrog.bintray'
4 |
5 | // 这个version是区分library版本的,因此当我们需要更新library时记得修改这个version
6 | version = "2.0.8"
7 | android {
8 | compileSdkVersion 23
9 | buildToolsVersion "23.0.3"
10 |
11 | defaultConfig {
12 | minSdkVersion 11
13 | targetSdkVersion 23
14 | versionCode 208
15 | versionName version
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(dir: 'libs', include: ['*.jar'])
27 | compile 'com.android.support:support-v4:23.4.0'
28 | }
29 |
30 | //def siteUrl = 'https://github.com/H07000223' // 项目的主页
31 | //def gitUrl = 'https://github.com/H07000223' // Git仓库的url
32 | //group = "com.flyco.tablayout" // Maven Group ID for the artifact,一般填你唯一的包名
33 | //install {
34 | // repositories.mavenInstaller {
35 | // // This generates POM.xml with proper parameters
36 | // pom {
37 | // project {
38 | // packaging 'aar'
39 | // // Add your description here
40 | // name 'Android TabLayout Library' //项目描述
41 | // url siteUrl
42 | // // Set your license
43 | // licenses {
44 | // license {
45 | // name 'MIT'
46 | // url 'http://opensource.org/licenses/MIT'
47 | // }
48 | // }
49 | // developers {
50 | // developer {
51 | // id 'H07000223' //填写的一些基本信息
52 | // name 'H07000223'
53 | // email '867318349@qq.com'
54 | // }
55 | // }
56 | // scm {
57 | // connection gitUrl
58 | // developerConnection gitUrl
59 | // url siteUrl
60 | // }
61 | // }
62 | // }
63 | // }
64 | //}
65 | //
66 | //task sourcesJar(type: Jar) {
67 | // from android.sourceSets.main.java.srcDirs
68 | // classifier = 'sources'
69 | //}
70 | //
71 | //artifacts {
72 | // archives sourcesJar
73 | //}
74 | //
75 | //android.libraryVariants.all { variant ->
76 | // println variant.javaCompile.classpath.files
77 | // if (variant.name == 'release') { //我们只需 release 的 javadoc
78 | // task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
79 | // // title = ''
80 | // // description = ''
81 | // source = variant.javaCompile.source
82 | // classpath = files(variant.javaCompile.classpath.files, project.android.getBootClasspath())
83 | // options {
84 | // encoding "utf-8"
85 | // links "http://docs.oracle.com/javase/7/docs/api/"
86 | // linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
87 | // }
88 | // exclude '**/BuildConfig.java'
89 | // exclude '**/R.java'
90 | // }
91 | // task("javadoc${variant.name.capitalize()}Jar", type: Jar, dependsOn: "generate${variant.name.capitalize()}Javadoc") {
92 | // classifier = 'javadoc'
93 | // from tasks.getByName("generate${variant.name.capitalize()}Javadoc").destinationDir
94 | // }
95 | // artifacts {
96 | // archives tasks.getByName("javadoc${variant.name.capitalize()}Jar")
97 | // }
98 | // }
99 | //}
100 | //
101 | //Properties properties = new Properties()
102 | //properties.load(project.rootProject.file('local.properties').newDataInputStream())
103 | //bintray {
104 | // user = properties.getProperty("bintray.user")
105 | // key = properties.getProperty("bintray.apikey")
106 | // configurations = ['archives']
107 | // pkg {
108 | // repo = "maven"
109 | // name = "FlycoTabLayout_Lib" //发布到JCenter上的项目名字
110 | // websiteUrl = siteUrl
111 | // vcsUrl = gitUrl
112 | // licenses = ["MIT"]
113 | // publish = true
114 | // }
115 | //}
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/ui/SegmentTabActivity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.ui;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.support.v4.app.Fragment;
6 | import android.support.v4.app.FragmentManager;
7 | import android.support.v4.app.FragmentPagerAdapter;
8 | import android.support.v4.view.ViewPager;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.view.View;
11 |
12 | import com.flyco.tablayout.SegmentTabLayout;
13 | import com.flyco.tablayout.listener.OnTabSelectListener;
14 | import com.flyco.tablayout.widget.MsgView;
15 | import com.flyco.tablayoutsamples.R;
16 | import com.flyco.tablayoutsamples.utils.ViewFindUtils;
17 |
18 | import java.util.ArrayList;
19 |
20 | public class SegmentTabActivity extends AppCompatActivity {
21 | private ArrayList mFragments = new ArrayList<>();
22 | private ArrayList mFragments2 = new ArrayList<>();
23 |
24 | private String[] mTitles = {"首页", "消息"};
25 | private String[] mTitles_2 = {"首页", "消息", "联系人"};
26 | private String[] mTitles_3 = {"首页", "消息", "联系人", "更多"};
27 | private View mDecorView;
28 | private SegmentTabLayout mTabLayout_3;
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_segment_tab);
34 |
35 | for (String title : mTitles_3) {
36 | mFragments.add(SimpleCardFragment.getInstance("Switch ViewPager " + title));
37 | }
38 |
39 | for (String title : mTitles_2) {
40 | mFragments2.add(SimpleCardFragment.getInstance("Switch Fragment " + title));
41 | }
42 |
43 | mDecorView = getWindow().getDecorView();
44 |
45 | SegmentTabLayout tabLayout_1 = ViewFindUtils.find(mDecorView, R.id.tl_1);
46 | SegmentTabLayout tabLayout_2 = ViewFindUtils.find(mDecorView, R.id.tl_2);
47 | mTabLayout_3 = ViewFindUtils.find(mDecorView, R.id.tl_3);
48 | SegmentTabLayout tabLayout_4 = ViewFindUtils.find(mDecorView, R.id.tl_4);
49 | SegmentTabLayout tabLayout_5 = ViewFindUtils.find(mDecorView, R.id.tl_5);
50 |
51 | tabLayout_1.setTabData(mTitles);
52 | tabLayout_2.setTabData(mTitles_2);
53 | tl_3();
54 | tabLayout_4.setTabData(mTitles_2, this, R.id.fl_change, mFragments2);
55 | tabLayout_5.setTabData(mTitles_3);
56 |
57 | //显示未读红点
58 | tabLayout_1.showDot(2);
59 | tabLayout_2.showDot(2);
60 | mTabLayout_3.showDot(1);
61 | tabLayout_4.showDot(1);
62 |
63 | //设置未读消息红点
64 | mTabLayout_3.showDot(2);
65 | MsgView rtv_3_2 = mTabLayout_3.getMsgView(2);
66 | if (rtv_3_2 != null) {
67 | rtv_3_2.setBackgroundColor(Color.parseColor("#6D8FB0"));
68 | }
69 | }
70 |
71 | private void tl_3() {
72 | final ViewPager vp_3 = ViewFindUtils.find(mDecorView, R.id.vp_2);
73 | vp_3.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
74 |
75 | mTabLayout_3.setTabData(mTitles_3);
76 | mTabLayout_3.setOnTabSelectListener(new OnTabSelectListener() {
77 | @Override
78 | public void onTabSelect(int position) {
79 | vp_3.setCurrentItem(position);
80 | }
81 |
82 | @Override
83 | public void onTabReselect(int position) {
84 | }
85 | });
86 |
87 | vp_3.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
88 | @Override
89 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
90 |
91 | }
92 |
93 | @Override
94 | public void onPageSelected(int position) {
95 | mTabLayout_3.setCurrentTab(position);
96 | }
97 |
98 | @Override
99 | public void onPageScrollStateChanged(int state) {
100 |
101 | }
102 | });
103 | vp_3.setCurrentItem(1);
104 | }
105 |
106 | private class MyPagerAdapter extends FragmentPagerAdapter {
107 | public MyPagerAdapter(FragmentManager fm) {
108 | super(fm);
109 | }
110 |
111 | @Override
112 | public int getCount() {
113 | return mFragments.size();
114 | }
115 |
116 | @Override
117 | public CharSequence getPageTitle(int position) {
118 | return mTitles_3[position];
119 | }
120 |
121 | @Override
122 | public Fragment getItem(int position) {
123 | return mFragments.get(position);
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/README_CN.md:
--------------------------------------------------------------------------------
1 | #FlycoTabLayout
2 | [](https://android-arsenal.com/details/1/2756)
3 |
4 | 一个Android TabLayout库,目前有3个TabLayout
5 |
6 | * SlidingTabLayout:参照[PagerSlidingTabStrip](https://github.com/jpardogo/PagerSlidingTabStrip)进行大量修改.
7 | * 新增部分属性
8 | * 新增支持多种Indicator显示器
9 | * 新增支持未读消息显示
10 | * 新增方法for懒癌患者
11 |
12 | ```java
13 | /** 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 */
14 | public void setViewPager(ViewPager vp, String[] titles)
15 |
16 | /** 关联ViewPager,用于连适配器都不想自己实例化的情况 */
17 | public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments)
18 | ```
19 |
20 | * CommonTabLayout:不同于SlidingTabLayout对ViewPager依赖,它是一个不依赖ViewPager可以与其他控件自由搭配使用的TabLayout.
21 | * 支持多种Indicator显示器,以及Indicator动画
22 | * 支持未读消息显示
23 | * 支持Icon以及Icon位置
24 | * 新增方法for懒癌患者
25 |
26 | ```java
27 | /** 关联数据支持同时切换fragments */
28 | public void setTabData(ArrayList tabEntitys, FragmentManager fm, int containerViewId, ArrayList fragments)
29 | ```
30 |
31 | * SegmentTabLayout
32 |
33 | ##Demo
34 | 
35 |
36 | 
37 |
38 | 
39 |
40 |
41 | >##Change Log
42 |
43 | > v2.0.0(2016-03-01)
44 | - 删除了对FlycoRoundView库的依赖
45 | - 新增方法getIconView和getTitleView(为了某些情况需要动态更新icon之类的)
46 |
47 | > v2.0.2(2016-04-23)
48 | - 删除了对NineOldAnimation库依赖(仅支持3.0+)
49 |
50 | ##Gradle
51 |
52 | ```groovy
53 | dependencies{
54 | compile 'com.android.support:support-v4:23.1.1'
55 | compile 'com.nineoldandroids:library:2.4.0'
56 | compile 'com.flyco.roundview:FlycoRoundView_Lib:1.1.2@aar'
57 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:1.5.0@aar'
58 | }
59 |
60 | After v2.0.0(support 2.2+)
61 | dependencies{
62 | compile 'com.android.support:support-v4:23.1.1'
63 | compile 'com.nineoldandroids:library:2.4.0'
64 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'
65 | }
66 |
67 | After v2.0.2(support 3.0+)
68 | dependencies{
69 | compile 'com.android.support:support-v4:23.1.1'
70 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.8@aar'
71 | }
72 | ```
73 |
74 | ##Attributes
75 |
76 | |name|format|description|
77 | |:---:|:---:|:---:|
78 | | tl_indicator_color | color |设置显示器颜色
79 | | tl_indicator_height | dimension |设置显示器高度
80 | | tl_indicator_width | dimension |设置显示器固定宽度
81 | | tl_indicator_margin_left | dimension |设置显示器margin,当indicator_width大于0,无效
82 | | tl_indicator_margin_top | dimension |设置显示器margin,当indicator_width大于0,无效
83 | | tl_indicator_margin_right | dimension |设置显示器margin,当indicator_width大于0,无效
84 | | tl_indicator_margin_bottom | dimension |设置显示器margin,当indicator_width大于0,无效
85 | | tl_indicator_corner_radius | dimension |设置显示器圆角弧度
86 | | tl_indicator_gravity | enum |设置显示器上方(TOP)还是下方(BOTTOM),只对常规显示器有用
87 | | tl_indicator_style | enum |设置显示器为常规(NORMAL)或三角形(TRIANGLE)或背景色块(BLOCK)
88 | | tl_underline_color | color |设置下划线颜色
89 | | tl_underline_height | dimension |设置下划线高度
90 | | tl_underline_gravity | enum |设置下划线上方(TOP)还是下方(BOTTOM)
91 | | tl_divider_color | color |设置分割线颜色
92 | | tl_divider_width | dimension |设置分割线宽度
93 | | tl_divider_padding |dimension| 设置分割线的paddingTop和paddingBottom
94 | | tl_tab_padding |dimension| 设置tab的paddingLeft和paddingRight
95 | | tl_tab_space_equal |boolean| 设置tab大小等分
96 | | tl_tab_width |dimension| 设置tab固定大小
97 | | tl_textsize |dimension| 设置字体大小
98 | | tl_textSelectColor |color| 设置字体选中颜色
99 | | tl_textUnselectColor |color| 设置字体未选中颜色
100 | | tl_textBold |boolean| 设置字体加粗
101 | | tl_iconWidth |dimension| 设置icon宽度(仅支持CommonTabLayout)
102 | | tl_iconHeight |dimension|设置icon高度(仅支持CommonTabLayout)
103 | | tl_iconVisible |boolean| 设置icon是否可见(仅支持CommonTabLayout)
104 | | tl_iconGravity |enum| 设置icon显示位置,对应Gravity中常量值,左上右下(仅支持CommonTabLayout)
105 | | tl_iconMargin |dimension| 设置icon与文字间距(仅支持CommonTabLayout)
106 | | tl_indicator_anim_enable |boolean| 设置显示器支持动画(only for CommonTabLayout)
107 | | tl_indicator_anim_duration |integer| 设置显示器动画时间(only for CommonTabLayout)
108 | | tl_indicator_bounce_enable |boolean| 设置显示器支持动画回弹效果(only for CommonTabLayout)
109 | | tl_indicator_width_equal_title |boolean| 设置显示器与标题一样长(only for SlidingTabLayout)
110 |
111 | ##Dependence
112 | * [NineOldAndroids](https://github.com/JakeWharton/NineOldAndroids)
113 | * [FlycoRoundView](https://github.com/H07000223/FlycoRoundView)
114 |
115 | ##Thanks
116 | * [PagerSlidingTabStrip](https://github.com/jpardogo/PagerSlidingTabStrip)
117 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_common_tab.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
14 |
26 |
27 |
31 |
32 |
46 |
47 |
51 |
52 |
53 |
71 |
72 |
81 |
82 |
93 |
94 |
104 |
105 |
117 |
118 |
130 |
131 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #FlycoTabLayout
2 | [](https://android-arsenal.com/details/1/2756)
3 | ####[中文版](https://github.com/H07000223/FlycoTabLayout/blob/master/README_CN.md)
4 | An Android TabLayout Lib has 3 kinds of TabLayout at present.
5 |
6 | * SlidingTabLayout: deeply modified from [PagerSlidingTabStrip](https://github.com/jpardogo/PagerSlidingTabStrip).
7 | * new added attribute
8 | * new added kinds of indicators
9 | * new added unread msg tip
10 | * new added method for convenience
11 |
12 | ```java
13 | /** no need to set titles in adapter */
14 | public void setViewPager(ViewPager vp, String[] titles)
15 |
16 | /** no need to initialize even adapter */
17 | public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments)
18 | ```
19 |
20 | * CommonTabLayout:unlike SlidingTabLayout's dependence on ViewPager,it is a tabLayout without dependence on ViewPager and
21 | can be used freely with other widgets together.
22 | * support kinds of indicators and indicator animation
23 | * support unread msg tip
24 | * support icon and icon gravity.
25 | * new added method for convenience
26 |
27 | ```java
28 | /** support switch fragments itself */
29 | public void setTabData(ArrayList tabEntitys, FragmentManager fm, int containerViewId, ArrayList fragments)
30 | ```
31 |
32 | * SegmentTabLayout
33 |
34 | ##Demo
35 | 
36 |
37 | 
38 |
39 | 
40 |
41 |
42 | >##Change Log
43 |
44 | > v2.0.0(2016-03-01)
45 | - remove the dependence of FlycoRoundView
46 | - new added method getIconView and getTitleView
47 |
48 | > v2.0.2(2016-04-23)
49 | - remove the dependence of NineOldAnimation(only support 3.0+)
50 |
51 |
52 | ##Gradle
53 |
54 | ```groovy
55 | dependencies{
56 | compile 'com.android.support:support-v4:23.1.1'
57 | compile 'com.nineoldandroids:library:2.4.0'
58 | compile 'com.flyco.roundview:FlycoRoundView_Lib:1.1.2@aar'
59 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:1.5.0@aar'
60 | }
61 |
62 | After v2.0.0
63 | dependencies{
64 | compile 'com.android.support:support-v4:23.1.1'
65 | compile 'com.nineoldandroids:library:2.4.0'
66 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'
67 | }
68 |
69 | After v2.0.2(support 3.0+)
70 | dependencies{
71 | compile 'com.android.support:support-v4:23.1.1'
72 | compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.8@aar'
73 | }
74 | ```
75 |
76 | ##Attributes
77 |
78 | |name|format|description|
79 | |:---:|:---:|:---:|
80 | | tl_indicator_color | color |set indicator color
81 | | tl_indicator_height | dimension |set indicator height
82 | | tl_indicator_width | dimension |set indicator width
83 | | tl_indicator_margin_left | dimension |set indicator margin,invalid when indicator width is greater than 0.
84 | | tl_indicator_margin_top | dimension |set indicator margin,invalid when indicator width is greater than 0.
85 | | tl_indicator_margin_right | dimension |set indicator margin,invalid when indicator width is greater than 0.
86 | | tl_indicator_margin_bottom | dimension |set indicator margin,invalid when indicator width is greater than 0.
87 | | tl_indicator_corner_radius | dimension |set indicator corner radius
88 | | tl_indicator_gravity | enum |set indicator gravity TOP or BOTTOM.
89 | | tl_indicator_style | enum |set indicator style NORMAL or TRIANGLE or BLOCK
90 | | tl_underline_color | color |set underline color
91 | | tl_underline_height | dimension |set underline height
92 | | tl_underline_gravity | enum |set underline gravity TOP or BOTTOM
93 | | tl_divider_color | color |set divider color
94 | | tl_divider_width | dimension |set divider width
95 | | tl_divider_padding |dimension| set divider paddingTop and paddingBottom
96 | | tl_tab_padding |dimension| set tab paddingLeft and paddingRight
97 | | tl_tab_space_equal |boolean| set tab space equal
98 | | tl_tab_width |dimension| set tab width
99 | | tl_textsize |dimension| set text size
100 | | tl_textSelectColor |color| set text select color
101 | | tl_textUnselectColor |color| set text unselect color
102 | | tl_textBold |boolean| set text is bold
103 | | tl_iconWidth |dimension| set icon width(only for CommonTabLayout)
104 | | tl_iconHeight |dimension|set icon height(only for CommonTabLayout)
105 | | tl_iconVisible |boolean| set icon is visible(only for CommonTabLayout)
106 | | tl_iconGravity |enum| set icon gravity LEFT or TOP or RIGHT or BOTTOM(only for CommonTabLayout)
107 | | tl_iconMargin |dimension| set icon margin with text(only for CommonTabLayout)
108 | | tl_indicator_anim_enable |boolean| set indicator support animation(only for CommonTabLayout)
109 | | tl_indicator_anim_duration |integer| set indicator animation duration(only for CommonTabLayout)
110 | | tl_indicator_bounce_enable |boolean| set indicator aniamtion with bounce effect(only for CommonTabLayout)
111 | | tl_indicator_width_equal_title |boolean| set indicator width same as text(only for SlidingTabLayout)
112 |
113 | ##Dependence
114 | * [NineOldAndroids](https://github.com/JakeWharton/NineOldAndroids)
115 | * [FlycoRoundView](https://github.com/H07000223/FlycoRoundView)
116 |
117 | ##Thanks
118 | * [PagerSlidingTabStrip](https://github.com/jpardogo/PagerSlidingTabStrip)
119 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/ui/SlidingTabActivity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.ui;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v4.app.FragmentManager;
8 | import android.support.v4.app.FragmentPagerAdapter;
9 | import android.support.v4.view.ViewPager;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.view.View;
12 | import android.widget.Toast;
13 |
14 | import com.flyco.tablayout.SlidingTabLayout;
15 | import com.flyco.tablayout.listener.OnTabSelectListener;
16 | import com.flyco.tablayout.widget.MsgView;
17 | import com.flyco.tablayoutsamples.R;
18 | import com.flyco.tablayoutsamples.utils.ViewFindUtils;
19 |
20 | import java.util.ArrayList;
21 |
22 | public class SlidingTabActivity extends AppCompatActivity implements OnTabSelectListener {
23 | private Context mContext = this;
24 | private ArrayList mFragments = new ArrayList<>();
25 | private final String[] mTitles = {
26 | "热门", "iOS", "Android"
27 | , "前端", "后端", "设计", "工具资源"
28 | };
29 | private MyPagerAdapter mAdapter;
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_sliding_tab);
35 |
36 | for (String title : mTitles) {
37 | mFragments.add(SimpleCardFragment.getInstance(title));
38 | }
39 |
40 |
41 | View decorView = getWindow().getDecorView();
42 | ViewPager vp = ViewFindUtils.find(decorView, R.id.vp);
43 | mAdapter = new MyPagerAdapter(getSupportFragmentManager());
44 | vp.setAdapter(mAdapter);
45 |
46 | /** 默认 */
47 | SlidingTabLayout tabLayout_1 = ViewFindUtils.find(decorView, R.id.tl_1);
48 | /**自定义部分属性*/
49 | SlidingTabLayout tabLayout_2 = ViewFindUtils.find(decorView, R.id.tl_2);
50 | /** 字体加粗,大写 */
51 | SlidingTabLayout tabLayout_3 = ViewFindUtils.find(decorView, R.id.tl_3);
52 | /** tab固定宽度 */
53 | SlidingTabLayout tabLayout_4 = ViewFindUtils.find(decorView, R.id.tl_4);
54 | /** indicator固定宽度 */
55 | SlidingTabLayout tabLayout_5 = ViewFindUtils.find(decorView, R.id.tl_5);
56 | /** indicator圆 */
57 | SlidingTabLayout tabLayout_6 = ViewFindUtils.find(decorView, R.id.tl_6);
58 | /** indicator矩形圆角 */
59 | final SlidingTabLayout tabLayout_7 = ViewFindUtils.find(decorView, R.id.tl_7);
60 | /** indicator三角形 */
61 | SlidingTabLayout tabLayout_8 = ViewFindUtils.find(decorView, R.id.tl_8);
62 | /** indicator圆角色块 */
63 | SlidingTabLayout tabLayout_9 = ViewFindUtils.find(decorView, R.id.tl_9);
64 | /** indicator圆角色块 */
65 | SlidingTabLayout tabLayout_10 = ViewFindUtils.find(decorView, R.id.tl_10);
66 |
67 | tabLayout_1.setViewPager(vp);
68 | tabLayout_2.setViewPager(vp);
69 | tabLayout_2.setOnTabSelectListener(this);
70 | tabLayout_3.setViewPager(vp);
71 | tabLayout_4.setViewPager(vp);
72 | tabLayout_5.setViewPager(vp);
73 | tabLayout_6.setViewPager(vp);
74 | tabLayout_7.setViewPager(vp, mTitles);
75 | tabLayout_8.setViewPager(vp, mTitles, this, mFragments);
76 | tabLayout_9.setViewPager(vp);
77 | tabLayout_10.setViewPager(vp);
78 |
79 | vp.setCurrentItem(4);
80 |
81 | tabLayout_1.showDot(4);
82 | tabLayout_3.showDot(4);
83 | tabLayout_2.showDot(4);
84 |
85 | tabLayout_2.showMsg(3, 5);
86 | tabLayout_2.setMsgMargin(3, 0, 10);
87 | MsgView rtv_2_3 = tabLayout_2.getMsgView(3);
88 | if (rtv_2_3 != null) {
89 | rtv_2_3.setBackgroundColor(Color.parseColor("#6D8FB0"));
90 | }
91 |
92 | tabLayout_2.showMsg(5, 5);
93 | tabLayout_2.setMsgMargin(5, 0, 10);
94 |
95 | // tabLayout_7.setOnTabSelectListener(new OnTabSelectListener() {
96 | // @Override
97 | // public void onTabSelect(int position) {
98 | // Toast.makeText(mContext, "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
99 | // }
100 | //
101 | // @Override
102 | // public void onTabReselect(int position) {
103 | // mFragments.add(SimpleCardFragment.getInstance("后端"));
104 | // mAdapter.notifyDataSetChanged();
105 | // tabLayout_7.addNewTab("后端");
106 | // }
107 | // });
108 | }
109 |
110 | @Override
111 | public void onTabSelect(int position) {
112 | Toast.makeText(mContext, "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
113 | }
114 |
115 | @Override
116 | public void onTabReselect(int position) {
117 | Toast.makeText(mContext, "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
118 | }
119 |
120 | private class MyPagerAdapter extends FragmentPagerAdapter {
121 | public MyPagerAdapter(FragmentManager fm) {
122 | super(fm);
123 | }
124 |
125 | @Override
126 | public int getCount() {
127 | return mFragments.size();
128 | }
129 |
130 | @Override
131 | public CharSequence getPageTitle(int position) {
132 | return mTitles[position];
133 | }
134 |
135 | @Override
136 | public Fragment getItem(int position) {
137 | return mFragments.get(position);
138 | }
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/widget/MsgView.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout.widget;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Color;
6 | import android.graphics.drawable.GradientDrawable;
7 | import android.graphics.drawable.StateListDrawable;
8 | import android.os.Build;
9 | import android.util.AttributeSet;
10 | import android.widget.TextView;
11 |
12 | import com.flyco.tablayout.R;
13 |
14 | /** 用于需要圆角矩形框背景的TextView的情况,减少直接使用TextView时引入的shape资源文件 */
15 | public class MsgView extends TextView {
16 | private Context context;
17 | private GradientDrawable gd_background = new GradientDrawable();
18 | private int backgroundColor;
19 | private int cornerRadius;
20 | private int strokeWidth;
21 | private int strokeColor;
22 | private boolean isRadiusHalfHeight;
23 | private boolean isWidthHeightEqual;
24 |
25 | public MsgView(Context context) {
26 | this(context, null);
27 | }
28 |
29 | public MsgView(Context context, AttributeSet attrs) {
30 | this(context, attrs, 0);
31 | }
32 |
33 | public MsgView(Context context, AttributeSet attrs, int defStyleAttr) {
34 | super(context, attrs, defStyleAttr);
35 | this.context = context;
36 | obtainAttributes(context, attrs);
37 | }
38 |
39 | private void obtainAttributes(Context context, AttributeSet attrs) {
40 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MsgView);
41 | backgroundColor = ta.getColor(R.styleable.MsgView_mv_backgroundColor, Color.TRANSPARENT);
42 | cornerRadius = ta.getDimensionPixelSize(R.styleable.MsgView_mv_cornerRadius, 0);
43 | strokeWidth = ta.getDimensionPixelSize(R.styleable.MsgView_mv_strokeWidth, 0);
44 | strokeColor = ta.getColor(R.styleable.MsgView_mv_strokeColor, Color.TRANSPARENT);
45 | isRadiusHalfHeight = ta.getBoolean(R.styleable.MsgView_mv_isRadiusHalfHeight, false);
46 | isWidthHeightEqual = ta.getBoolean(R.styleable.MsgView_mv_isWidthHeightEqual, false);
47 |
48 | ta.recycle();
49 | }
50 |
51 | @Override
52 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53 | if (isWidthHeightEqual() && getWidth() > 0 && getHeight() > 0) {
54 | int max = Math.max(getWidth(), getHeight());
55 | int measureSpec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
56 | super.onMeasure(measureSpec, measureSpec);
57 | return;
58 | }
59 |
60 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
61 | }
62 |
63 | @Override
64 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
65 | super.onLayout(changed, left, top, right, bottom);
66 | if (isRadiusHalfHeight()) {
67 | setCornerRadius(getHeight() / 2);
68 | } else {
69 | setBgSelector();
70 | }
71 | }
72 |
73 |
74 | public void setBackgroundColor(int backgroundColor) {
75 | this.backgroundColor = backgroundColor;
76 | setBgSelector();
77 | }
78 |
79 | public void setCornerRadius(int cornerRadius) {
80 | this.cornerRadius = dp2px(cornerRadius);
81 | setBgSelector();
82 | }
83 |
84 | public void setStrokeWidth(int strokeWidth) {
85 | this.strokeWidth = dp2px(strokeWidth);
86 | setBgSelector();
87 | }
88 |
89 | public void setStrokeColor(int strokeColor) {
90 | this.strokeColor = strokeColor;
91 | setBgSelector();
92 | }
93 |
94 | public void setIsRadiusHalfHeight(boolean isRadiusHalfHeight) {
95 | this.isRadiusHalfHeight = isRadiusHalfHeight;
96 | setBgSelector();
97 | }
98 |
99 | public void setIsWidthHeightEqual(boolean isWidthHeightEqual) {
100 | this.isWidthHeightEqual = isWidthHeightEqual;
101 | setBgSelector();
102 | }
103 |
104 | public int getBackgroundColor() {
105 | return backgroundColor;
106 | }
107 |
108 | public int getCornerRadius() {
109 | return cornerRadius;
110 | }
111 |
112 | public int getStrokeWidth() {
113 | return strokeWidth;
114 | }
115 |
116 | public int getStrokeColor() {
117 | return strokeColor;
118 | }
119 |
120 | public boolean isRadiusHalfHeight() {
121 | return isRadiusHalfHeight;
122 | }
123 |
124 | public boolean isWidthHeightEqual() {
125 | return isWidthHeightEqual;
126 | }
127 |
128 | protected int dp2px(float dp) {
129 | final float scale = context.getResources().getDisplayMetrics().density;
130 | return (int) (dp * scale + 0.5f);
131 | }
132 |
133 | protected int sp2px(float sp) {
134 | final float scale = this.context.getResources().getDisplayMetrics().scaledDensity;
135 | return (int) (sp * scale + 0.5f);
136 | }
137 |
138 | private void setDrawable(GradientDrawable gd, int color, int strokeColor) {
139 | gd.setColor(color);
140 | gd.setCornerRadius(cornerRadius);
141 | gd.setStroke(strokeWidth, strokeColor);
142 | }
143 |
144 | public void setBgSelector() {
145 | StateListDrawable bg = new StateListDrawable();
146 |
147 | setDrawable(gd_background, backgroundColor, strokeColor);
148 | bg.addState(new int[]{-android.R.attr.state_pressed}, gd_background);
149 |
150 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//16
151 | setBackground(bg);
152 | } else {
153 | //noinspection deprecation
154 | setBackgroundDrawable(bg);
155 | }
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_sliding_tab.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
14 |
18 |
19 |
31 |
32 |
49 |
50 |
58 |
59 |
67 |
68 |
74 |
75 |
83 |
84 |
92 |
93 |
100 |
101 |
111 |
112 |
124 |
125 |
126 |
127 |
131 |
132 |
133 |
--------------------------------------------------------------------------------
/app/src/main/java/com/flyco/tablayoutsamples/ui/CommonTabActivity.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayoutsamples.ui;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v4.app.FragmentManager;
8 | import android.support.v4.app.FragmentPagerAdapter;
9 | import android.support.v4.view.ViewPager;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.view.View;
12 |
13 | import com.flyco.tablayout.CommonTabLayout;
14 | import com.flyco.tablayout.listener.CustomTabEntity;
15 | import com.flyco.tablayout.listener.OnTabSelectListener;
16 | import com.flyco.tablayout.utils.UnreadMsgUtils;
17 | import com.flyco.tablayout.widget.MsgView;
18 | import com.flyco.tablayoutsamples.R;
19 | import com.flyco.tablayoutsamples.entity.TabEntity;
20 | import com.flyco.tablayoutsamples.utils.ViewFindUtils;
21 |
22 | import java.util.ArrayList;
23 | import java.util.Random;
24 |
25 | public class CommonTabActivity extends AppCompatActivity {
26 | private Context mContext = this;
27 | private ArrayList mFragments = new ArrayList<>();
28 | private ArrayList mFragments2 = new ArrayList<>();
29 |
30 | private String[] mTitles = {"首页", "消息", "联系人", "更多"};
31 | private int[] mIconUnselectIds = {
32 | R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
33 | R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
34 | private int[] mIconSelectIds = {
35 | R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
36 | R.mipmap.tab_contact_select, R.mipmap.tab_more_select};
37 | private ArrayList mTabEntities = new ArrayList<>();
38 | private View mDecorView;
39 | private ViewPager mViewPager;
40 | private CommonTabLayout mTabLayout_1;
41 | private CommonTabLayout mTabLayout_2;
42 | private CommonTabLayout mTabLayout_3;
43 | private CommonTabLayout mTabLayout_4;
44 | private CommonTabLayout mTabLayout_5;
45 | private CommonTabLayout mTabLayout_6;
46 | private CommonTabLayout mTabLayout_7;
47 | private CommonTabLayout mTabLayout_8;
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 | setContentView(R.layout.activity_common_tab);
53 |
54 | for (String title : mTitles) {
55 | mFragments.add(SimpleCardFragment.getInstance("Switch ViewPager " + title));
56 | mFragments2.add(SimpleCardFragment.getInstance("Switch Fragment " + title));
57 | }
58 |
59 |
60 | for (int i = 0; i < mTitles.length; i++) {
61 | mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
62 | }
63 |
64 | mDecorView = getWindow().getDecorView();
65 | mViewPager = ViewFindUtils.find(mDecorView, R.id.vp_2);
66 | mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
67 | /** with nothing */
68 | mTabLayout_1 = ViewFindUtils.find(mDecorView, R.id.tl_1);
69 | /** with ViewPager */
70 | mTabLayout_2 = ViewFindUtils.find(mDecorView, R.id.tl_2);
71 | /** with Fragments */
72 | mTabLayout_3 = ViewFindUtils.find(mDecorView, R.id.tl_3);
73 | /** indicator固定宽度 */
74 | mTabLayout_4 = ViewFindUtils.find(mDecorView, R.id.tl_4);
75 | /** indicator固定宽度 */
76 | mTabLayout_5 = ViewFindUtils.find(mDecorView, R.id.tl_5);
77 | /** indicator矩形圆角 */
78 | mTabLayout_6 = ViewFindUtils.find(mDecorView, R.id.tl_6);
79 | /** indicator三角形 */
80 | mTabLayout_7 = ViewFindUtils.find(mDecorView, R.id.tl_7);
81 | /** indicator圆角色块 */
82 | mTabLayout_8 = ViewFindUtils.find(mDecorView, R.id.tl_8);
83 |
84 | mTabLayout_1.setTabData(mTabEntities);
85 | tl_2();
86 | mTabLayout_3.setTabData(mTabEntities, this, R.id.fl_change, mFragments2);
87 | mTabLayout_4.setTabData(mTabEntities);
88 | mTabLayout_5.setTabData(mTabEntities);
89 | mTabLayout_6.setTabData(mTabEntities);
90 | mTabLayout_7.setTabData(mTabEntities);
91 | mTabLayout_8.setTabData(mTabEntities);
92 |
93 | mTabLayout_3.setOnTabSelectListener(new OnTabSelectListener() {
94 | @Override
95 | public void onTabSelect(int position) {
96 | mTabLayout_1.setCurrentTab(position);
97 | mTabLayout_2.setCurrentTab(position);
98 | mTabLayout_4.setCurrentTab(position);
99 | mTabLayout_5.setCurrentTab(position);
100 | mTabLayout_6.setCurrentTab(position);
101 | mTabLayout_7.setCurrentTab(position);
102 | mTabLayout_8.setCurrentTab(position);
103 | }
104 |
105 | @Override
106 | public void onTabReselect(int position) {
107 |
108 | }
109 | });
110 | mTabLayout_8.setCurrentTab(2);
111 | mTabLayout_3.setCurrentTab(1);
112 |
113 | //显示未读红点
114 | mTabLayout_1.showDot(2);
115 | mTabLayout_3.showDot(1);
116 | mTabLayout_4.showDot(1);
117 |
118 | //两位数
119 | mTabLayout_2.showMsg(0, 55);
120 | mTabLayout_2.setMsgMargin(0, -5, 5);
121 |
122 | //三位数
123 | mTabLayout_2.showMsg(1, 100);
124 | mTabLayout_2.setMsgMargin(1, -5, 5);
125 |
126 | //设置未读消息红点
127 | mTabLayout_2.showDot(2);
128 | MsgView rtv_2_2 = mTabLayout_2.getMsgView(2);
129 | if (rtv_2_2 != null) {
130 | UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f));
131 | }
132 |
133 | //设置未读消息背景
134 | mTabLayout_2.showMsg(3, 5);
135 | mTabLayout_2.setMsgMargin(3, 0, 5);
136 | MsgView rtv_2_3 = mTabLayout_2.getMsgView(3);
137 | if (rtv_2_3 != null) {
138 | rtv_2_3.setBackgroundColor(Color.parseColor("#6D8FB0"));
139 | }
140 | }
141 |
142 | Random mRandom = new Random();
143 |
144 | private void tl_2() {
145 | mTabLayout_2.setTabData(mTabEntities);
146 | mTabLayout_2.setOnTabSelectListener(new OnTabSelectListener() {
147 | @Override
148 | public void onTabSelect(int position) {
149 | mViewPager.setCurrentItem(position);
150 | }
151 |
152 | @Override
153 | public void onTabReselect(int position) {
154 | if (position == 0) {
155 | mTabLayout_2.showMsg(0, mRandom.nextInt(100) + 1);
156 | // UnreadMsgUtils.show(mTabLayout_2.getMsgView(0), mRandom.nextInt(100) + 1);
157 | }
158 | }
159 | });
160 |
161 | mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
162 | @Override
163 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
164 |
165 | }
166 |
167 | @Override
168 | public void onPageSelected(int position) {
169 | mTabLayout_2.setCurrentTab(position);
170 | }
171 |
172 | @Override
173 | public void onPageScrollStateChanged(int state) {
174 |
175 | }
176 | });
177 |
178 | mViewPager.setCurrentItem(1);
179 | }
180 |
181 | private class MyPagerAdapter extends FragmentPagerAdapter {
182 | public MyPagerAdapter(FragmentManager fm) {
183 | super(fm);
184 | }
185 |
186 | @Override
187 | public int getCount() {
188 | return mFragments.size();
189 | }
190 |
191 | @Override
192 | public CharSequence getPageTitle(int position) {
193 | return mTitles[position];
194 | }
195 |
196 | @Override
197 | public Fragment getItem(int position) {
198 | return mFragments.get(position);
199 | }
200 | }
201 |
202 | protected int dp2px(float dp) {
203 | final float scale = mContext.getResources().getDisplayMetrics().density;
204 | return (int) (dp * scale + 0.5f);
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SegmentTabLayout.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout;
2 |
3 | import android.animation.TypeEvaluator;
4 | import android.animation.ValueAnimator;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.Paint;
10 | import android.graphics.Rect;
11 | import android.graphics.drawable.GradientDrawable;
12 | import android.os.Bundle;
13 | import android.os.Parcelable;
14 | import android.support.v4.app.Fragment;
15 | import android.support.v4.app.FragmentActivity;
16 | import android.util.AttributeSet;
17 | import android.util.SparseArray;
18 | import android.util.TypedValue;
19 | import android.view.View;
20 | import android.view.ViewGroup;
21 | import android.view.animation.OvershootInterpolator;
22 | import android.widget.FrameLayout;
23 | import android.widget.LinearLayout;
24 | import android.widget.TextView;
25 |
26 | import com.flyco.tablayout.listener.OnTabSelectListener;
27 | import com.flyco.tablayout.utils.FragmentChangeManager;
28 | import com.flyco.tablayout.utils.UnreadMsgUtils;
29 | import com.flyco.tablayout.widget.MsgView;
30 |
31 | import java.util.ArrayList;
32 |
33 | public class SegmentTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {
34 | private Context mContext;
35 | private String[] mTitles;
36 | private LinearLayout mTabsContainer;
37 | private int mCurrentTab;
38 | private int mLastTab;
39 | private int mTabCount;
40 | /** 用于绘制显示器 */
41 | private Rect mIndicatorRect = new Rect();
42 | private GradientDrawable mIndicatorDrawable = new GradientDrawable();
43 | private GradientDrawable mRectDrawable = new GradientDrawable();
44 |
45 | private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
46 |
47 | private float mTabPadding;
48 | private boolean mTabSpaceEqual;
49 | private float mTabWidth;
50 |
51 | /** indicator */
52 | private int mIndicatorColor;
53 | private float mIndicatorHeight;
54 | private float mIndicatorCornerRadius;
55 | private float mIndicatorMarginLeft;
56 | private float mIndicatorMarginTop;
57 | private float mIndicatorMarginRight;
58 | private float mIndicatorMarginBottom;
59 | private long mIndicatorAnimDuration;
60 | private boolean mIndicatorAnimEnable;
61 | private boolean mIndicatorBounceEnable;
62 |
63 | /** divider */
64 | private int mDividerColor;
65 | private float mDividerWidth;
66 | private float mDividerPadding;
67 |
68 | /** title */
69 | private static final int TEXT_BOLD_NONE = 0;
70 | private static final int TEXT_BOLD_WHEN_SELECT = 1;
71 | private static final int TEXT_BOLD_BOTH = 2;
72 | private float mTextsize;
73 | private int mTextSelectColor;
74 | private int mTextUnselectColor;
75 | private int mTextBold;
76 | private boolean mTextAllCaps;
77 |
78 | private int mBarColor;
79 | private int mBarStrokeColor;
80 | private float mBarStrokeWidth;
81 |
82 | private int mHeight;
83 |
84 | /** anim */
85 | private ValueAnimator mValueAnimator;
86 | private OvershootInterpolator mInterpolator = new OvershootInterpolator(0.8f);
87 |
88 | private FragmentChangeManager mFragmentChangeManager;
89 | private float[] mRadiusArr = new float[8];
90 |
91 | public SegmentTabLayout(Context context) {
92 | this(context, null, 0);
93 | }
94 |
95 | public SegmentTabLayout(Context context, AttributeSet attrs) {
96 | this(context, attrs, 0);
97 | }
98 |
99 | public SegmentTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
100 | super(context, attrs, defStyleAttr);
101 | setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
102 | setClipChildren(false);
103 | setClipToPadding(false);
104 |
105 | this.mContext = context;
106 | mTabsContainer = new LinearLayout(context);
107 | addView(mTabsContainer);
108 |
109 | obtainAttributes(context, attrs);
110 |
111 | //get layout_height
112 | String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
113 |
114 | //create ViewPager
115 | if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
116 | } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
117 | } else {
118 | int[] systemAttrs = {android.R.attr.layout_height};
119 | TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
120 | mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
121 | a.recycle();
122 | }
123 |
124 | mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);
125 | mValueAnimator.addUpdateListener(this);
126 | }
127 |
128 | private void obtainAttributes(Context context, AttributeSet attrs) {
129 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SegmentTabLayout);
130 |
131 | mIndicatorColor = ta.getColor(R.styleable.SegmentTabLayout_tl_indicator_color, Color.parseColor("#222831"));
132 | mIndicatorHeight = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_height, -1);
133 | mIndicatorCornerRadius = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_corner_radius, -1);
134 | mIndicatorMarginLeft = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_left, dp2px(0));
135 | mIndicatorMarginTop = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_top, 0);
136 | mIndicatorMarginRight = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_right, dp2px(0));
137 | mIndicatorMarginBottom = ta.getDimension(R.styleable.SegmentTabLayout_tl_indicator_margin_bottom, 0);
138 | mIndicatorAnimEnable = ta.getBoolean(R.styleable.SegmentTabLayout_tl_indicator_anim_enable, false);
139 | mIndicatorBounceEnable = ta.getBoolean(R.styleable.SegmentTabLayout_tl_indicator_bounce_enable, true);
140 | mIndicatorAnimDuration = ta.getInt(R.styleable.SegmentTabLayout_tl_indicator_anim_duration, -1);
141 |
142 | mDividerColor = ta.getColor(R.styleable.SegmentTabLayout_tl_divider_color, mIndicatorColor);
143 | mDividerWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_divider_width, dp2px(1));
144 | mDividerPadding = ta.getDimension(R.styleable.SegmentTabLayout_tl_divider_padding, 0);
145 |
146 | mTextsize = ta.getDimension(R.styleable.SegmentTabLayout_tl_textsize, sp2px(13f));
147 | mTextSelectColor = ta.getColor(R.styleable.SegmentTabLayout_tl_textSelectColor, Color.parseColor("#ffffff"));
148 | mTextUnselectColor = ta.getColor(R.styleable.SegmentTabLayout_tl_textUnselectColor, mIndicatorColor);
149 | mTextBold = ta.getInt(R.styleable.SegmentTabLayout_tl_textBold, TEXT_BOLD_NONE);
150 | mTextAllCaps = ta.getBoolean(R.styleable.SegmentTabLayout_tl_textAllCaps, false);
151 |
152 | mTabSpaceEqual = ta.getBoolean(R.styleable.SegmentTabLayout_tl_tab_space_equal, true);
153 | mTabWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_tab_width, dp2px(-1));
154 | mTabPadding = ta.getDimension(R.styleable.SegmentTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(10));
155 |
156 | mBarColor = ta.getColor(R.styleable.SegmentTabLayout_tl_bar_color, Color.TRANSPARENT);
157 | mBarStrokeColor = ta.getColor(R.styleable.SegmentTabLayout_tl_bar_stroke_color, mIndicatorColor);
158 | mBarStrokeWidth = ta.getDimension(R.styleable.SegmentTabLayout_tl_bar_stroke_width, dp2px(1));
159 |
160 | ta.recycle();
161 | }
162 |
163 | public void setTabData(String[] titles) {
164 | if (titles == null || titles.length == 0) {
165 | throw new IllegalStateException("Titles can not be NULL or EMPTY !");
166 | }
167 |
168 | this.mTitles = titles;
169 |
170 | notifyDataSetChanged();
171 | }
172 |
173 | /** 关联数据支持同时切换fragments */
174 | public void setTabData(String[] titles, FragmentActivity fa, int containerViewId, ArrayList fragments) {
175 | mFragmentChangeManager = new FragmentChangeManager(fa.getSupportFragmentManager(), containerViewId, fragments);
176 | setTabData(titles);
177 | }
178 |
179 | /** 更新数据 */
180 | public void notifyDataSetChanged() {
181 | mTabsContainer.removeAllViews();
182 | this.mTabCount = mTitles.length;
183 | View tabView;
184 | for (int i = 0; i < mTabCount; i++) {
185 | tabView = View.inflate(mContext, R.layout.layout_tab_segment, null);
186 | tabView.setTag(i);
187 | addTab(i, tabView);
188 | }
189 |
190 | updateTabStyles();
191 | }
192 |
193 | /** 创建并添加tab */
194 | private void addTab(final int position, View tabView) {
195 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
196 | tv_tab_title.setText(mTitles[position]);
197 |
198 | tabView.setOnClickListener(new OnClickListener() {
199 | @Override
200 | public void onClick(View v) {
201 | int position = (Integer) v.getTag();
202 | if (mCurrentTab != position) {
203 | setCurrentTab(position);
204 | if (mListener != null) {
205 | mListener.onTabSelect(position);
206 | }
207 | } else {
208 | if (mListener != null) {
209 | mListener.onTabReselect(position);
210 | }
211 | }
212 | }
213 | });
214 |
215 | /** 每一个Tab的布局参数 */
216 | LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ?
217 | new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) :
218 | new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
219 | if (mTabWidth > 0) {
220 | lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT);
221 | }
222 | mTabsContainer.addView(tabView, position, lp_tab);
223 | }
224 |
225 | private void updateTabStyles() {
226 | for (int i = 0; i < mTabCount; i++) {
227 | View tabView = mTabsContainer.getChildAt(i);
228 | tabView.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
229 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
230 | tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor);
231 | tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize);
232 | // tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
233 | if (mTextAllCaps) {
234 | tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase());
235 | }
236 |
237 | if (mTextBold == TEXT_BOLD_BOTH) {
238 | tv_tab_title.getPaint().setFakeBoldText(true);
239 | } else if (mTextBold == TEXT_BOLD_NONE) {
240 | tv_tab_title.getPaint().setFakeBoldText(false);
241 | }
242 | }
243 | }
244 |
245 | private void updateTabSelection(int position) {
246 | for (int i = 0; i < mTabCount; ++i) {
247 | View tabView = mTabsContainer.getChildAt(i);
248 | final boolean isSelect = i == position;
249 | TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
250 | tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor);
251 | if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
252 | tab_title.getPaint().setFakeBoldText(isSelect);
253 | }
254 | }
255 | }
256 |
257 | private void calcOffset() {
258 | final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
259 | mCurrentP.left = currentTabView.getLeft();
260 | mCurrentP.right = currentTabView.getRight();
261 |
262 | final View lastTabView = mTabsContainer.getChildAt(this.mLastTab);
263 | mLastP.left = lastTabView.getLeft();
264 | mLastP.right = lastTabView.getRight();
265 |
266 | // Log.d("AAA", "mLastP--->" + mLastP.left + "&" + mLastP.right);
267 | // Log.d("AAA", "mCurrentP--->" + mCurrentP.left + "&" + mCurrentP.right);
268 | if (mLastP.left == mCurrentP.left && mLastP.right == mCurrentP.right) {
269 | invalidate();
270 | } else {
271 | mValueAnimator.setObjectValues(mLastP, mCurrentP);
272 | if (mIndicatorBounceEnable) {
273 | mValueAnimator.setInterpolator(mInterpolator);
274 | }
275 |
276 | if (mIndicatorAnimDuration < 0) {
277 | mIndicatorAnimDuration = mIndicatorBounceEnable ? 500 : 250;
278 | }
279 | mValueAnimator.setDuration(mIndicatorAnimDuration);
280 | mValueAnimator.start();
281 | }
282 | }
283 |
284 | private void calcIndicatorRect() {
285 | View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
286 | float left = currentTabView.getLeft();
287 | float right = currentTabView.getRight();
288 |
289 | mIndicatorRect.left = (int) left;
290 | mIndicatorRect.right = (int) right;
291 |
292 | if (!mIndicatorAnimEnable) {
293 | if (mCurrentTab == 0) {
294 | /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/
295 | mRadiusArr[0] = mIndicatorCornerRadius;
296 | mRadiusArr[1] = mIndicatorCornerRadius;
297 | mRadiusArr[2] = 0;
298 | mRadiusArr[3] = 0;
299 | mRadiusArr[4] = 0;
300 | mRadiusArr[5] = 0;
301 | mRadiusArr[6] = mIndicatorCornerRadius;
302 | mRadiusArr[7] = mIndicatorCornerRadius;
303 | } else if (mCurrentTab == mTabCount - 1) {
304 | /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/
305 | mRadiusArr[0] = 0;
306 | mRadiusArr[1] = 0;
307 | mRadiusArr[2] = mIndicatorCornerRadius;
308 | mRadiusArr[3] = mIndicatorCornerRadius;
309 | mRadiusArr[4] = mIndicatorCornerRadius;
310 | mRadiusArr[5] = mIndicatorCornerRadius;
311 | mRadiusArr[6] = 0;
312 | mRadiusArr[7] = 0;
313 | } else {
314 | /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/
315 | mRadiusArr[0] = 0;
316 | mRadiusArr[1] = 0;
317 | mRadiusArr[2] = 0;
318 | mRadiusArr[3] = 0;
319 | mRadiusArr[4] = 0;
320 | mRadiusArr[5] = 0;
321 | mRadiusArr[6] = 0;
322 | mRadiusArr[7] = 0;
323 | }
324 | } else {
325 | /**The corners are ordered top-left, top-right, bottom-right, bottom-left*/
326 | mRadiusArr[0] = mIndicatorCornerRadius;
327 | mRadiusArr[1] = mIndicatorCornerRadius;
328 | mRadiusArr[2] = mIndicatorCornerRadius;
329 | mRadiusArr[3] = mIndicatorCornerRadius;
330 | mRadiusArr[4] = mIndicatorCornerRadius;
331 | mRadiusArr[5] = mIndicatorCornerRadius;
332 | mRadiusArr[6] = mIndicatorCornerRadius;
333 | mRadiusArr[7] = mIndicatorCornerRadius;
334 | }
335 | }
336 |
337 | @Override
338 | public void onAnimationUpdate(ValueAnimator animation) {
339 | IndicatorPoint p = (IndicatorPoint) animation.getAnimatedValue();
340 | mIndicatorRect.left = (int) p.left;
341 | mIndicatorRect.right = (int) p.right;
342 | invalidate();
343 | }
344 |
345 | private boolean mIsFirstDraw = true;
346 |
347 | @Override
348 | protected void onDraw(Canvas canvas) {
349 | super.onDraw(canvas);
350 |
351 | if (isInEditMode() || mTabCount <= 0) {
352 | return;
353 | }
354 |
355 | int height = getHeight();
356 | int paddingLeft = getPaddingLeft();
357 |
358 | if (mIndicatorHeight < 0) {
359 | mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom;
360 | }
361 |
362 | if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) {
363 | mIndicatorCornerRadius = mIndicatorHeight / 2;
364 | }
365 |
366 | //draw rect
367 | mRectDrawable.setColor(mBarColor);
368 | mRectDrawable.setStroke((int) mBarStrokeWidth, mBarStrokeColor);
369 | mRectDrawable.setCornerRadius(mIndicatorCornerRadius);
370 | mRectDrawable.setBounds(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom());
371 | mRectDrawable.draw(canvas);
372 |
373 | // draw divider
374 | if (!mIndicatorAnimEnable && mDividerWidth > 0) {
375 | mDividerPaint.setStrokeWidth(mDividerWidth);
376 | mDividerPaint.setColor(mDividerColor);
377 | for (int i = 0; i < mTabCount - 1; i++) {
378 | View tab = mTabsContainer.getChildAt(i);
379 | canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint);
380 | }
381 | }
382 |
383 |
384 | //draw indicator line
385 | if (mIndicatorAnimEnable) {
386 | if (mIsFirstDraw) {
387 | mIsFirstDraw = false;
388 | calcIndicatorRect();
389 | }
390 | } else {
391 | calcIndicatorRect();
392 | }
393 |
394 | mIndicatorDrawable.setColor(mIndicatorColor);
395 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
396 | (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight),
397 | (int) (mIndicatorMarginTop + mIndicatorHeight));
398 | mIndicatorDrawable.setCornerRadii(mRadiusArr);
399 | mIndicatorDrawable.draw(canvas);
400 |
401 | }
402 |
403 | //setter and getter
404 | public void setCurrentTab(int currentTab) {
405 | mLastTab = this.mCurrentTab;
406 | this.mCurrentTab = currentTab;
407 | updateTabSelection(currentTab);
408 | if (mFragmentChangeManager != null) {
409 | mFragmentChangeManager.setFragments(currentTab);
410 | }
411 | if (mIndicatorAnimEnable) {
412 | calcOffset();
413 | } else {
414 | invalidate();
415 | }
416 | }
417 |
418 | public void setTabPadding(float tabPadding) {
419 | this.mTabPadding = dp2px(tabPadding);
420 | updateTabStyles();
421 | }
422 |
423 | public void setTabSpaceEqual(boolean tabSpaceEqual) {
424 | this.mTabSpaceEqual = tabSpaceEqual;
425 | updateTabStyles();
426 | }
427 |
428 | public void setTabWidth(float tabWidth) {
429 | this.mTabWidth = dp2px(tabWidth);
430 | updateTabStyles();
431 | }
432 |
433 | public void setIndicatorColor(int indicatorColor) {
434 | this.mIndicatorColor = indicatorColor;
435 | invalidate();
436 | }
437 |
438 | public void setIndicatorHeight(float indicatorHeight) {
439 | this.mIndicatorHeight = dp2px(indicatorHeight);
440 | invalidate();
441 | }
442 |
443 | public void setIndicatorCornerRadius(float indicatorCornerRadius) {
444 | this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius);
445 | invalidate();
446 | }
447 |
448 | public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop,
449 | float indicatorMarginRight, float indicatorMarginBottom) {
450 | this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft);
451 | this.mIndicatorMarginTop = dp2px(indicatorMarginTop);
452 | this.mIndicatorMarginRight = dp2px(indicatorMarginRight);
453 | this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom);
454 | invalidate();
455 | }
456 |
457 | public void setIndicatorAnimDuration(long indicatorAnimDuration) {
458 | this.mIndicatorAnimDuration = indicatorAnimDuration;
459 | }
460 |
461 | public void setIndicatorAnimEnable(boolean indicatorAnimEnable) {
462 | this.mIndicatorAnimEnable = indicatorAnimEnable;
463 | }
464 |
465 | public void setIndicatorBounceEnable(boolean indicatorBounceEnable) {
466 | this.mIndicatorBounceEnable = indicatorBounceEnable;
467 | }
468 |
469 | public void setDividerColor(int dividerColor) {
470 | this.mDividerColor = dividerColor;
471 | invalidate();
472 | }
473 |
474 | public void setDividerWidth(float dividerWidth) {
475 | this.mDividerWidth = dp2px(dividerWidth);
476 | invalidate();
477 | }
478 |
479 | public void setDividerPadding(float dividerPadding) {
480 | this.mDividerPadding = dp2px(dividerPadding);
481 | invalidate();
482 | }
483 |
484 | public void setTextsize(float textsize) {
485 | this.mTextsize = sp2px(textsize);
486 | updateTabStyles();
487 | }
488 |
489 | public void setTextSelectColor(int textSelectColor) {
490 | this.mTextSelectColor = textSelectColor;
491 | updateTabStyles();
492 | }
493 |
494 | public void setTextUnselectColor(int textUnselectColor) {
495 | this.mTextUnselectColor = textUnselectColor;
496 | updateTabStyles();
497 | }
498 |
499 | public void setTextBold(int textBold) {
500 | this.mTextBold = textBold;
501 | updateTabStyles();
502 | }
503 |
504 | public void setTextAllCaps(boolean textAllCaps) {
505 | this.mTextAllCaps = textAllCaps;
506 | updateTabStyles();
507 | }
508 |
509 | public int getTabCount() {
510 | return mTabCount;
511 | }
512 |
513 | public int getCurrentTab() {
514 | return mCurrentTab;
515 | }
516 |
517 | public float getTabPadding() {
518 | return mTabPadding;
519 | }
520 |
521 | public boolean isTabSpaceEqual() {
522 | return mTabSpaceEqual;
523 | }
524 |
525 | public float getTabWidth() {
526 | return mTabWidth;
527 | }
528 |
529 | public int getIndicatorColor() {
530 | return mIndicatorColor;
531 | }
532 |
533 | public float getIndicatorHeight() {
534 | return mIndicatorHeight;
535 | }
536 |
537 | public float getIndicatorCornerRadius() {
538 | return mIndicatorCornerRadius;
539 | }
540 |
541 | public float getIndicatorMarginLeft() {
542 | return mIndicatorMarginLeft;
543 | }
544 |
545 | public float getIndicatorMarginTop() {
546 | return mIndicatorMarginTop;
547 | }
548 |
549 | public float getIndicatorMarginRight() {
550 | return mIndicatorMarginRight;
551 | }
552 |
553 | public float getIndicatorMarginBottom() {
554 | return mIndicatorMarginBottom;
555 | }
556 |
557 | public long getIndicatorAnimDuration() {
558 | return mIndicatorAnimDuration;
559 | }
560 |
561 | public boolean isIndicatorAnimEnable() {
562 | return mIndicatorAnimEnable;
563 | }
564 |
565 | public boolean isIndicatorBounceEnable() {
566 | return mIndicatorBounceEnable;
567 | }
568 |
569 | public int getDividerColor() {
570 | return mDividerColor;
571 | }
572 |
573 | public float getDividerWidth() {
574 | return mDividerWidth;
575 | }
576 |
577 | public float getDividerPadding() {
578 | return mDividerPadding;
579 | }
580 |
581 | public float getTextsize() {
582 | return mTextsize;
583 | }
584 |
585 | public int getTextSelectColor() {
586 | return mTextSelectColor;
587 | }
588 |
589 | public int getTextUnselectColor() {
590 | return mTextUnselectColor;
591 | }
592 |
593 | public int getTextBold() {
594 | return mTextBold;
595 | }
596 |
597 | public boolean isTextAllCaps() {
598 | return mTextAllCaps;
599 | }
600 |
601 | public TextView getTitleView(int tab) {
602 | View tabView = mTabsContainer.getChildAt(tab);
603 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
604 | return tv_tab_title;
605 | }
606 |
607 | //setter and getter
608 | // show MsgTipView
609 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
610 | private SparseArray mInitSetMap = new SparseArray<>();
611 |
612 | /**
613 | * 显示未读消息
614 | *
615 | * @param position 显示tab位置
616 | * @param num num小于等于0显示红点,num大于0显示数字
617 | */
618 | public void showMsg(int position, int num) {
619 | if (position >= mTabCount) {
620 | position = mTabCount - 1;
621 | }
622 |
623 | View tabView = mTabsContainer.getChildAt(position);
624 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
625 | if (tipView != null) {
626 | UnreadMsgUtils.show(tipView, num);
627 |
628 | if (mInitSetMap.get(position) != null && mInitSetMap.get(position)) {
629 | return;
630 | }
631 |
632 | setMsgMargin(position, 2, 2);
633 |
634 | mInitSetMap.put(position, true);
635 | }
636 | }
637 |
638 | /**
639 | * 显示未读红点
640 | *
641 | * @param position 显示tab位置
642 | */
643 | public void showDot(int position) {
644 | if (position >= mTabCount) {
645 | position = mTabCount - 1;
646 | }
647 | showMsg(position, 0);
648 | }
649 |
650 | public void hideMsg(int position) {
651 | if (position >= mTabCount) {
652 | position = mTabCount - 1;
653 | }
654 |
655 | View tabView = mTabsContainer.getChildAt(position);
656 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
657 | if (tipView != null) {
658 | tipView.setVisibility(View.GONE);
659 | }
660 | }
661 |
662 | /**
663 | * 设置提示红点偏移,注意
664 | * 1.控件为固定高度:参照点为tab内容的右上角
665 | * 2.控件高度不固定(WRAP_CONTENT):参照点为tab内容的右上角,此时高度已是红点的最高显示范围,所以这时bottomPadding其实就是topPadding
666 | */
667 | public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
668 | if (position >= mTabCount) {
669 | position = mTabCount - 1;
670 | }
671 | View tabView = mTabsContainer.getChildAt(position);
672 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
673 | if (tipView != null) {
674 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
675 | mTextPaint.setTextSize(mTextsize);
676 | float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString());
677 | float textHeight = mTextPaint.descent() - mTextPaint.ascent();
678 | MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams();
679 |
680 | lp.leftMargin = dp2px(leftPadding);
681 | lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding);
682 |
683 | tipView.setLayoutParams(lp);
684 | }
685 | }
686 |
687 | /** 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 */
688 | public MsgView getMsgView(int position) {
689 | if (position >= mTabCount) {
690 | position = mTabCount - 1;
691 | }
692 | View tabView = mTabsContainer.getChildAt(position);
693 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
694 | return tipView;
695 | }
696 |
697 | private OnTabSelectListener mListener;
698 |
699 | public void setOnTabSelectListener(OnTabSelectListener listener) {
700 | this.mListener = listener;
701 | }
702 |
703 | @Override
704 | protected Parcelable onSaveInstanceState() {
705 | Bundle bundle = new Bundle();
706 | bundle.putParcelable("instanceState", super.onSaveInstanceState());
707 | bundle.putInt("mCurrentTab", mCurrentTab);
708 | return bundle;
709 | }
710 |
711 | @Override
712 | protected void onRestoreInstanceState(Parcelable state) {
713 | if (state instanceof Bundle) {
714 | Bundle bundle = (Bundle) state;
715 | mCurrentTab = bundle.getInt("mCurrentTab");
716 | state = bundle.getParcelable("instanceState");
717 | if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) {
718 | updateTabSelection(mCurrentTab);
719 | }
720 | }
721 | super.onRestoreInstanceState(state);
722 | }
723 |
724 | class IndicatorPoint {
725 | public float left;
726 | public float right;
727 | }
728 |
729 | private IndicatorPoint mCurrentP = new IndicatorPoint();
730 | private IndicatorPoint mLastP = new IndicatorPoint();
731 |
732 | class PointEvaluator implements TypeEvaluator {
733 | @Override
734 | public IndicatorPoint evaluate(float fraction, IndicatorPoint startValue, IndicatorPoint endValue) {
735 | float left = startValue.left + fraction * (endValue.left - startValue.left);
736 | float right = startValue.right + fraction * (endValue.right - startValue.right);
737 | IndicatorPoint point = new IndicatorPoint();
738 | point.left = left;
739 | point.right = right;
740 | return point;
741 | }
742 | }
743 |
744 | protected int dp2px(float dp) {
745 | final float scale = mContext.getResources().getDisplayMetrics().density;
746 | return (int) (dp * scale + 0.5f);
747 | }
748 |
749 | protected int sp2px(float sp) {
750 | final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity;
751 | return (int) (sp * scale + 0.5f);
752 | }
753 | }
754 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 | import android.graphics.Path;
9 | import android.graphics.Rect;
10 | import android.graphics.drawable.GradientDrawable;
11 | import android.os.Bundle;
12 | import android.os.Parcelable;
13 | import android.support.v4.app.Fragment;
14 | import android.support.v4.app.FragmentActivity;
15 | import android.support.v4.app.FragmentManager;
16 | import android.support.v4.app.FragmentPagerAdapter;
17 | import android.support.v4.view.PagerAdapter;
18 | import android.support.v4.view.ViewPager;
19 | import android.util.AttributeSet;
20 | import android.util.SparseArray;
21 | import android.util.TypedValue;
22 | import android.view.Gravity;
23 | import android.view.View;
24 | import android.view.ViewGroup;
25 | import android.widget.HorizontalScrollView;
26 | import android.widget.LinearLayout;
27 | import android.widget.TextView;
28 |
29 | import com.flyco.tablayout.listener.OnTabSelectListener;
30 | import com.flyco.tablayout.utils.UnreadMsgUtils;
31 | import com.flyco.tablayout.widget.MsgView;
32 |
33 | import java.util.ArrayList;
34 | import java.util.Collections;
35 |
36 | /** 滑动TabLayout,对于ViewPager的依赖性强 */
37 | public class SlidingTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener {
38 | private Context mContext;
39 | private ViewPager mViewPager;
40 | private ArrayList mTitles;
41 | private LinearLayout mTabsContainer;
42 | private int mCurrentTab;
43 | private float mCurrentPositionOffset;
44 | private int mTabCount;
45 | /** 用于绘制显示器 */
46 | private Rect mIndicatorRect = new Rect();
47 | /** 用于实现滚动居中 */
48 | private Rect mTabRect = new Rect();
49 | private GradientDrawable mIndicatorDrawable = new GradientDrawable();
50 |
51 | private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
52 | private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
53 | private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
54 | private Path mTrianglePath = new Path();
55 | private static final int STYLE_NORMAL = 0;
56 | private static final int STYLE_TRIANGLE = 1;
57 | private static final int STYLE_BLOCK = 2;
58 | private int mIndicatorStyle = STYLE_NORMAL;
59 |
60 | private float mTabPadding;
61 | private boolean mTabSpaceEqual;
62 | private float mTabWidth;
63 |
64 | /** indicator */
65 | private int mIndicatorColor;
66 | private float mIndicatorHeight;
67 | private float mIndicatorWidth;
68 | private float mIndicatorCornerRadius;
69 | private float mIndicatorMarginLeft;
70 | private float mIndicatorMarginTop;
71 | private float mIndicatorMarginRight;
72 | private float mIndicatorMarginBottom;
73 | private int mIndicatorGravity;
74 | private boolean mIndicatorWidthEqualTitle;
75 |
76 | /** underline */
77 | private int mUnderlineColor;
78 | private float mUnderlineHeight;
79 | private int mUnderlineGravity;
80 |
81 | /** divider */
82 | private int mDividerColor;
83 | private float mDividerWidth;
84 | private float mDividerPadding;
85 |
86 | /** title */
87 | private static final int TEXT_BOLD_NONE = 0;
88 | private static final int TEXT_BOLD_WHEN_SELECT = 1;
89 | private static final int TEXT_BOLD_BOTH = 2;
90 | private float mTextsize;
91 | private int mTextSelectColor;
92 | private int mTextUnselectColor;
93 | private int mTextBold;
94 | private boolean mTextAllCaps;
95 |
96 | private int mLastScrollX;
97 | private int mHeight;
98 |
99 | public SlidingTabLayout(Context context) {
100 | this(context, null, 0);
101 | }
102 |
103 | public SlidingTabLayout(Context context, AttributeSet attrs) {
104 | this(context, attrs, 0);
105 | }
106 |
107 | public SlidingTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
108 | super(context, attrs, defStyleAttr);
109 | setFillViewport(true);//设置滚动视图是否可以伸缩其内容以填充视口
110 | setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
111 | setClipChildren(false);
112 | setClipToPadding(false);
113 |
114 | this.mContext = context;
115 | mTabsContainer = new LinearLayout(context);
116 | addView(mTabsContainer);
117 |
118 | obtainAttributes(context, attrs);
119 |
120 | //get layout_height
121 | String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
122 |
123 | //create ViewPager
124 | if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
125 | } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
126 | } else {
127 | int[] systemAttrs = {android.R.attr.layout_height};
128 | TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
129 | mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
130 | a.recycle();
131 | }
132 | }
133 |
134 | private void obtainAttributes(Context context, AttributeSet attrs) {
135 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingTabLayout);
136 |
137 | mIndicatorStyle = ta.getInt(R.styleable.SlidingTabLayout_tl_indicator_style, STYLE_NORMAL);
138 | mIndicatorColor = ta.getColor(R.styleable.SlidingTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? "#4B6A87" : "#ffffff"));
139 | mIndicatorHeight = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_height,
140 | dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2)));
141 | mIndicatorWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1));
142 | mIndicatorCornerRadius = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_corner_radius, dp2px(mIndicatorStyle == STYLE_BLOCK ? -1 : 0));
143 | mIndicatorMarginLeft = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_left, dp2px(0));
144 | mIndicatorMarginTop = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_top, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0));
145 | mIndicatorMarginRight = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_right, dp2px(0));
146 | mIndicatorMarginBottom = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_bottom, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0));
147 | mIndicatorGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_indicator_gravity, Gravity.BOTTOM);
148 | mIndicatorWidthEqualTitle = ta.getBoolean(R.styleable.SlidingTabLayout_tl_indicator_width_equal_title, false);
149 |
150 | mUnderlineColor = ta.getColor(R.styleable.SlidingTabLayout_tl_underline_color, Color.parseColor("#ffffff"));
151 | mUnderlineHeight = ta.getDimension(R.styleable.SlidingTabLayout_tl_underline_height, dp2px(0));
152 | mUnderlineGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_underline_gravity, Gravity.BOTTOM);
153 |
154 | mDividerColor = ta.getColor(R.styleable.SlidingTabLayout_tl_divider_color, Color.parseColor("#ffffff"));
155 | mDividerWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_divider_width, dp2px(0));
156 | mDividerPadding = ta.getDimension(R.styleable.SlidingTabLayout_tl_divider_padding, dp2px(12));
157 |
158 | mTextsize = ta.getDimension(R.styleable.SlidingTabLayout_tl_textsize, sp2px(14));
159 | mTextSelectColor = ta.getColor(R.styleable.SlidingTabLayout_tl_textSelectColor, Color.parseColor("#ffffff"));
160 | mTextUnselectColor = ta.getColor(R.styleable.SlidingTabLayout_tl_textUnselectColor, Color.parseColor("#AAffffff"));
161 | mTextBold = ta.getInt(R.styleable.SlidingTabLayout_tl_textBold, TEXT_BOLD_NONE);
162 | mTextAllCaps = ta.getBoolean(R.styleable.SlidingTabLayout_tl_textAllCaps, false);
163 |
164 | mTabSpaceEqual = ta.getBoolean(R.styleable.SlidingTabLayout_tl_tab_space_equal, false);
165 | mTabWidth = ta.getDimension(R.styleable.SlidingTabLayout_tl_tab_width, dp2px(-1));
166 | mTabPadding = ta.getDimension(R.styleable.SlidingTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(20));
167 |
168 | ta.recycle();
169 | }
170 |
171 | /** 关联ViewPager */
172 | public void setViewPager(ViewPager vp) {
173 | if (vp == null || vp.getAdapter() == null) {
174 | throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !");
175 | }
176 |
177 | this.mViewPager = vp;
178 |
179 | this.mViewPager.removeOnPageChangeListener(this);
180 | this.mViewPager.addOnPageChangeListener(this);
181 | notifyDataSetChanged();
182 | }
183 |
184 | /** 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 */
185 | public void setViewPager(ViewPager vp, String[] titles) {
186 | if (vp == null || vp.getAdapter() == null) {
187 | throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !");
188 | }
189 |
190 | if (titles == null || titles.length == 0) {
191 | throw new IllegalStateException("Titles can not be EMPTY !");
192 | }
193 |
194 | if (titles.length != vp.getAdapter().getCount()) {
195 | throw new IllegalStateException("Titles length must be the same as the page count !");
196 | }
197 |
198 | this.mViewPager = vp;
199 | mTitles = new ArrayList<>();
200 | Collections.addAll(mTitles, titles);
201 |
202 | this.mViewPager.removeOnPageChangeListener(this);
203 | this.mViewPager.addOnPageChangeListener(this);
204 | notifyDataSetChanged();
205 | }
206 |
207 | /** 关联ViewPager,用于连适配器都不想自己实例化的情况 */
208 | public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList fragments) {
209 | if (vp == null) {
210 | throw new IllegalStateException("ViewPager can not be NULL !");
211 | }
212 |
213 | if (titles == null || titles.length == 0) {
214 | throw new IllegalStateException("Titles can not be EMPTY !");
215 | }
216 |
217 | this.mViewPager = vp;
218 | this.mViewPager.setAdapter(new InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles));
219 |
220 | this.mViewPager.removeOnPageChangeListener(this);
221 | this.mViewPager.addOnPageChangeListener(this);
222 | notifyDataSetChanged();
223 | }
224 |
225 | /** 更新数据 */
226 | public void notifyDataSetChanged() {
227 | mTabsContainer.removeAllViews();
228 | this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size();
229 | View tabView;
230 | for (int i = 0; i < mTabCount; i++) {
231 | tabView = View.inflate(mContext, R.layout.layout_tab, null);
232 | CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles.get(i);
233 | addTab(i, pageTitle.toString(), tabView);
234 | }
235 |
236 | updateTabStyles();
237 | }
238 |
239 | public void addNewTab(String title) {
240 | View tabView = View.inflate(mContext, R.layout.layout_tab, null);
241 | if (mTitles != null) {
242 | mTitles.add(title);
243 | }
244 |
245 | CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(mTabCount) : mTitles.get(mTabCount);
246 | addTab(mTabCount, pageTitle.toString(), tabView);
247 | this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size();
248 |
249 | updateTabStyles();
250 | }
251 |
252 | /** 创建并添加tab */
253 | private void addTab(final int position, String title, View tabView) {
254 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
255 | if (tv_tab_title != null) {
256 | if (title != null) tv_tab_title.setText(title);
257 | }
258 |
259 | tabView.setOnClickListener(new OnClickListener() {
260 | @Override
261 | public void onClick(View v) {
262 | int position = mTabsContainer.indexOfChild(v);
263 | if (position != -1) {
264 | if (mViewPager.getCurrentItem() != position) {
265 | mViewPager.setCurrentItem(position);
266 | if (mListener != null) {
267 | mListener.onTabSelect(position);
268 | }
269 | } else {
270 | if (mListener != null) {
271 | mListener.onTabReselect(position);
272 | }
273 | }
274 | }
275 | }
276 | });
277 |
278 | /** 每一个Tab的布局参数 */
279 | LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ?
280 | new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) :
281 | new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
282 | if (mTabWidth > 0) {
283 | lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT);
284 | }
285 |
286 | mTabsContainer.addView(tabView, position, lp_tab);
287 | }
288 |
289 | private void updateTabStyles() {
290 | for (int i = 0; i < mTabCount; i++) {
291 | View v = mTabsContainer.getChildAt(i);
292 | // v.setPadding((int) mTabPadding, v.getPaddingTop(), (int) mTabPadding, v.getPaddingBottom());
293 | TextView tv_tab_title = (TextView) v.findViewById(R.id.tv_tab_title);
294 | if (tv_tab_title != null) {
295 | tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor);
296 | tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize);
297 | tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
298 | if (mTextAllCaps) {
299 | tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase());
300 | }
301 |
302 | if (mTextBold == TEXT_BOLD_BOTH) {
303 | tv_tab_title.getPaint().setFakeBoldText(true);
304 | } else if (mTextBold == TEXT_BOLD_NONE) {
305 | tv_tab_title.getPaint().setFakeBoldText(false);
306 | }
307 | }
308 | }
309 | }
310 |
311 | @Override
312 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
313 | /**
314 | * position:当前View的位置
315 | * mCurrentPositionOffset:当前View的偏移量比例.[0,1)
316 | */
317 | this.mCurrentTab = position;
318 | this.mCurrentPositionOffset = positionOffset;
319 | scrollToCurrentTab();
320 | invalidate();
321 | }
322 |
323 | @Override
324 | public void onPageSelected(int position) {
325 | updateTabSelection(position);
326 | }
327 |
328 | @Override
329 | public void onPageScrollStateChanged(int state) {
330 | }
331 |
332 | /** HorizontalScrollView滚到当前tab,并且居中显示 */
333 | private void scrollToCurrentTab() {
334 | if (mTabCount <= 0) {
335 | return;
336 | }
337 |
338 | int offset = (int) (mCurrentPositionOffset * mTabsContainer.getChildAt(mCurrentTab).getWidth());
339 | /**当前Tab的left+当前Tab的Width乘以positionOffset*/
340 | int newScrollX = mTabsContainer.getChildAt(mCurrentTab).getLeft() + offset;
341 |
342 | if (mCurrentTab > 0 || offset > 0) {
343 | /**HorizontalScrollView移动到当前tab,并居中*/
344 | newScrollX -= getWidth() / 2 - getPaddingLeft();
345 | calcIndicatorRect();
346 | newScrollX += ((mTabRect.right - mTabRect.left) / 2);
347 | }
348 |
349 | if (newScrollX != mLastScrollX) {
350 | mLastScrollX = newScrollX;
351 | /** scrollTo(int x,int y):x,y代表的不是坐标点,而是偏移量
352 | * x:表示离起始位置的x水平方向的偏移量
353 | * y:表示离起始位置的y垂直方向的偏移量
354 | */
355 | scrollTo(newScrollX, 0);
356 | }
357 | }
358 |
359 | private void updateTabSelection(int position) {
360 | for (int i = 0; i < mTabCount; ++i) {
361 | View tabView = mTabsContainer.getChildAt(i);
362 | final boolean isSelect = i == position;
363 | TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
364 |
365 | if (tab_title != null) {
366 | tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor);
367 | if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
368 | tab_title.getPaint().setFakeBoldText(isSelect);
369 | }
370 | }
371 | }
372 | }
373 |
374 | private float margin;
375 |
376 | private void calcIndicatorRect() {
377 | View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
378 | float left = currentTabView.getLeft();
379 | float right = currentTabView.getRight();
380 |
381 | //for mIndicatorWidthEqualTitle
382 | if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) {
383 | TextView tab_title = (TextView) currentTabView.findViewById(R.id.tv_tab_title);
384 | mTextPaint.setTextSize(mTextsize);
385 | float textWidth = mTextPaint.measureText(tab_title.getText().toString());
386 | margin = (right - left - textWidth) / 2;
387 | }
388 |
389 | if (this.mCurrentTab < mTabCount - 1) {
390 | View nextTabView = mTabsContainer.getChildAt(this.mCurrentTab + 1);
391 | float nextTabLeft = nextTabView.getLeft();
392 | float nextTabRight = nextTabView.getRight();
393 |
394 | left = left + mCurrentPositionOffset * (nextTabLeft - left);
395 | right = right + mCurrentPositionOffset * (nextTabRight - right);
396 |
397 | //for mIndicatorWidthEqualTitle
398 | if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) {
399 | TextView next_tab_title = (TextView) nextTabView.findViewById(R.id.tv_tab_title);
400 | mTextPaint.setTextSize(mTextsize);
401 | float nextTextWidth = mTextPaint.measureText(next_tab_title.getText().toString());
402 | float nextMargin = (nextTabRight - nextTabLeft - nextTextWidth) / 2;
403 | margin = margin + mCurrentPositionOffset * (nextMargin - margin);
404 | }
405 | }
406 |
407 | mIndicatorRect.left = (int) left;
408 | mIndicatorRect.right = (int) right;
409 | //for mIndicatorWidthEqualTitle
410 | if (mIndicatorStyle == STYLE_NORMAL && mIndicatorWidthEqualTitle) {
411 | mIndicatorRect.left = (int) (left + margin - 1);
412 | mIndicatorRect.right = (int) (right - margin - 1);
413 | }
414 |
415 | mTabRect.left = (int) left;
416 | mTabRect.right = (int) right;
417 |
418 | if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip
419 |
420 | } else {//indicatorWidth大于0时,圆角矩形以及三角形
421 | float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - mIndicatorWidth) / 2;
422 |
423 | if (this.mCurrentTab < mTabCount - 1) {
424 | View nextTab = mTabsContainer.getChildAt(this.mCurrentTab + 1);
425 | indicatorLeft = indicatorLeft + mCurrentPositionOffset * (currentTabView.getWidth() / 2 + nextTab.getWidth() / 2);
426 | }
427 |
428 | mIndicatorRect.left = (int) indicatorLeft;
429 | mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth);
430 | }
431 | }
432 |
433 | @Override
434 | protected void onDraw(Canvas canvas) {
435 | super.onDraw(canvas);
436 |
437 | if (isInEditMode() || mTabCount <= 0) {
438 | return;
439 | }
440 |
441 | int height = getHeight();
442 | int paddingLeft = getPaddingLeft();
443 | // draw divider
444 | if (mDividerWidth > 0) {
445 | mDividerPaint.setStrokeWidth(mDividerWidth);
446 | mDividerPaint.setColor(mDividerColor);
447 | for (int i = 0; i < mTabCount - 1; i++) {
448 | View tab = mTabsContainer.getChildAt(i);
449 | canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint);
450 | }
451 | }
452 |
453 | // draw underline
454 | if (mUnderlineHeight > 0) {
455 | mRectPaint.setColor(mUnderlineColor);
456 | if (mUnderlineGravity == Gravity.BOTTOM) {
457 | canvas.drawRect(paddingLeft, height - mUnderlineHeight, mTabsContainer.getWidth() + paddingLeft, height, mRectPaint);
458 | } else {
459 | canvas.drawRect(paddingLeft, 0, mTabsContainer.getWidth() + paddingLeft, mUnderlineHeight, mRectPaint);
460 | }
461 | }
462 |
463 | //draw indicator line
464 |
465 | calcIndicatorRect();
466 | if (mIndicatorStyle == STYLE_TRIANGLE) {
467 | if (mIndicatorHeight > 0) {
468 | mTrianglePaint.setColor(mIndicatorColor);
469 | mTrianglePath.reset();
470 | mTrianglePath.moveTo(paddingLeft + mIndicatorRect.left, height);
471 | mTrianglePath.lineTo(paddingLeft + mIndicatorRect.left / 2 + mIndicatorRect.right / 2, height - mIndicatorHeight);
472 | mTrianglePath.lineTo(paddingLeft + mIndicatorRect.right, height);
473 | mTrianglePath.close();
474 | canvas.drawPath(mTrianglePath, mTrianglePaint);
475 | }
476 | } else if (mIndicatorStyle == STYLE_BLOCK) {
477 | if (mIndicatorHeight < 0) {
478 | mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom;
479 | } else {
480 |
481 | }
482 |
483 | if (mIndicatorHeight > 0) {
484 | if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) {
485 | mIndicatorCornerRadius = mIndicatorHeight / 2;
486 | }
487 |
488 | mIndicatorDrawable.setColor(mIndicatorColor);
489 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
490 | (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight),
491 | (int) (mIndicatorMarginTop + mIndicatorHeight));
492 | mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
493 | mIndicatorDrawable.draw(canvas);
494 | }
495 | } else {
496 | /* mRectPaint.setColor(mIndicatorColor);
497 | calcIndicatorRect();
498 | canvas.drawRect(getPaddingLeft() + mIndicatorRect.left, getHeight() - mIndicatorHeight,
499 | mIndicatorRect.right + getPaddingLeft(), getHeight(), mRectPaint);*/
500 |
501 | if (mIndicatorHeight > 0) {
502 | mIndicatorDrawable.setColor(mIndicatorColor);
503 |
504 | if (mIndicatorGravity == Gravity.BOTTOM) {
505 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
506 | height - (int) mIndicatorHeight - (int) mIndicatorMarginBottom,
507 | paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight,
508 | height - (int) mIndicatorMarginBottom);
509 | } else {
510 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
511 | (int) mIndicatorMarginTop,
512 | paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight,
513 | (int) mIndicatorHeight + (int) mIndicatorMarginTop);
514 | }
515 | mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
516 | mIndicatorDrawable.draw(canvas);
517 | }
518 | }
519 | }
520 |
521 | //setter and getter
522 | public void setCurrentTab(int currentTab) {
523 | this.mCurrentTab = currentTab;
524 | mViewPager.setCurrentItem(currentTab);
525 |
526 | }
527 |
528 | public void setCurrentTab(int currentTab, boolean smoothScroll) {
529 | this.mCurrentTab = currentTab;
530 | mViewPager.setCurrentItem(currentTab, smoothScroll);
531 | }
532 |
533 | public void setIndicatorStyle(int indicatorStyle) {
534 | this.mIndicatorStyle = indicatorStyle;
535 | invalidate();
536 | }
537 |
538 | public void setTabPadding(float tabPadding) {
539 | this.mTabPadding = dp2px(tabPadding);
540 | updateTabStyles();
541 | }
542 |
543 | public void setTabSpaceEqual(boolean tabSpaceEqual) {
544 | this.mTabSpaceEqual = tabSpaceEqual;
545 | updateTabStyles();
546 | }
547 |
548 | public void setTabWidth(float tabWidth) {
549 | this.mTabWidth = dp2px(tabWidth);
550 | updateTabStyles();
551 | }
552 |
553 | public void setIndicatorColor(int indicatorColor) {
554 | this.mIndicatorColor = indicatorColor;
555 | invalidate();
556 | }
557 |
558 | public void setIndicatorHeight(float indicatorHeight) {
559 | this.mIndicatorHeight = dp2px(indicatorHeight);
560 | invalidate();
561 | }
562 |
563 | public void setIndicatorWidth(float indicatorWidth) {
564 | this.mIndicatorWidth = dp2px(indicatorWidth);
565 | invalidate();
566 | }
567 |
568 | public void setIndicatorCornerRadius(float indicatorCornerRadius) {
569 | this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius);
570 | invalidate();
571 | }
572 |
573 | public void setIndicatorGravity(int indicatorGravity) {
574 | this.mIndicatorGravity = indicatorGravity;
575 | invalidate();
576 | }
577 |
578 | public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop,
579 | float indicatorMarginRight, float indicatorMarginBottom) {
580 | this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft);
581 | this.mIndicatorMarginTop = dp2px(indicatorMarginTop);
582 | this.mIndicatorMarginRight = dp2px(indicatorMarginRight);
583 | this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom);
584 | invalidate();
585 | }
586 |
587 | public void setIndicatorWidthEqualTitle(boolean indicatorWidthEqualTitle) {
588 | this.mIndicatorWidthEqualTitle = indicatorWidthEqualTitle;
589 | invalidate();
590 | }
591 |
592 | public void setUnderlineColor(int underlineColor) {
593 | this.mUnderlineColor = underlineColor;
594 | invalidate();
595 | }
596 |
597 | public void setUnderlineHeight(float underlineHeight) {
598 | this.mUnderlineHeight = dp2px(underlineHeight);
599 | invalidate();
600 | }
601 |
602 | public void setUnderlineGravity(int underlineGravity) {
603 | this.mUnderlineGravity = underlineGravity;
604 | invalidate();
605 | }
606 |
607 | public void setDividerColor(int dividerColor) {
608 | this.mDividerColor = dividerColor;
609 | invalidate();
610 | }
611 |
612 | public void setDividerWidth(float dividerWidth) {
613 | this.mDividerWidth = dp2px(dividerWidth);
614 | invalidate();
615 | }
616 |
617 | public void setDividerPadding(float dividerPadding) {
618 | this.mDividerPadding = dp2px(dividerPadding);
619 | invalidate();
620 | }
621 |
622 | public void setTextsize(float textsize) {
623 | this.mTextsize = sp2px(textsize);
624 | updateTabStyles();
625 | }
626 |
627 | public void setTextSelectColor(int textSelectColor) {
628 | this.mTextSelectColor = textSelectColor;
629 | updateTabStyles();
630 | }
631 |
632 | public void setTextUnselectColor(int textUnselectColor) {
633 | this.mTextUnselectColor = textUnselectColor;
634 | updateTabStyles();
635 | }
636 |
637 | public void setTextBold(int textBold) {
638 | this.mTextBold = textBold;
639 | updateTabStyles();
640 | }
641 |
642 | public void setTextAllCaps(boolean textAllCaps) {
643 | this.mTextAllCaps = textAllCaps;
644 | updateTabStyles();
645 | }
646 |
647 |
648 | public int getTabCount() {
649 | return mTabCount;
650 | }
651 |
652 | public int getCurrentTab() {
653 | return mCurrentTab;
654 | }
655 |
656 | public int getIndicatorStyle() {
657 | return mIndicatorStyle;
658 | }
659 |
660 | public float getTabPadding() {
661 | return mTabPadding;
662 | }
663 |
664 | public boolean isTabSpaceEqual() {
665 | return mTabSpaceEqual;
666 | }
667 |
668 | public float getTabWidth() {
669 | return mTabWidth;
670 | }
671 |
672 | public int getIndicatorColor() {
673 | return mIndicatorColor;
674 | }
675 |
676 | public float getIndicatorHeight() {
677 | return mIndicatorHeight;
678 | }
679 |
680 | public float getIndicatorWidth() {
681 | return mIndicatorWidth;
682 | }
683 |
684 | public float getIndicatorCornerRadius() {
685 | return mIndicatorCornerRadius;
686 | }
687 |
688 | public float getIndicatorMarginLeft() {
689 | return mIndicatorMarginLeft;
690 | }
691 |
692 | public float getIndicatorMarginTop() {
693 | return mIndicatorMarginTop;
694 | }
695 |
696 | public float getIndicatorMarginRight() {
697 | return mIndicatorMarginRight;
698 | }
699 |
700 | public float getIndicatorMarginBottom() {
701 | return mIndicatorMarginBottom;
702 | }
703 |
704 | public int getUnderlineColor() {
705 | return mUnderlineColor;
706 | }
707 |
708 | public float getUnderlineHeight() {
709 | return mUnderlineHeight;
710 | }
711 |
712 | public int getDividerColor() {
713 | return mDividerColor;
714 | }
715 |
716 | public float getDividerWidth() {
717 | return mDividerWidth;
718 | }
719 |
720 | public float getDividerPadding() {
721 | return mDividerPadding;
722 | }
723 |
724 | public float getTextsize() {
725 | return mTextsize;
726 | }
727 |
728 | public int getTextSelectColor() {
729 | return mTextSelectColor;
730 | }
731 |
732 | public int getTextUnselectColor() {
733 | return mTextUnselectColor;
734 | }
735 |
736 | public int getTextBold() {
737 | return mTextBold;
738 | }
739 |
740 | public boolean isTextAllCaps() {
741 | return mTextAllCaps;
742 | }
743 |
744 | public TextView getTitleView(int tab) {
745 | View tabView = mTabsContainer.getChildAt(tab);
746 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
747 | return tv_tab_title;
748 | }
749 |
750 | //setter and getter
751 |
752 | // show MsgTipView
753 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
754 | private SparseArray mInitSetMap = new SparseArray<>();
755 |
756 | /**
757 | * 显示未读消息
758 | *
759 | * @param position 显示tab位置
760 | * @param num num小于等于0显示红点,num大于0显示数字
761 | */
762 | public void showMsg(int position, int num) {
763 | if (position >= mTabCount) {
764 | position = mTabCount - 1;
765 | }
766 |
767 | View tabView = mTabsContainer.getChildAt(position);
768 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
769 | if (tipView != null) {
770 | UnreadMsgUtils.show(tipView, num);
771 |
772 | if (mInitSetMap.get(position) != null && mInitSetMap.get(position)) {
773 | return;
774 | }
775 |
776 | setMsgMargin(position, 4, 2);
777 | mInitSetMap.put(position, true);
778 | }
779 | }
780 |
781 | /**
782 | * 显示未读红点
783 | *
784 | * @param position 显示tab位置
785 | */
786 | public void showDot(int position) {
787 | if (position >= mTabCount) {
788 | position = mTabCount - 1;
789 | }
790 | showMsg(position, 0);
791 | }
792 |
793 | /** 隐藏未读消息 */
794 | public void hideMsg(int position) {
795 | if (position >= mTabCount) {
796 | position = mTabCount - 1;
797 | }
798 |
799 | View tabView = mTabsContainer.getChildAt(position);
800 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
801 | if (tipView != null) {
802 | tipView.setVisibility(View.GONE);
803 | }
804 | }
805 |
806 | /** 设置未读消息偏移,原点为文字的右上角.当控件高度固定,消息提示位置易控制,显示效果佳 */
807 | public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
808 | if (position >= mTabCount) {
809 | position = mTabCount - 1;
810 | }
811 | View tabView = mTabsContainer.getChildAt(position);
812 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
813 | if (tipView != null) {
814 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
815 | mTextPaint.setTextSize(mTextsize);
816 | float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString());
817 | float textHeight = mTextPaint.descent() - mTextPaint.ascent();
818 | MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams();
819 | lp.leftMargin = mTabWidth >= 0 ? (int) (mTabWidth / 2 + textWidth / 2 + dp2px(leftPadding)) : (int) (mTabPadding + textWidth + dp2px(leftPadding));
820 | lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight) / 2 - dp2px(bottomPadding) : 0;
821 | tipView.setLayoutParams(lp);
822 | }
823 | }
824 |
825 | /** 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 */
826 | public MsgView getMsgView(int position) {
827 | if (position >= mTabCount) {
828 | position = mTabCount - 1;
829 | }
830 | View tabView = mTabsContainer.getChildAt(position);
831 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
832 | return tipView;
833 | }
834 |
835 | private OnTabSelectListener mListener;
836 |
837 | public void setOnTabSelectListener(OnTabSelectListener listener) {
838 | this.mListener = listener;
839 | }
840 |
841 | class InnerPagerAdapter extends FragmentPagerAdapter {
842 | private ArrayList fragments = new ArrayList<>();
843 | private String[] titles;
844 |
845 | public InnerPagerAdapter(FragmentManager fm, ArrayList fragments, String[] titles) {
846 | super(fm);
847 | this.fragments = fragments;
848 | this.titles = titles;
849 | }
850 |
851 | @Override
852 | public int getCount() {
853 | return fragments.size();
854 | }
855 |
856 | @Override
857 | public CharSequence getPageTitle(int position) {
858 | return titles[position];
859 | }
860 |
861 | @Override
862 | public Fragment getItem(int position) {
863 | return fragments.get(position);
864 | }
865 |
866 | @Override
867 | public void destroyItem(ViewGroup container, int position, Object object) {
868 | // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁
869 | // super.destroyItem(container, position, object);
870 | }
871 |
872 | @Override
873 | public int getItemPosition(Object object) {
874 | return PagerAdapter.POSITION_NONE;
875 | }
876 | }
877 |
878 | @Override
879 | protected Parcelable onSaveInstanceState() {
880 | Bundle bundle = new Bundle();
881 | bundle.putParcelable("instanceState", super.onSaveInstanceState());
882 | bundle.putInt("mCurrentTab", mCurrentTab);
883 | return bundle;
884 | }
885 |
886 | @Override
887 | protected void onRestoreInstanceState(Parcelable state) {
888 | if (state instanceof Bundle) {
889 | Bundle bundle = (Bundle) state;
890 | mCurrentTab = bundle.getInt("mCurrentTab");
891 | state = bundle.getParcelable("instanceState");
892 | if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) {
893 | updateTabSelection(mCurrentTab);
894 | scrollToCurrentTab();
895 | }
896 | }
897 | super.onRestoreInstanceState(state);
898 | }
899 |
900 | protected int dp2px(float dp) {
901 | final float scale = mContext.getResources().getDisplayMetrics().density;
902 | return (int) (dp * scale + 0.5f);
903 | }
904 |
905 | protected int sp2px(float sp) {
906 | final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity;
907 | return (int) (sp * scale + 0.5f);
908 | }
909 | }
910 |
--------------------------------------------------------------------------------
/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java:
--------------------------------------------------------------------------------
1 | package com.flyco.tablayout;
2 |
3 | import android.animation.TypeEvaluator;
4 | import android.animation.ValueAnimator;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.Paint;
10 | import android.graphics.Path;
11 | import android.graphics.Rect;
12 | import android.graphics.drawable.GradientDrawable;
13 | import android.os.Bundle;
14 | import android.os.Parcelable;
15 | import android.support.v4.app.Fragment;
16 | import android.support.v4.app.FragmentActivity;
17 | import android.util.AttributeSet;
18 | import android.util.SparseArray;
19 | import android.util.TypedValue;
20 | import android.view.Gravity;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 | import android.view.animation.OvershootInterpolator;
24 | import android.widget.FrameLayout;
25 | import android.widget.ImageView;
26 | import android.widget.LinearLayout;
27 | import android.widget.TextView;
28 |
29 | import com.flyco.tablayout.listener.CustomTabEntity;
30 | import com.flyco.tablayout.listener.OnTabSelectListener;
31 | import com.flyco.tablayout.utils.FragmentChangeManager;
32 | import com.flyco.tablayout.utils.UnreadMsgUtils;
33 | import com.flyco.tablayout.widget.MsgView;
34 |
35 | import java.util.ArrayList;
36 |
37 | /** 没有继承HorizontalScrollView不能滑动,对于ViewPager无依赖 */
38 | public class CommonTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {
39 | private Context mContext;
40 | private ArrayList mTabEntitys = new ArrayList<>();
41 | private LinearLayout mTabsContainer;
42 | private int mCurrentTab;
43 | private int mLastTab;
44 | private int mTabCount;
45 | /** 用于绘制显示器 */
46 | private Rect mIndicatorRect = new Rect();
47 | private GradientDrawable mIndicatorDrawable = new GradientDrawable();
48 |
49 | private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
50 | private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
51 | private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
52 | private Path mTrianglePath = new Path();
53 | private static final int STYLE_NORMAL = 0;
54 | private static final int STYLE_TRIANGLE = 1;
55 | private static final int STYLE_BLOCK = 2;
56 | private int mIndicatorStyle = STYLE_NORMAL;
57 |
58 | private float mTabPadding;
59 | private boolean mTabSpaceEqual;
60 | private float mTabWidth;
61 |
62 | /** indicator */
63 | private int mIndicatorColor;
64 | private float mIndicatorHeight;
65 | private float mIndicatorWidth;
66 | private float mIndicatorCornerRadius;
67 | private float mIndicatorMarginLeft;
68 | private float mIndicatorMarginTop;
69 | private float mIndicatorMarginRight;
70 | private float mIndicatorMarginBottom;
71 | private long mIndicatorAnimDuration;
72 | private boolean mIndicatorAnimEnable;
73 | private boolean mIndicatorBounceEnable;
74 | private int mIndicatorGravity;
75 |
76 | /** underline */
77 | private int mUnderlineColor;
78 | private float mUnderlineHeight;
79 | private int mUnderlineGravity;
80 |
81 | /** divider */
82 | private int mDividerColor;
83 | private float mDividerWidth;
84 | private float mDividerPadding;
85 |
86 | /** title */
87 | private static final int TEXT_BOLD_NONE = 0;
88 | private static final int TEXT_BOLD_WHEN_SELECT = 1;
89 | private static final int TEXT_BOLD_BOTH = 2;
90 | private float mTextsize;
91 | private int mTextSelectColor;
92 | private int mTextUnselectColor;
93 | private int mTextBold;
94 | private boolean mTextAllCaps;
95 |
96 | /** icon */
97 | private boolean mIconVisible;
98 | private int mIconGravity;
99 | private float mIconWidth;
100 | private float mIconHeight;
101 | private float mIconMargin;
102 |
103 | private int mHeight;
104 |
105 | /** anim */
106 | private ValueAnimator mValueAnimator;
107 | private OvershootInterpolator mInterpolator = new OvershootInterpolator(1.5f);
108 |
109 | private FragmentChangeManager mFragmentChangeManager;
110 |
111 | public CommonTabLayout(Context context) {
112 | this(context, null, 0);
113 | }
114 |
115 | public CommonTabLayout(Context context, AttributeSet attrs) {
116 | this(context, attrs, 0);
117 | }
118 |
119 | public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
120 | super(context, attrs, defStyleAttr);
121 | setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
122 | setClipChildren(false);
123 | setClipToPadding(false);
124 |
125 | this.mContext = context;
126 | mTabsContainer = new LinearLayout(context);
127 | addView(mTabsContainer);
128 |
129 | obtainAttributes(context, attrs);
130 |
131 | //get layout_height
132 | String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
133 |
134 | //create ViewPager
135 | if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
136 | } else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
137 | } else {
138 | int[] systemAttrs = {android.R.attr.layout_height};
139 | TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
140 | mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
141 | a.recycle();
142 | }
143 |
144 | mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);
145 | mValueAnimator.addUpdateListener(this);
146 | }
147 |
148 | private void obtainAttributes(Context context, AttributeSet attrs) {
149 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonTabLayout);
150 |
151 | mIndicatorStyle = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_style, 0);
152 | mIndicatorColor = ta.getColor(R.styleable.CommonTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? "#4B6A87" : "#ffffff"));
153 | mIndicatorHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_height,
154 | dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2)));
155 | mIndicatorWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1));
156 | mIndicatorCornerRadius = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_corner_radius, dp2px(mIndicatorStyle == STYLE_BLOCK ? -1 : 0));
157 | mIndicatorMarginLeft = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_left, dp2px(0));
158 | mIndicatorMarginTop = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_top, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0));
159 | mIndicatorMarginRight = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_right, dp2px(0));
160 | mIndicatorMarginBottom = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_margin_bottom, dp2px(mIndicatorStyle == STYLE_BLOCK ? 7 : 0));
161 | mIndicatorAnimEnable = ta.getBoolean(R.styleable.CommonTabLayout_tl_indicator_anim_enable, true);
162 | mIndicatorBounceEnable = ta.getBoolean(R.styleable.CommonTabLayout_tl_indicator_bounce_enable, true);
163 | mIndicatorAnimDuration = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_anim_duration, -1);
164 | mIndicatorGravity = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_gravity, Gravity.BOTTOM);
165 |
166 | mUnderlineColor = ta.getColor(R.styleable.CommonTabLayout_tl_underline_color, Color.parseColor("#ffffff"));
167 | mUnderlineHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_underline_height, dp2px(0));
168 | mUnderlineGravity = ta.getInt(R.styleable.CommonTabLayout_tl_underline_gravity, Gravity.BOTTOM);
169 |
170 | mDividerColor = ta.getColor(R.styleable.CommonTabLayout_tl_divider_color, Color.parseColor("#ffffff"));
171 | mDividerWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_divider_width, dp2px(0));
172 | mDividerPadding = ta.getDimension(R.styleable.CommonTabLayout_tl_divider_padding, dp2px(12));
173 |
174 | mTextsize = ta.getDimension(R.styleable.CommonTabLayout_tl_textsize, sp2px(13f));
175 | mTextSelectColor = ta.getColor(R.styleable.CommonTabLayout_tl_textSelectColor, Color.parseColor("#ffffff"));
176 | mTextUnselectColor = ta.getColor(R.styleable.CommonTabLayout_tl_textUnselectColor, Color.parseColor("#AAffffff"));
177 | mTextBold = ta.getInt(R.styleable.CommonTabLayout_tl_textBold, TEXT_BOLD_NONE);
178 | mTextAllCaps = ta.getBoolean(R.styleable.CommonTabLayout_tl_textAllCaps, false);
179 |
180 | mIconVisible = ta.getBoolean(R.styleable.CommonTabLayout_tl_iconVisible, true);
181 | mIconGravity = ta.getInt(R.styleable.CommonTabLayout_tl_iconGravity, Gravity.TOP);
182 | mIconWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_iconWidth, dp2px(0));
183 | mIconHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_iconHeight, dp2px(0));
184 | mIconMargin = ta.getDimension(R.styleable.CommonTabLayout_tl_iconMargin, dp2px(2.5f));
185 |
186 | mTabSpaceEqual = ta.getBoolean(R.styleable.CommonTabLayout_tl_tab_space_equal, true);
187 | mTabWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_tab_width, dp2px(-1));
188 | mTabPadding = ta.getDimension(R.styleable.CommonTabLayout_tl_tab_padding, mTabSpaceEqual || mTabWidth > 0 ? dp2px(0) : dp2px(10));
189 |
190 | ta.recycle();
191 | }
192 |
193 | public void setTabData(ArrayList tabEntitys) {
194 | if (tabEntitys == null || tabEntitys.size() == 0) {
195 | throw new IllegalStateException("TabEntitys can not be NULL or EMPTY !");
196 | }
197 |
198 | this.mTabEntitys.clear();
199 | this.mTabEntitys.addAll(tabEntitys);
200 |
201 | notifyDataSetChanged();
202 | }
203 |
204 | /** 关联数据支持同时切换fragments */
205 | public void setTabData(ArrayList tabEntitys, FragmentActivity fa, int containerViewId, ArrayList fragments) {
206 | mFragmentChangeManager = new FragmentChangeManager(fa.getSupportFragmentManager(), containerViewId, fragments);
207 | setTabData(tabEntitys);
208 | }
209 |
210 | /** 更新数据 */
211 | public void notifyDataSetChanged() {
212 | mTabsContainer.removeAllViews();
213 | this.mTabCount = mTabEntitys.size();
214 | View tabView;
215 | for (int i = 0; i < mTabCount; i++) {
216 | if (mIconGravity == Gravity.LEFT) {
217 | tabView = View.inflate(mContext, R.layout.layout_tab_left, null);
218 | } else if (mIconGravity == Gravity.RIGHT) {
219 | tabView = View.inflate(mContext, R.layout.layout_tab_right, null);
220 | } else if (mIconGravity == Gravity.BOTTOM) {
221 | tabView = View.inflate(mContext, R.layout.layout_tab_bottom, null);
222 | } else {
223 | tabView = View.inflate(mContext, R.layout.layout_tab_top, null);
224 | }
225 |
226 | tabView.setTag(i);
227 | addTab(i, tabView);
228 | }
229 |
230 | updateTabStyles();
231 | }
232 |
233 | /** 创建并添加tab */
234 | private void addTab(final int position, View tabView) {
235 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
236 | tv_tab_title.setText(mTabEntitys.get(position).getTabTitle());
237 | ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
238 | iv_tab_icon.setImageResource(mTabEntitys.get(position).getTabUnselectedIcon());
239 |
240 | tabView.setOnClickListener(new OnClickListener() {
241 | @Override
242 | public void onClick(View v) {
243 | int position = (Integer) v.getTag();
244 | if (mCurrentTab != position) {
245 | setCurrentTab(position);
246 | if (mListener != null) {
247 | mListener.onTabSelect(position);
248 | }
249 | } else {
250 | if (mListener != null) {
251 | mListener.onTabReselect(position);
252 | }
253 | }
254 | }
255 | });
256 |
257 | /** 每一个Tab的布局参数 */
258 | LinearLayout.LayoutParams lp_tab = mTabSpaceEqual ?
259 | new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) :
260 | new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
261 | if (mTabWidth > 0) {
262 | lp_tab = new LinearLayout.LayoutParams((int) mTabWidth, LayoutParams.MATCH_PARENT);
263 | }
264 | mTabsContainer.addView(tabView, position, lp_tab);
265 | }
266 |
267 | private void updateTabStyles() {
268 | for (int i = 0; i < mTabCount; i++) {
269 | View tabView = mTabsContainer.getChildAt(i);
270 | tabView.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
271 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
272 | tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor);
273 | tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize);
274 | // tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
275 | if (mTextAllCaps) {
276 | tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase());
277 | }
278 |
279 | if (mTextBold == TEXT_BOLD_BOTH) {
280 | tv_tab_title.getPaint().setFakeBoldText(true);
281 | } else if (mTextBold == TEXT_BOLD_NONE) {
282 | tv_tab_title.getPaint().setFakeBoldText(false);
283 | }
284 |
285 | ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
286 | if (mIconVisible) {
287 | iv_tab_icon.setVisibility(View.VISIBLE);
288 | CustomTabEntity tabEntity = mTabEntitys.get(i);
289 | iv_tab_icon.setImageResource(i == mCurrentTab ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
290 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
291 | mIconWidth <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconWidth,
292 | mIconHeight <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconHeight);
293 | if (mIconGravity == Gravity.LEFT) {
294 | lp.rightMargin = (int) mIconMargin;
295 | } else if (mIconGravity == Gravity.RIGHT) {
296 | lp.leftMargin = (int) mIconMargin;
297 | } else if (mIconGravity == Gravity.BOTTOM) {
298 | lp.topMargin = (int) mIconMargin;
299 | } else {
300 | lp.bottomMargin = (int) mIconMargin;
301 | }
302 |
303 | iv_tab_icon.setLayoutParams(lp);
304 | } else {
305 | iv_tab_icon.setVisibility(View.GONE);
306 | }
307 | }
308 | }
309 |
310 | private void updateTabSelection(int position) {
311 | for (int i = 0; i < mTabCount; ++i) {
312 | View tabView = mTabsContainer.getChildAt(i);
313 | final boolean isSelect = i == position;
314 | TextView tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
315 | tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor);
316 | ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
317 | CustomTabEntity tabEntity = mTabEntitys.get(i);
318 | iv_tab_icon.setImageResource(isSelect ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
319 | if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
320 | tab_title.getPaint().setFakeBoldText(isSelect);
321 | }
322 | }
323 | }
324 |
325 | private void calcOffset() {
326 | final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
327 | mCurrentP.left = currentTabView.getLeft();
328 | mCurrentP.right = currentTabView.getRight();
329 |
330 | final View lastTabView = mTabsContainer.getChildAt(this.mLastTab);
331 | mLastP.left = lastTabView.getLeft();
332 | mLastP.right = lastTabView.getRight();
333 |
334 | // Log.d("AAA", "mLastP--->" + mLastP.left + "&" + mLastP.right);
335 | // Log.d("AAA", "mCurrentP--->" + mCurrentP.left + "&" + mCurrentP.right);
336 | if (mLastP.left == mCurrentP.left && mLastP.right == mCurrentP.right) {
337 | invalidate();
338 | } else {
339 | mValueAnimator.setObjectValues(mLastP, mCurrentP);
340 | if (mIndicatorBounceEnable) {
341 | mValueAnimator.setInterpolator(mInterpolator);
342 | }
343 |
344 | if (mIndicatorAnimDuration < 0) {
345 | mIndicatorAnimDuration = mIndicatorBounceEnable ? 500 : 250;
346 | }
347 | mValueAnimator.setDuration(mIndicatorAnimDuration);
348 | mValueAnimator.start();
349 | }
350 | }
351 |
352 | private void calcIndicatorRect() {
353 | View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
354 | float left = currentTabView.getLeft();
355 | float right = currentTabView.getRight();
356 |
357 | mIndicatorRect.left = (int) left;
358 | mIndicatorRect.right = (int) right;
359 |
360 | if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip
361 |
362 | } else {//indicatorWidth大于0时,圆角矩形以及三角形
363 | float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - mIndicatorWidth) / 2;
364 |
365 | mIndicatorRect.left = (int) indicatorLeft;
366 | mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth);
367 | }
368 | }
369 |
370 | @Override
371 | public void onAnimationUpdate(ValueAnimator animation) {
372 | View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
373 | IndicatorPoint p = (IndicatorPoint) animation.getAnimatedValue();
374 | mIndicatorRect.left = (int) p.left;
375 | mIndicatorRect.right = (int) p.right;
376 |
377 | if (mIndicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip
378 |
379 | } else {//indicatorWidth大于0时,圆角矩形以及三角形
380 | float indicatorLeft = p.left + (currentTabView.getWidth() - mIndicatorWidth) / 2;
381 |
382 | mIndicatorRect.left = (int) indicatorLeft;
383 | mIndicatorRect.right = (int) (mIndicatorRect.left + mIndicatorWidth);
384 | }
385 | invalidate();
386 | }
387 |
388 | private boolean mIsFirstDraw = true;
389 |
390 | @Override
391 | protected void onDraw(Canvas canvas) {
392 | super.onDraw(canvas);
393 |
394 | if (isInEditMode() || mTabCount <= 0) {
395 | return;
396 | }
397 |
398 | int height = getHeight();
399 | int paddingLeft = getPaddingLeft();
400 | // draw divider
401 | if (mDividerWidth > 0) {
402 | mDividerPaint.setStrokeWidth(mDividerWidth);
403 | mDividerPaint.setColor(mDividerColor);
404 | for (int i = 0; i < mTabCount - 1; i++) {
405 | View tab = mTabsContainer.getChildAt(i);
406 | canvas.drawLine(paddingLeft + tab.getRight(), mDividerPadding, paddingLeft + tab.getRight(), height - mDividerPadding, mDividerPaint);
407 | }
408 | }
409 |
410 | // draw underline
411 | if (mUnderlineHeight > 0) {
412 | mRectPaint.setColor(mUnderlineColor);
413 | if (mUnderlineGravity == Gravity.BOTTOM) {
414 | canvas.drawRect(paddingLeft, height - mUnderlineHeight, mTabsContainer.getWidth() + paddingLeft, height, mRectPaint);
415 | } else {
416 | canvas.drawRect(paddingLeft, 0, mTabsContainer.getWidth() + paddingLeft, mUnderlineHeight, mRectPaint);
417 | }
418 | }
419 |
420 | //draw indicator line
421 | if (mIndicatorAnimEnable) {
422 | if (mIsFirstDraw) {
423 | mIsFirstDraw = false;
424 | calcIndicatorRect();
425 | }
426 | } else {
427 | calcIndicatorRect();
428 | }
429 |
430 |
431 | if (mIndicatorStyle == STYLE_TRIANGLE) {
432 | if (mIndicatorHeight > 0) {
433 | mTrianglePaint.setColor(mIndicatorColor);
434 | mTrianglePath.reset();
435 | mTrianglePath.moveTo(paddingLeft + mIndicatorRect.left, height);
436 | mTrianglePath.lineTo(paddingLeft + mIndicatorRect.left / 2 + mIndicatorRect.right / 2, height - mIndicatorHeight);
437 | mTrianglePath.lineTo(paddingLeft + mIndicatorRect.right, height);
438 | mTrianglePath.close();
439 | canvas.drawPath(mTrianglePath, mTrianglePaint);
440 | }
441 | } else if (mIndicatorStyle == STYLE_BLOCK) {
442 | if (mIndicatorHeight < 0) {
443 | mIndicatorHeight = height - mIndicatorMarginTop - mIndicatorMarginBottom;
444 | } else {
445 |
446 | }
447 |
448 | if (mIndicatorHeight > 0) {
449 | if (mIndicatorCornerRadius < 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) {
450 | mIndicatorCornerRadius = mIndicatorHeight / 2;
451 | }
452 |
453 | mIndicatorDrawable.setColor(mIndicatorColor);
454 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
455 | (int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight),
456 | (int) (mIndicatorMarginTop + mIndicatorHeight));
457 | mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
458 | mIndicatorDrawable.draw(canvas);
459 | }
460 | } else {
461 | /* mRectPaint.setColor(mIndicatorColor);
462 | calcIndicatorRect();
463 | canvas.drawRect(getPaddingLeft() + mIndicatorRect.left, getHeight() - mIndicatorHeight,
464 | mIndicatorRect.right + getPaddingLeft(), getHeight(), mRectPaint);*/
465 |
466 | if (mIndicatorHeight > 0) {
467 | mIndicatorDrawable.setColor(mIndicatorColor);
468 | if (mIndicatorGravity == Gravity.BOTTOM) {
469 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
470 | height - (int) mIndicatorHeight - (int) mIndicatorMarginBottom,
471 | paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight,
472 | height - (int) mIndicatorMarginBottom);
473 | } else {
474 | mIndicatorDrawable.setBounds(paddingLeft + (int) mIndicatorMarginLeft + mIndicatorRect.left,
475 | (int) mIndicatorMarginTop,
476 | paddingLeft + mIndicatorRect.right - (int) mIndicatorMarginRight,
477 | (int) mIndicatorHeight + (int) mIndicatorMarginTop);
478 | }
479 | mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
480 | mIndicatorDrawable.draw(canvas);
481 | }
482 | }
483 | }
484 |
485 | //setter and getter
486 | public void setCurrentTab(int currentTab) {
487 | mLastTab = this.mCurrentTab;
488 | this.mCurrentTab = currentTab;
489 | updateTabSelection(currentTab);
490 | if (mFragmentChangeManager != null) {
491 | mFragmentChangeManager.setFragments(currentTab);
492 | }
493 | if (mIndicatorAnimEnable) {
494 | calcOffset();
495 | } else {
496 | invalidate();
497 | }
498 | }
499 |
500 | public void setIndicatorStyle(int indicatorStyle) {
501 | this.mIndicatorStyle = indicatorStyle;
502 | invalidate();
503 | }
504 |
505 | public void setTabPadding(float tabPadding) {
506 | this.mTabPadding = dp2px(tabPadding);
507 | updateTabStyles();
508 | }
509 |
510 | public void setTabSpaceEqual(boolean tabSpaceEqual) {
511 | this.mTabSpaceEqual = tabSpaceEqual;
512 | updateTabStyles();
513 | }
514 |
515 | public void setTabWidth(float tabWidth) {
516 | this.mTabWidth = dp2px(tabWidth);
517 | updateTabStyles();
518 | }
519 |
520 | public void setIndicatorColor(int indicatorColor) {
521 | this.mIndicatorColor = indicatorColor;
522 | invalidate();
523 | }
524 |
525 | public void setIndicatorHeight(float indicatorHeight) {
526 | this.mIndicatorHeight = dp2px(indicatorHeight);
527 | invalidate();
528 | }
529 |
530 | public void setIndicatorWidth(float indicatorWidth) {
531 | this.mIndicatorWidth = dp2px(indicatorWidth);
532 | invalidate();
533 | }
534 |
535 | public void setIndicatorCornerRadius(float indicatorCornerRadius) {
536 | this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius);
537 | invalidate();
538 | }
539 |
540 | public void setIndicatorGravity(int indicatorGravity) {
541 | this.mIndicatorGravity = indicatorGravity;
542 | invalidate();
543 | }
544 |
545 | public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginTop,
546 | float indicatorMarginRight, float indicatorMarginBottom) {
547 | this.mIndicatorMarginLeft = dp2px(indicatorMarginLeft);
548 | this.mIndicatorMarginTop = dp2px(indicatorMarginTop);
549 | this.mIndicatorMarginRight = dp2px(indicatorMarginRight);
550 | this.mIndicatorMarginBottom = dp2px(indicatorMarginBottom);
551 | invalidate();
552 | }
553 |
554 | public void setIndicatorAnimDuration(long indicatorAnimDuration) {
555 | this.mIndicatorAnimDuration = indicatorAnimDuration;
556 | }
557 |
558 | public void setIndicatorAnimEnable(boolean indicatorAnimEnable) {
559 | this.mIndicatorAnimEnable = indicatorAnimEnable;
560 | }
561 |
562 | public void setIndicatorBounceEnable(boolean indicatorBounceEnable) {
563 | this.mIndicatorBounceEnable = indicatorBounceEnable;
564 | }
565 |
566 | public void setUnderlineColor(int underlineColor) {
567 | this.mUnderlineColor = underlineColor;
568 | invalidate();
569 | }
570 |
571 | public void setUnderlineHeight(float underlineHeight) {
572 | this.mUnderlineHeight = dp2px(underlineHeight);
573 | invalidate();
574 | }
575 |
576 | public void setUnderlineGravity(int underlineGravity) {
577 | this.mUnderlineGravity = underlineGravity;
578 | invalidate();
579 | }
580 |
581 | public void setDividerColor(int dividerColor) {
582 | this.mDividerColor = dividerColor;
583 | invalidate();
584 | }
585 |
586 | public void setDividerWidth(float dividerWidth) {
587 | this.mDividerWidth = dp2px(dividerWidth);
588 | invalidate();
589 | }
590 |
591 | public void setDividerPadding(float dividerPadding) {
592 | this.mDividerPadding = dp2px(dividerPadding);
593 | invalidate();
594 | }
595 |
596 | public void setTextsize(float textsize) {
597 | this.mTextsize = sp2px(textsize);
598 | updateTabStyles();
599 | }
600 |
601 | public void setTextSelectColor(int textSelectColor) {
602 | this.mTextSelectColor = textSelectColor;
603 | updateTabStyles();
604 | }
605 |
606 | public void setTextUnselectColor(int textUnselectColor) {
607 | this.mTextUnselectColor = textUnselectColor;
608 | updateTabStyles();
609 | }
610 |
611 | public void setTextBold(int textBold) {
612 | this.mTextBold = textBold;
613 | updateTabStyles();
614 | }
615 |
616 | public void setIconVisible(boolean iconVisible) {
617 | this.mIconVisible = iconVisible;
618 | updateTabStyles();
619 | }
620 |
621 | public void setIconGravity(int iconGravity) {
622 | this.mIconGravity = iconGravity;
623 | notifyDataSetChanged();
624 | }
625 |
626 | public void setIconWidth(float iconWidth) {
627 | this.mIconWidth = dp2px(iconWidth);
628 | updateTabStyles();
629 | }
630 |
631 | public void setIconHeight(float iconHeight) {
632 | this.mIconHeight = dp2px(iconHeight);
633 | updateTabStyles();
634 | }
635 |
636 | public void setIconMargin(float iconMargin) {
637 | this.mIconMargin = dp2px(iconMargin);
638 | updateTabStyles();
639 | }
640 |
641 | public void setTextAllCaps(boolean textAllCaps) {
642 | this.mTextAllCaps = textAllCaps;
643 | updateTabStyles();
644 | }
645 |
646 |
647 | public int getTabCount() {
648 | return mTabCount;
649 | }
650 |
651 | public int getCurrentTab() {
652 | return mCurrentTab;
653 | }
654 |
655 | public int getIndicatorStyle() {
656 | return mIndicatorStyle;
657 | }
658 |
659 | public float getTabPadding() {
660 | return mTabPadding;
661 | }
662 |
663 | public boolean isTabSpaceEqual() {
664 | return mTabSpaceEqual;
665 | }
666 |
667 | public float getTabWidth() {
668 | return mTabWidth;
669 | }
670 |
671 | public int getIndicatorColor() {
672 | return mIndicatorColor;
673 | }
674 |
675 | public float getIndicatorHeight() {
676 | return mIndicatorHeight;
677 | }
678 |
679 | public float getIndicatorWidth() {
680 | return mIndicatorWidth;
681 | }
682 |
683 | public float getIndicatorCornerRadius() {
684 | return mIndicatorCornerRadius;
685 | }
686 |
687 | public float getIndicatorMarginLeft() {
688 | return mIndicatorMarginLeft;
689 | }
690 |
691 | public float getIndicatorMarginTop() {
692 | return mIndicatorMarginTop;
693 | }
694 |
695 | public float getIndicatorMarginRight() {
696 | return mIndicatorMarginRight;
697 | }
698 |
699 | public float getIndicatorMarginBottom() {
700 | return mIndicatorMarginBottom;
701 | }
702 |
703 | public long getIndicatorAnimDuration() {
704 | return mIndicatorAnimDuration;
705 | }
706 |
707 | public boolean isIndicatorAnimEnable() {
708 | return mIndicatorAnimEnable;
709 | }
710 |
711 | public boolean isIndicatorBounceEnable() {
712 | return mIndicatorBounceEnable;
713 | }
714 |
715 | public int getUnderlineColor() {
716 | return mUnderlineColor;
717 | }
718 |
719 | public float getUnderlineHeight() {
720 | return mUnderlineHeight;
721 | }
722 |
723 | public int getDividerColor() {
724 | return mDividerColor;
725 | }
726 |
727 | public float getDividerWidth() {
728 | return mDividerWidth;
729 | }
730 |
731 | public float getDividerPadding() {
732 | return mDividerPadding;
733 | }
734 |
735 | public float getTextsize() {
736 | return mTextsize;
737 | }
738 |
739 | public int getTextSelectColor() {
740 | return mTextSelectColor;
741 | }
742 |
743 | public int getTextUnselectColor() {
744 | return mTextUnselectColor;
745 | }
746 |
747 | public int getTextBold() {
748 | return mTextBold;
749 | }
750 |
751 | public boolean isTextAllCaps() {
752 | return mTextAllCaps;
753 | }
754 |
755 | public int getIconGravity() {
756 | return mIconGravity;
757 | }
758 |
759 | public float getIconWidth() {
760 | return mIconWidth;
761 | }
762 |
763 | public float getIconHeight() {
764 | return mIconHeight;
765 | }
766 |
767 | public float getIconMargin() {
768 | return mIconMargin;
769 | }
770 |
771 | public boolean isIconVisible() {
772 | return mIconVisible;
773 | }
774 |
775 |
776 | public ImageView getIconView(int tab) {
777 | View tabView = mTabsContainer.getChildAt(tab);
778 | ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
779 | return iv_tab_icon;
780 | }
781 |
782 | public TextView getTitleView(int tab) {
783 | View tabView = mTabsContainer.getChildAt(tab);
784 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
785 | return tv_tab_title;
786 | }
787 |
788 | //setter and getter
789 |
790 | // show MsgTipView
791 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
792 | private SparseArray mInitSetMap = new SparseArray<>();
793 |
794 | /**
795 | * 显示未读消息
796 | *
797 | * @param position 显示tab位置
798 | * @param num num小于等于0显示红点,num大于0显示数字
799 | */
800 | public void showMsg(int position, int num) {
801 | if (position >= mTabCount) {
802 | position = mTabCount - 1;
803 | }
804 |
805 | View tabView = mTabsContainer.getChildAt(position);
806 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
807 | if (tipView != null) {
808 | UnreadMsgUtils.show(tipView, num);
809 |
810 | if (mInitSetMap.get(position) != null && mInitSetMap.get(position)) {
811 | return;
812 | }
813 |
814 | if (!mIconVisible) {
815 | setMsgMargin(position, 2, 2);
816 | } else {
817 | setMsgMargin(position, 0,
818 | mIconGravity == Gravity.LEFT || mIconGravity == Gravity.RIGHT ? 4 : 0);
819 | }
820 |
821 | mInitSetMap.put(position, true);
822 | }
823 | }
824 |
825 | /**
826 | * 显示未读红点
827 | *
828 | * @param position 显示tab位置
829 | */
830 | public void showDot(int position) {
831 | if (position >= mTabCount) {
832 | position = mTabCount - 1;
833 | }
834 | showMsg(position, 0);
835 | }
836 |
837 | public void hideMsg(int position) {
838 | if (position >= mTabCount) {
839 | position = mTabCount - 1;
840 | }
841 |
842 | View tabView = mTabsContainer.getChildAt(position);
843 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
844 | if (tipView != null) {
845 | tipView.setVisibility(View.GONE);
846 | }
847 | }
848 |
849 | /**
850 | * 设置提示红点偏移,注意
851 | * 1.控件为固定高度:参照点为tab内容的右上角
852 | * 2.控件高度不固定(WRAP_CONTENT):参照点为tab内容的右上角,此时高度已是红点的最高显示范围,所以这时bottomPadding其实就是topPadding
853 | */
854 | public void setMsgMargin(int position, float leftPadding, float bottomPadding) {
855 | if (position >= mTabCount) {
856 | position = mTabCount - 1;
857 | }
858 | View tabView = mTabsContainer.getChildAt(position);
859 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
860 | if (tipView != null) {
861 | TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
862 | mTextPaint.setTextSize(mTextsize);
863 | float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString());
864 | float textHeight = mTextPaint.descent() - mTextPaint.ascent();
865 | MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams();
866 |
867 | float iconH = mIconHeight;
868 | float margin = 0;
869 | if (mIconVisible) {
870 | if (iconH <= 0) {
871 | iconH = mContext.getResources().getDrawable(mTabEntitys.get(position).getTabSelectedIcon()).getIntrinsicHeight();
872 | }
873 | margin = mIconMargin;
874 | }
875 |
876 | if (mIconGravity == Gravity.TOP || mIconGravity == Gravity.BOTTOM) {
877 | lp.leftMargin = dp2px(leftPadding);
878 | lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight - iconH - margin) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding);
879 | } else {
880 | lp.leftMargin = dp2px(leftPadding);
881 | lp.topMargin = mHeight > 0 ? (int) (mHeight - Math.max(textHeight, iconH)) / 2 - dp2px(bottomPadding) : dp2px(bottomPadding);
882 | }
883 |
884 | tipView.setLayoutParams(lp);
885 | }
886 | }
887 |
888 | /** 当前类只提供了少许设置未读消息属性的方法,可以通过该方法获取MsgView对象从而各种设置 */
889 | public MsgView getMsgView(int position) {
890 | if (position >= mTabCount) {
891 | position = mTabCount - 1;
892 | }
893 | View tabView = mTabsContainer.getChildAt(position);
894 | MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip);
895 | return tipView;
896 | }
897 |
898 | private OnTabSelectListener mListener;
899 |
900 | public void setOnTabSelectListener(OnTabSelectListener listener) {
901 | this.mListener = listener;
902 | }
903 |
904 |
905 | @Override
906 | protected Parcelable onSaveInstanceState() {
907 | Bundle bundle = new Bundle();
908 | bundle.putParcelable("instanceState", super.onSaveInstanceState());
909 | bundle.putInt("mCurrentTab", mCurrentTab);
910 | return bundle;
911 | }
912 |
913 | @Override
914 | protected void onRestoreInstanceState(Parcelable state) {
915 | if (state instanceof Bundle) {
916 | Bundle bundle = (Bundle) state;
917 | mCurrentTab = bundle.getInt("mCurrentTab");
918 | state = bundle.getParcelable("instanceState");
919 | if (mCurrentTab != 0 && mTabsContainer.getChildCount() > 0) {
920 | updateTabSelection(mCurrentTab);
921 | }
922 | }
923 | super.onRestoreInstanceState(state);
924 | }
925 |
926 | class IndicatorPoint {
927 | public float left;
928 | public float right;
929 | }
930 |
931 | private IndicatorPoint mCurrentP = new IndicatorPoint();
932 | private IndicatorPoint mLastP = new IndicatorPoint();
933 |
934 | class PointEvaluator implements TypeEvaluator {
935 | @Override
936 | public IndicatorPoint evaluate(float fraction, IndicatorPoint startValue, IndicatorPoint endValue) {
937 | float left = startValue.left + fraction * (endValue.left - startValue.left);
938 | float right = startValue.right + fraction * (endValue.right - startValue.right);
939 | IndicatorPoint point = new IndicatorPoint();
940 | point.left = left;
941 | point.right = right;
942 | return point;
943 | }
944 | }
945 |
946 |
947 | protected int dp2px(float dp) {
948 | final float scale = mContext.getResources().getDisplayMetrics().density;
949 | return (int) (dp * scale + 0.5f);
950 | }
951 |
952 | protected int sp2px(float sp) {
953 | final float scale = this.mContext.getResources().getDisplayMetrics().scaledDensity;
954 | return (int) (sp * scale + 0.5f);
955 | }
956 |
957 | }
958 |
--------------------------------------------------------------------------------