MagicIndicator
MagicIndicator copied to clipboard
如何更新角标内的数字
我是这样子做的: public void updateMagicIndicator() { // 取出MagicIndicator当前的Navigator,强转为CommonNavigator CommonNavigator mCommonNavigator = (CommonNavigator) (myMagicIndicator.getNavigator()); // 进行更新 mCommonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { // TODO }
@Override
public IPagerTitleView getTitleView(Context context, final int index) {
ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
colorTransitionPagerTitleView.setNormalColor(yourColor);
colorTransitionPagerTitleView.setSelectedColor(yourColor);
colorTransitionPagerTitleView.setText(yourTitle);
colorTransitionPagerTitleView.setTextSize(16);
colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myViewPager.setCurrentItem(index);
}
});
//创建一个角标注入到当前Tab
BadgePagerTitleView badgePagerTitleView = new BadgePagerTitleView(context);
badgePagerTitleView.setInnerPagerTitleView(colorTransitionPagerTitleView);
int randNum = (int) (Math.random() * 101);
String textValue = "";
if (randNum > 99) {
textValue = "99+";
}else {
textValue = String.valueOf(randNum);
}
//创建一个TextView来显示角标
TextView badgeTextView = new TextView(context);
badgeTextView.setText(statusNum);
badgeTextView.setTextColor(Color.WHITE);
badgeTextView.setTextSize(12);
if (statusNum.length() <= 1) {
badgeTextView.setTextSize(14);
badgeTextView.setPadding(12, 1, 12, 1);
} else if (statusNum.length() <= 2) {
badgeTextView.setPadding(8, 5, 8, 5);
} else {
badgeTextView.setPadding(4, 7, 4, 7);
}
badgeTextView.setBackgroundResource(yourBackgroundResource);
badgePagerTitleView.setBadgeView(badgeTextView);
//定位角标位于Tab的X,Y轴的位置
badgePagerTitleView.setXBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_RIGHT, -5));
badgePagerTitleView.setYBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_TOP, -15));
badgePagerTitleView.setAutoCancelBadge(false);
return badgePagerTitleView;
}
@Override
public IPagerIndicator getIndicator(Context context) {
// TODO
}
});
}
请问你这么做可以实现角标更新了吗
/**
* 显示角标数量
*
* @param index 位置
* @param count 数量
*/
protected void showSignCount(int index, int count) {
BadgePagerTitleView badgePagerTitleView = (BadgePagerTitleView) mCommonNavigator.getPagerTitleView(index);
TextView tvCount = (TextView) badgePagerTitleView.getBadgeView();
if (count > 0) {
boolean beyond = count > 99;
tvCount.setText(beyond ? "99+" : count + "");
badgePagerTitleView.setXBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_RIGHT, -UIUtil.dip2px(_mActivity, beyond?20:12)));
badgePagerTitleView.setYBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_TOP, -UIUtil.dip2px(_mActivity, 3)));
showView(tvCount);
} else {
goneView(tvCount);
}
}
一般显示的都是从后台获取的数量,你不可能把它放在那里面直接显示。等数据返回后,再用此方法进行设置。