一 自定义WeiXinTabChangeView
1 自定义属性attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="WinXinTabView">
<!-- 是否选中-->
<attr name="tabChecked" format="boolean" />
<!-- tab的标题-->
<attr name="tabTitle" format="string" />
<!-- 标题的大小-->
<attr name="tabTitleSize" format="dimension">
<!-- 标题的颜色-->
<attr name="tabTitleColor" format="color" />
<!-- tab的图片-->
<attr name="tabIcon" format="reference" />
<!-- 未选中时的图片-->
<attr name="tabIconOver" format="reference" />
</declare-styleable>
</resources>
2 自定义 WeiXinTabChangeView类
public classWeiXinTabChangeViewextendsRelativeLayout {
privateContextmContext;
private booleanisChecked;//tab是否被选中
privateStringtable_title;//tab的标题
private floattabTitleSize;//标题的字体大小
private inttabTitleColor;//字体的颜色
//标题
privateTextViewtitle_one;
//二层标题
privateTextViewtitle_two;
//图标
privateDrawabletabIcon;
privateImageViewimageview_one;
//二层图标
privateDrawabletabIcon_two;
privateImageViewimageview_two;
//透明度
private floatmAlpha=1f;
private inttitleNolColor= Color.GRAY;
publicWeiXinTabChangeView(Context context) {
super(context);
}
publicWeiXinTabChangeView(Context context,AttributeSet attrs) {
super(context,attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.WinXinTabView);//获取TypedArray从而获取属性
isChecked= typedArray.getBoolean(R.styleable.WinXinTabView_tabChecked, false);
table_title= typedArray.getString(R.styleable.WinXinTabView_tabTitle);
tabTitleSize= typedArray.getDimension(R.styleable.WinXinTabView_tabTitleSize,20);
tabTitleColor= typedArray.getColor(R.styleable.WinXinTabView_tabTitleColor,Color.GRAY);
tabIcon= typedArray.getDrawable(R.styleable.WinXinTabView_tabIcon);
tabIcon_two= typedArray.getDrawable(R.styleable.WinXinTabView_tabIconOver);
typedArray.recycle();
mContext= context;
initView();
}
publicWeiXinTabChangeView(Context context,AttributeSet attrs, intdefStyleAttr) {
super(context,attrs,defStyleAttr);
}
//初始化控件
private voidinitView() {
RelativeLayout.LayoutParams titleLayoutParams =newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//父容器底部
titleLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
//二层标题
title_two=newTextView(mContext);
title_two.setId(R.id.title_two);//设置id
title_two.setLayoutParams(titleLayoutParams);
this.addView(title_two);//二层标题添加到 控件中
//标题
title_one=newTextView(mContext);
title_one.setId(R.id.title_one);
title_one.setLayoutParams(titleLayoutParams);
this.addView(title_one);
//图标的layoutparams
RelativeLayout.LayoutParams iconLayoutParams =newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
iconLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
iconLayoutParams.addRule(RelativeLayout.ABOVE,R.id.title_one);
//二层图标
imageview_two=newImageView(mContext);
imageview_two.setId(R.id.image_two);
imageview_two.setLayoutParams(iconLayoutParams);
this.addView(imageview_two);
//图标
imageview_one=newImageView(mContext);
imageview_one.setId(R.id.image_one);
imageview_one.setLayoutParams(iconLayoutParams);
this.addView(imageview_one);
//设置数据并刷新
refreshData();
}
private voidrefreshData() {
mAlpha=1;
setTitleOne();
setTitleTwo();
setIconOne();
setIconTwo();
}
//设置二层图标
private voidsetIconTwo() {
imageview_two.setImageDrawable(tabIcon_two);
if(isChecked){
imageview_two.setAlpha(mAlpha);
}else{
imageview_two.setAlpha(1-mAlpha);
}
}
//设置 一层 图标
private voidsetIconOne() {
imageview_one.setImageDrawable(tabIcon);
if(isChecked){
imageview_one.setAlpha(1-mAlpha);
}else{
imageview_one.setAlpha(mAlpha);
}
}
//设置 二层标题
private voidsetTitleTwo() {
title_two.setText(table_title);
title_two.setTextColor(tabTitleColor);
if(isChecked){
title_two.setAlpha(mAlpha);
}else{
title_two.setAlpha(1-mAlpha);
}
}
//设置标题的
private voidsetTitleOne() {
title_one.setText(table_title);
title_one.setTextColor(titleNolColor);
if(isChecked){
title_one.setAlpha(1-mAlpha);
}else{
title_one.setAlpha(mAlpha);
}
}
/**
*设置选中状态
* */
public voidsetChecked(booleanisChecked) {
this.isChecked= isChecked;
setCheckedData();
}
/**
*选中之后重新设置数据
* */
private voidsetCheckedData(){
refreshData();
}
/**
*滑动时改变颜色
* */
public voidonScrolling(floatalpha){
mAlpha= alpha;
onScrollSetData();
}
/**
*滑动时设置图标透明度以及文字透明度
* */
private voidonScrollSetData(){
//设置图标透明度
imageview_one.setAlpha(1-mAlpha);
imageview_two.setAlpha(mAlpha);
//设置标题透明度
title_one.setAlpha(1-mAlpha);
title_two.setAlpha(mAlpha);
}
publicStringgetTitleText() {
returntable_title;
}
public voidsetTitleText(String titleText) {
this.table_title= titleText;
}
publicDrawablegetTabIcon() {
returntabIcon;
}
public voidsetTabIcon(Drawable tabIcon) {
this.tabIcon= tabIcon;
}
public booleanisChecked() {
returnisChecked;
}
publicImageViewgetIconImageView() {
returnimageview_one;
}
}
二 自定义 一个 viewGroup 来存放 WeiXinTabChangeView
public classTabGroupViewextendsLinearLayout {
privateArrayListtabViewList;
privateWeiXinTabVieOnItemClickweiXinTabVieOnItemClick;
publicTabGroupView(Context context) {
this(context,null);
}
publicTabGroupView(Context context,AttributeSet attrs) {
super(context,attrs);
tabViewList=newArrayList();
//设置默认的 水平布局
this.setOrientation(HORIZONTAL);
}
/**
*选中某一个
* */
public voidsetCurrentItem(intitem){
changeItem();
WeiXinTabChangeView weiXinTab =tabViewList.get(item);
weiXinTab.setChecked(true);
}
/**
*清除选中状态
* */
private voidchangeItem(){
for(WeiXinTabChangeView weiXinTab :tabViewList){
weiXinTab.setChecked(false);
}
}
/**
*@paramposition当前界面索引
*@parampositionOffset滑动的百分比
* */
public voidonPageScrolling(intposition, floatpositionOffset) {
if(positionOffset>0){
WeiXinTabChangeView weiXinTab =tabViewList.get(position);
weiXinTab.onScrolling(1-positionOffset);
if(position+1
WeiXinTabChangeView nextweiXinTab =tabViewList.get(position+1);
nextweiXinTab.onScrolling(positionOffset);
}
}
}
/**
*点击事件
* */
public interfaceWeiXinTabVieOnItemClick{
voidonClick(intposition,WeiXinTabChangeView tabLayout);
}
/**
*设置每个Item的监听事件
* */
public voidsetOnItemClickListener(WeiXinTabVieOnItemClick weiXinTabVieOnItemClick) {
this.weiXinTabVieOnItemClick= weiXinTabVieOnItemClick;
initListener();
}
/**
*设置监听器
* */
private voidinitListener(){
intcount =this.getChildCount();
for(inti=0;i
finalWeiXinTabChangeView weiXinTab = (WeiXinTabChangeView)this.getChildAt(i);
tabViewList.add(weiXinTab);
final intfinalI = i;
weiXinTab.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
changeItem();//清除选中状态
weiXinTab.setChecked(true);
if(weiXinTabVieOnItemClick!=null){
weiXinTabVieOnItemClick.onClick(finalI,weiXinTab);
}
}
});
}
}
/**
*设置只能水平布局
* */
@Override
public voidsetOrientation(intorientation) {
super.setOrientation(HORIZONTAL);
}
}
三 使用方法
1 布局中的使用方式
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:lishuai="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lishuai.copyweixinhomedemo.ui.MainActivity">
<com.example.lishuai.copyweixinhomedemo.view.TabGroupView
android:id="@+id/tgv_weixingroup"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
>
<com.example.lishuai.copyweixinhomedemo.view.WeiXinTabChangeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab_main"
android:layout_weight="1"
lishuai:tabTitle="微信"
lishuai:tabTitleColor="#FBA615"
lishuai:tabIcon="@mipmap/tab_1_false"
lishuai:tabIconOver="@mipmap/tab_1_true"
lishuai:tabChecked="true"/>
<com.example.lishuai.copyweixinhomedemo.view.WeiXinTabChangeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
lishuai:tabTitle="微信"
lishuai:tabTitleColor="#FBA615"
lishuai:tabIcon="@mipmap/tab_1_false"
lishuai:tabIconOver="@mipmap/tab_1_true"
/>
<com.example.lishuai.copyweixinhomedemo.view.WeiXinTabChangeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
lishuai:tabTitle="微信"
lishuai:tabTitleColor="#FBA615"
lishuai:tabIcon="@mipmap/tab_1_false"
lishuai:tabIconOver="@mipmap/tab_1_true"
/>
<com.example.lishuai.copyweixinhomedemo.view.WeiXinTabChangeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
lishuai:tabTitle="微信"
lishuai:tabTitleColor="#FBA615"
lishuai:tabIcon="@mipmap/tab_1_false"
lishuai:tabIconOver="@mipmap/tab_1_true"
/>
</com.example.lishuai.copyweixinhomedemo.view.TabGroupView>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_above="@id/tgv_weixingroup"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
2代码中使用
public classMainActivityextendsAppCompatActivity {
privateTabGroupViewtabGroupView;
privateViewPagerviewpager;
private intmPosition=0;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private voidinitView() {
tabGroupView= (TabGroupView) findViewById(R.id.tgv_weixingroup);
viewpager= (ViewPager) findViewById(R.id.viewpager);
tabGroupView.setOnItemClickListener(newTabGroupView.WeiXinTabVieOnItemClick() {
@Override
public voidonClick(intposition,WeiXinTabChangeView tabLayout) {
Log.i("dfdf","选中的tablayout="+ position);
viewpager.setCurrentItem(position,false);
}
});
viewpager.setAdapter(newMainPagerAdapter(getSupportFragmentManager()));
viewpager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
@Override
public voidonPageScrolled(intposition, floatpositionOffset, intpositionOffsetPixels) {
tabGroupView.onPageScrolling(position,positionOffset);
}
@Override
public voidonPageSelected(intposition) {
mPosition= position;
tabGroupView.setCurrentItem(position);
}
@Override
public voidonPageScrollStateChanged(intstate) {
}
});
}
}
3 MainPagerAdapter
public classMainPagerAdapterextendsFragmentPagerAdapter {
publicMainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
publicFragmentgetItem(intposition) {
returnFragmentFactory.create(position);
}
@Override
public intgetCount() {
return4;
}
}
4 FragmentFactory
public classFragmentFactory {
public staticFragmentcreate(intposition){
Fragment fragment =null;
switch(position) {
case0:
fragment =newWeiXinFragment();
break;
case1:
fragment =newTongXunLuFragment();
break;
case2:
fragment =newFindFragment();
break;
case3:
fragment =newMeFragment();
break;
}
returnfragment;
}
}
5 Fragment
public classWeiXinFragmentextendsFragment {
@Nullable
@Override
publicViewonCreateView(LayoutInflater inflater,@NullableViewGroup container,@NullableBundle savedInstanceState) {
TextView textView =newTextView(getActivity());
textView.setText(getClass().getSimpleName());
returntextView;
}
}