|
import android.content.Context; |
|
import android.graphics.Color; |
|
import android.os.Build; |
|
import android.support.annotation.Nullable; |
|
import android.support.annotation.RequiresApi; |
|
import android.support.annotation.StringRes; |
|
import android.util.AttributeSet; |
|
import android.view.LayoutInflater; |
|
import android.view.View; |
|
import android.widget.FrameLayout; |
|
import android.widget.ImageView; |
|
import android.widget.LinearLayout; |
|
import android.widget.TextView; |
|
|
|
/** |
|
* @author yuana <[email protected]> |
|
* @since 9/29/17 |
|
*/ |
|
|
|
public class TabView extends LinearLayout { |
|
|
|
private TextView tvLabel; |
|
private TextView tvCounter; |
|
private ImageView ivBgCounter; |
|
private FrameLayout flBadge; |
|
|
|
public TabView(Context context) { |
|
super(context); |
|
init(null); |
|
} |
|
|
|
public TabView(Context context, @Nullable AttributeSet attrs) { |
|
super(context, attrs); |
|
init(attrs); |
|
} |
|
|
|
public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
|
super(context, attrs, defStyleAttr); |
|
init(attrs); |
|
} |
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) |
|
public TabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
|
super(context, attrs, defStyleAttr, defStyleRes); |
|
init(attrs); |
|
} |
|
|
|
private void init(AttributeSet attrs) { |
|
LayoutInflater inflater = (LayoutInflater) getContext() |
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
View view = inflater.inflate(R.layout.view_tab, this); |
|
|
|
initView(view); |
|
} |
|
|
|
private void initView(View v) { |
|
tvLabel = (TextView) v.findViewById(R.id.tvLabel); |
|
tvCounter = (TextView) v.findViewById(R.id.tvCounter); |
|
ivBgCounter = (ImageView) v.findViewById(R.id.ivBgCounter); |
|
flBadge = (FrameLayout) v.findViewById(R.id.flBadge); |
|
} |
|
|
|
public void setLabel(String label) { |
|
tvLabel.setText(label); |
|
} |
|
|
|
public void setLabel(@StringRes int stringRes) { |
|
tvLabel.setText(stringRes); |
|
} |
|
|
|
public void setBadgeCount(int badgeCount) { |
|
flBadge.setVisibility(badgeCount > 0 ? VISIBLE : GONE); |
|
tvCounter.setText(badgeCount > 99 ? "99+" : String.valueOf(badgeCount)); |
|
} |
|
|
|
@Override |
|
public void setSelected(boolean selected) { |
|
super.setSelected(selected); |
|
|
|
tvLabel.setTextColor(selected ? Color.WHITE : Color.parseColor("#fc959b")); |
|
ivBgCounter.setImageResource(selected ? R.drawable.bg_circle_white : |
|
R.drawable.bg_circle_primary_light); |
|
} |
|
} |
|
|