如何在我的标签布局中像Whatsapp一样制作计数器?

如何在我的标签布局中像Whatsapp一样制作计数器?

问题描述:

现在我只有一个简单的标签布局与查看传呼机一起工作。我想知道如何自定义我的标签布局,以便例如在我的应用程序中显示一个功能,以显示我的查看传呼机中该片段的回收器视图中的项目数。 感谢如何在我的标签布局中像Whatsapp一样制作计数器?

This is an example

+0

你可以在标签标题中使用跨度吗?如果是这样,可能会在某个地方开始。 – stkent

public View getTabView(Context context, int position) { 
    int[] unreadCount = {3, 1, 2}; 

    View tab = LayoutInflater.from(context).inflate(R.layout.partial_custom_tab, null); 
    tabText = (TextView) tab.findViewById(R.id.tab_title); 
    counter = (View) tab.findViewById(R.id.tab_badge); 
    tabText.setText(mPages.get(position).title); 
    if (mPages.get(position).icon != null) { 
     counter.setVisibility(View.VISIBLE); 
     counter.setBackground(mPages.get(position).icon); 
    } else { 
     counter.setVisibility(View.GONE); 
    } 

    BadgeDrawable badge = new BadgeDrawable(getContext(), 
      getResources().getColor(R.color.colorAccent)); 

    if (unreadCount[position] > 0) { 
     counter.setVisibility(View.VISIBLE); 
     badge.setCount(unreadCount[position]); 
     badge.mutate(); 
     counter.setBackground(badge); 
    } else { 
     counter.setVisibility(View.GONE); 
    } 

    if (position == 0) { 
     tab.setSelected(true); 
    } 
    return tab; 
} 

你可以做这样的事情:

LinearLaout tabone= (LinearLaout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 

TextView txtView= (TextView) tabone.findViewById(R.id.txtview); 
txtView.setText("ONE"); 

TextView txtCount= (ImageView) tabone.findViewById(R.id.txtcount); 
txtCount.setText("200")//Count 
/*Also don't forgot to add background of count textview in xml according to your design.*/ 

tabLayout.getTabAt(0).setCustomView(tabone); 

这就是它!