Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可通过Drawable设置Tab的图片 #238

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
增加drawable的接口。
  • Loading branch information
lizk authored and lizk committed Nov 1, 2017
commit c5bceca57d8a765bfb54434ae3daea64ab0f1703
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Parcelable;
Expand Down Expand Up @@ -235,7 +236,7 @@ private void addTab(final int position, View tabView) {
TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
tv_tab_title.setText(mTabEntitys.get(position).getTabTitle());
ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
iv_tab_icon.setImageResource(mTabEntitys.get(position).getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, mTabEntitys.get(position), false);

tabView.setOnClickListener(new OnClickListener() {
@Override
Expand Down Expand Up @@ -286,7 +287,7 @@ private void updateTabStyles() {
if (mIconVisible) {
iv_tab_icon.setVisibility(View.VISIBLE);
CustomTabEntity tabEntity = mTabEntitys.get(i);
iv_tab_icon.setImageResource(i == mCurrentTab ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, tabEntity, i == mCurrentTab);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
mIconWidth <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconWidth,
mIconHeight <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconHeight);
Expand Down Expand Up @@ -315,13 +316,23 @@ private void updateTabSelection(int position) {
tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor);
ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
CustomTabEntity tabEntity = mTabEntitys.get(i);
iv_tab_icon.setImageResource(isSelect ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, tabEntity, isSelect);
if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
tab_title.getPaint().setFakeBoldText(isSelect);
}
}
}

protected void updateTabIcon(ImageView tabIcon, CustomTabEntity tabEntity, boolean isSelected) {
Drawable d = isSelected ? tabEntity.getTabSelectedDrawable() : tabEntity.getTabUnselectedDrawable();
if (d != null) {
tabIcon.setImageDrawable(d);
} else {
int resId = isSelected ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon();
tabIcon.setImageResource(resId);
}
}

private void calcOffset() {
final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
mCurrentP.left = currentTabView.getLeft();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flyco.tablayout.listener;

import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;

public interface CustomTabEntity {
Expand All @@ -10,4 +11,7 @@ public interface CustomTabEntity {

@DrawableRes
int getTabUnselectedIcon();

Drawable getTabSelectedDrawable();
Drawable getTabUnselectedDrawable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.flyco.tablayout.listener;

import android.graphics.drawable.Drawable;

/**
* Created by lizk on 2017/11/1.
*/

public class TabEntity implements CustomTabEntity {

private String tabTitle;
private Drawable selectedDrawable;
private Drawable unselectedDrawable;
private int selectedIcon;
private int unSelectedIcon;

public TabEntity(String tabTitle, Drawable selectedDrawable, Drawable unselectedDrawable) {
this.tabTitle = tabTitle;
this.selectedDrawable = selectedDrawable;
this.unselectedDrawable = unselectedDrawable;
}

public TabEntity(String tabTitle, int selectedIcon, int unselectedIcon) {
this.tabTitle = tabTitle;
this.selectedIcon = selectedIcon;
this.unSelectedIcon = unselectedIcon;
}

@Override
public String getTabTitle() {
return tabTitle;
}

@Override
public int getTabSelectedIcon() {
return selectedIcon;
}

@Override
public int getTabUnselectedIcon() {
return unSelectedIcon;
}

@Override
public Drawable getTabSelectedDrawable() {
return selectedDrawable;
}

@Override
public Drawable getTabUnselectedDrawable() {
return unselectedDrawable;
}
}