From 19278dd46e5d75a31064b4d56acba4d0a1adddc0 Mon Sep 17 00:00:00 2001 From: H07000223 <867318349@qq.com> Date: Sat, 21 May 2016 16:28:41 +0800 Subject: [PATCH] remove CustomTabProvider in SlidingTabLayout new added method 'addNewTab(String title)' for SlidingTabLayout --- CHNAGELOG.md | 5 ++ FlycoTabLayout_Lib/build.gradle | 10 +-- .../com/flyco/tablayout/SlidingTabLayout.java | 65 +++++++++---------- README.md | 2 +- README_CN.md | 2 +- app/build.gradle | 6 +- .../ui/SlidingTabActivity.java | 24 +++++-- build.gradle | 2 +- 8 files changed, 67 insertions(+), 49 deletions(-) diff --git a/CHNAGELOG.md b/CHNAGELOG.md index 7c8be9a2..9e02bb2a 100644 --- a/CHNAGELOG.md +++ b/CHNAGELOG.md @@ -66,3 +66,8 @@ Version 2.0.2 *(2016.4.23)* ---------------------------- * remove the dependence of NineOldAnimation(only support 3.0+) +Version 2.0.6 *(2016.5.21)* +---------------------------- +* remove CustomTabProvider in SlidingTabLayout +* new added method 'addNewTab(String title)' for SlidingTabLayout + diff --git a/FlycoTabLayout_Lib/build.gradle b/FlycoTabLayout_Lib/build.gradle index 4acd42e3..41be4abf 100644 --- a/FlycoTabLayout_Lib/build.gradle +++ b/FlycoTabLayout_Lib/build.gradle @@ -3,15 +3,15 @@ apply plugin: 'com.android.library' //apply plugin: 'com.jfrog.bintray' // 这个version是区分library版本的,因此当我们需要更新library时记得修改这个version -version = "2.0.2" +version = "2.0.6" android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 11 targetSdkVersion 23 - versionCode 202 + versionCode 206 versionName version } buildTypes { @@ -24,8 +24,8 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:support-v4:23.3.0' -// compile 'com.android.support:appcompat-v7:23.3.0' + compile 'com.android.support:support-v4:23.4.0' +// compile 'com.android.support:appcompat-v7:23.4.0' // compile 'com.nineoldandroids:library:2.4.0' } diff --git a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java index a69f2768..a2924f37 100644 --- a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java +++ b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java @@ -31,12 +31,13 @@ import com.flyco.tablayout.widget.MsgView; import java.util.ArrayList; +import java.util.Collections; /** 滑动TabLayout,对于ViewPager的依赖性强 */ public class SlidingTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener { private Context mContext; private ViewPager mViewPager; - private String[] mTitles; + private ArrayList mTitles; private LinearLayout mTabsContainer; private int mCurrentTab; private float mCurrentPositionOffset; @@ -192,7 +193,8 @@ public void setViewPager(ViewPager vp, String[] titles) { } this.mViewPager = vp; - this.mTitles = titles; + mTitles = new ArrayList<>(); + Collections.addAll(mTitles, titles); this.mViewPager.removeOnPageChangeListener(this); this.mViewPager.addOnPageChangeListener(this); @@ -220,22 +222,30 @@ public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, Arr /** 更新数据 */ public void notifyDataSetChanged() { mTabsContainer.removeAllViews(); - this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.length; + this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); View tabView; for (int i = 0; i < mTabCount; i++) { - if (mViewPager.getAdapter() instanceof CustomTabProvider) { - tabView = ((CustomTabProvider) mViewPager.getAdapter()).getCustomTabView(this, i); - } else { - tabView = View.inflate(mContext, R.layout.layout_tab, null); - } - - CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles[i]; + tabView = View.inflate(mContext, R.layout.layout_tab, null); + CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles.get(i); addTab(i, pageTitle.toString(), tabView); } updateTabStyles(); } + public void addNewTab(String title) { + View tabView = View.inflate(mContext, R.layout.layout_tab, null); + if (mTitles != null) { + mTitles.add(title); + } + + CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(mTabCount) : mTitles.get(mTabCount); + addTab(mTabCount, pageTitle.toString(), tabView); + this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); + + updateTabStyles(); + } + /** 创建并添加tab */ private void addTab(final int position, String title, View tabView) { TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); @@ -246,14 +256,17 @@ private void addTab(final int position, String title, View tabView) { tabView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - if (mViewPager.getCurrentItem() != position) { - mViewPager.setCurrentItem(position); - if (mListener != null) { - mListener.onTabSelect(position); - } - } else { - if (mListener != null) { - mListener.onTabReselect(position); + int position = mTabsContainer.indexOfChild(v); + if (position != -1) { + if (mViewPager.getCurrentItem() != position) { + mViewPager.setCurrentItem(position); + if (mListener != null) { + mListener.onTabSelect(position); + } + } else { + if (mListener != null) { + mListener.onTabReselect(position); + } } } } @@ -347,14 +360,6 @@ private void updateTabSelection(int position) { if (tab_title != null) { tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor); } - - if (mViewPager.getAdapter() instanceof CustomTabProvider) { - if (isSelect) { - ((CustomTabProvider) mViewPager.getAdapter()).tabSelect(tabView); - } else { - ((CustomTabProvider) mViewPager.getAdapter()).tabUnselect(tabView); - } - } } } @@ -856,14 +861,6 @@ public int getItemPosition(Object object) { } } - public interface CustomTabProvider { - View getCustomTabView(ViewGroup parent, int position); - - void tabSelect(View tab); - - void tabUnselect(View tab); - } - @Override protected Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); diff --git a/README.md b/README.md index 77735641..e86925ce 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ dependencies{ After v2.0.2(support 3.0+) dependencies{ compile 'com.android.support:support-v4:23.1.1' - compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar' + compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.6@aar' } ``` diff --git a/README_CN.md b/README_CN.md index cb7ec40d..8e90d4cc 100644 --- a/README_CN.md +++ b/README_CN.md @@ -67,7 +67,7 @@ dependencies{ After v2.0.2(support 3.0+) dependencies{ compile 'com.android.support:support-v4:23.1.1' - compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar' + compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.6@aar' } ``` diff --git a/app/build.gradle b/app/build.gradle index 93376795..e155af85 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { applicationId "com.flyco.tablayoutsamples" @@ -21,11 +21,11 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.3.0' + compile 'com.android.support:appcompat-v7:23.4.0' compile project(':FlycoTabLayout_Lib') //--->support 3.0+ -// compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar' +// compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.6@aar' //--->support 2.2+ // compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar' // compile 'com.nineoldandroids:library:2.4.0' diff --git a/app/src/main/java/com/flyco/tablayoutsamples/ui/SlidingTabActivity.java b/app/src/main/java/com/flyco/tablayoutsamples/ui/SlidingTabActivity.java index bf3cc1e7..290c9b03 100644 --- a/app/src/main/java/com/flyco/tablayoutsamples/ui/SlidingTabActivity.java +++ b/app/src/main/java/com/flyco/tablayoutsamples/ui/SlidingTabActivity.java @@ -23,9 +23,10 @@ public class SlidingTabActivity extends AppCompatActivity implements OnTabSelect private Context mContext = this; private ArrayList mFragments = new ArrayList<>(); private final String[] mTitles = { - "热门", "iOS", "Android", "前端" - , "后端", "设计", "工具资源" + "热门", "iOS", "Android" + , "前端", "后端", "设计", "工具资源" }; + private MyPagerAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,7 +40,8 @@ protected void onCreate(Bundle savedInstanceState) { View decorView = getWindow().getDecorView(); ViewPager vp = ViewFindUtils.find(decorView, R.id.vp); - vp.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); + mAdapter = new MyPagerAdapter(getSupportFragmentManager()); + vp.setAdapter(mAdapter); /** 默认 */ SlidingTabLayout tabLayout_1 = ViewFindUtils.find(decorView, R.id.tl_1); @@ -54,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) { /** indicator圆 */ SlidingTabLayout tabLayout_6 = ViewFindUtils.find(decorView, R.id.tl_6); /** indicator矩形圆角 */ - SlidingTabLayout tabLayout_7 = ViewFindUtils.find(decorView, R.id.tl_7); + final SlidingTabLayout tabLayout_7 = ViewFindUtils.find(decorView, R.id.tl_7); /** indicator三角形 */ SlidingTabLayout tabLayout_8 = ViewFindUtils.find(decorView, R.id.tl_8); /** indicator圆角色块 */ @@ -89,6 +91,20 @@ protected void onCreate(Bundle savedInstanceState) { tabLayout_2.showMsg(5, 5); tabLayout_2.setMsgMargin(5, 0, 10); + +// tabLayout_7.setOnTabSelectListener(new OnTabSelectListener() { +// @Override +// public void onTabSelect(int position) { +// Toast.makeText(mContext, "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show(); +// } +// +// @Override +// public void onTabReselect(int position) { +// mFragments.add(SimpleCardFragment.getInstance("后端")); +// mAdapter.notifyDataSetChanged(); +// tabLayout_7.addNewTab("后端"); +// } +// }); } @Override diff --git a/build.gradle b/build.gradle index 0fb6d67f..7e31c53e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'com.android.tools.build:gradle:2.1.0' // classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' // classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' // NOTE: Do not place your application dependencies here; they belong