Skip to content

Commit

Permalink
remove CustomTabProvider in SlidingTabLayout
Browse files Browse the repository at this point in the history
new added method 'addNewTab(String title)' for SlidingTabLayout
  • Loading branch information
H07000223 committed May 21, 2016
1 parent f461a66 commit 19278dd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHNAGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

10 changes: 5 additions & 5 deletions FlycoTabLayout_Lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> mTitles;
private LinearLayout mTabsContainer;
private int mCurrentTab;
private float mCurrentPositionOffset;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public class SlidingTabActivity extends AppCompatActivity implements OnTabSelect
private Context mContext = this;
private ArrayList<Fragment> mFragments = new ArrayList<>();
private final String[] mTitles = {
"热门", "iOS", "Android", "前端"
, "后端", "设计", "工具资源"
"热门", "iOS", "Android"
, "前端", "后端", "设计", "工具资源"
};
private MyPagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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);
Expand All @@ -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圆角色块 */
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19278dd

Please sign in to comment.