2017-10-17 31 views
0

À l'heure actuelle, je n'ai qu'une simple disposition d'onglets fonctionnant avec un pageur de vue. Je voudrais savoir comment personnaliser ma disposition d'onglet afin d'avoir par exemple une fonctionnalité dans mon application pour montrer en cela le nombre d'éléments dans ma vue de recycleur pour ce fragment dans mon pager de vue. MerciComment faire un compteur dans ma disposition de l'onglet comme WhatsApp?

This is an example

+0

Pouvez-vous utiliser les plages dans les titres d'onglet? Si c'est le cas, cela pourrait être un début. – stkent

Répondre

1
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; 
} 
0

Vous pouvez faire quelque chose comme ceci:

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); 

Ca y est !!