forked from hugeterry/CoordinatorTabLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
188 additions
and
0 deletions.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
...inatortablayout/src/main/java/cn/hugeterry/coordinatortablayout/CoordinatorTabLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package cn.hugeterry.coordinatortablayout; | ||
|
||
import android.content.Context; | ||
import android.content.res.ColorStateList; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Color; | ||
import android.support.design.widget.CollapsingToolbarLayout; | ||
import android.support.design.widget.CoordinatorLayout; | ||
import android.support.design.widget.TabLayout; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v4.view.ViewPager; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.Toolbar; | ||
import android.util.AttributeSet; | ||
import android.util.TypedValue; | ||
import android.view.LayoutInflater; | ||
import android.view.animation.AnimationUtils; | ||
import android.widget.ImageView; | ||
|
||
/** | ||
* @author hugeterry(http://hugeterry.cn) | ||
*/ | ||
|
||
public class CoordinatorTabLayout extends CoordinatorLayout { | ||
private Context mContext; | ||
private Toolbar mToolbar; | ||
private ActionBar mActionbar; | ||
private TabLayout mTabLayout; | ||
private ImageView mImageView; | ||
private CollapsingToolbarLayout mCollapsingToolbarLayout; | ||
|
||
public CoordinatorTabLayout(Context context) { | ||
super(context); | ||
mContext = context; | ||
} | ||
|
||
public CoordinatorTabLayout(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
mContext = context; | ||
if (!isInEditMode()) { | ||
initView(context); | ||
initWidget(context, attrs); | ||
} | ||
} | ||
|
||
public CoordinatorTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
mContext = context; | ||
if (!isInEditMode()) { | ||
initView(context); | ||
initWidget(context, attrs); | ||
} | ||
} | ||
|
||
private void initView(Context context) { | ||
LayoutInflater.from(context).inflate(R.layout.view_coordinatortablayout, this, true); | ||
initToolbar(); | ||
mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingtoolbarlayout); | ||
mTabLayout = (TabLayout) findViewById(R.id.tabLayout); | ||
mImageView = (ImageView) findViewById(R.id.imageview); | ||
} | ||
|
||
private void initWidget(Context context, AttributeSet attrs) { | ||
TypedArray typedArray = context.obtainStyledAttributes(attrs | ||
, R.styleable.CoordinatorTabLayout); | ||
|
||
TypedValue typedValue = new TypedValue(); | ||
mContext.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true); | ||
int contentScrimColor = typedArray.getColor( | ||
R.styleable.CoordinatorTabLayout_contentScrim, typedValue.data); | ||
mCollapsingToolbarLayout.setContentScrimColor(contentScrimColor); | ||
|
||
int tabIndicatorColor = typedArray.getColor(R.styleable.CoordinatorTabLayout_tabIndicatorColor, Color.WHITE); | ||
mTabLayout.setSelectedTabIndicatorColor(tabIndicatorColor); | ||
|
||
int tabTextColor = typedArray.getColor(R.styleable.CoordinatorTabLayout_tabTextColor, Color.WHITE); | ||
mTabLayout.setTabTextColors(ColorStateList.valueOf(tabTextColor)); | ||
typedArray.recycle(); | ||
} | ||
|
||
private void initToolbar() { | ||
mToolbar = (Toolbar) findViewById(R.id.toolbar); | ||
((AppCompatActivity) mContext).setSupportActionBar(mToolbar); | ||
mActionbar = ((AppCompatActivity) mContext).getSupportActionBar(); | ||
} | ||
|
||
/** | ||
* 设置Toolbar标题 | ||
* | ||
* @param title 标题 | ||
*/ | ||
public CoordinatorTabLayout setTitle(String title) { | ||
if (mActionbar != null) { | ||
mActionbar.setTitle(title); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* 设置Toolbar显示返回按钮及标题 | ||
* | ||
* @param canBack 是否返回 | ||
*/ | ||
public CoordinatorTabLayout setBackEnable(Boolean canBack) { | ||
if (canBack && mActionbar != null) { | ||
mActionbar.setDisplayHomeAsUpEnabled(true); | ||
mActionbar.setHomeAsUpIndicator(R.drawable.ic_arrow_white_24dp); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* 设置每个tab对应的头部图片 | ||
* | ||
* @param imageArray 图片数组 | ||
*/ | ||
public CoordinatorTabLayout setImageArray(int[] imageArray) { | ||
if (imageArray != null) { | ||
mImageView.setImageResource(imageArray[0]); | ||
setupTabLayout(imageArray, null); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* 设置每个tab对应的头部照片和ContentScrimColor | ||
* | ||
* @param imageArray 图片数组 | ||
* @param colorArray ContentScrimColor数组 | ||
*/ | ||
public CoordinatorTabLayout setImageArray(int[] imageArray, int[] colorArray) { | ||
if (imageArray != null) { | ||
mImageView.setImageResource(imageArray[0]); | ||
setupTabLayout(imageArray, colorArray); | ||
} | ||
return this; | ||
} | ||
|
||
private void setupTabLayout(final int[] imageArray, final int[] colorArray) { | ||
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { | ||
@Override | ||
public void onTabSelected(TabLayout.Tab tab) { | ||
mImageView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_dismiss)); | ||
mImageView.setImageResource(imageArray[tab.getPosition()]); | ||
if (colorArray != null) { | ||
mCollapsingToolbarLayout.setContentScrimColor( | ||
ContextCompat.getColor( | ||
mContext, colorArray[tab.getPosition()])); | ||
} | ||
mImageView.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_show)); | ||
} | ||
|
||
@Override | ||
public void onTabUnselected(TabLayout.Tab tab) { | ||
} | ||
|
||
@Override | ||
public void onTabReselected(TabLayout.Tab tab) { | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* 设置与该组件搭配的ViewPager | ||
* | ||
* @param viewPager 与TabLayout结合的ViewPager | ||
*/ | ||
public CoordinatorTabLayout setupWithViewPager(ViewPager viewPager) { | ||
mTabLayout.setupWithViewPager(viewPager); | ||
return this; | ||
} | ||
|
||
/** | ||
* 获取该组件中的ActionBar | ||
*/ | ||
public ActionBar getActionBar() { | ||
return mActionbar; | ||
} | ||
|
||
/** | ||
* 获取该组件中的TabLayout | ||
*/ | ||
public TabLayout getTabLayout() { | ||
return mTabLayout; | ||
} | ||
|
||
} |