2015-03-18 1 views
0

J'ai essayé d'ajouter une vue personnalisée sur PagerSlidingTabStrip qui peut être utilisée comme compteur non lu ou nombre de notifications pour cette page. J'utilise PagerSlidingTabStrip car il est plus personnalisable que SlidingTabLayout.Ajouter le compte non-notifcation/notifcation à PagerSlidingTabStrip

unread counter

J'ai essayé ce qui suit: -

@Override 
public CharSequence getPageTitle(int position) { 
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); 
    String mCounterText = "1"; 
    String mHeaderText = "Header "; 
    stringBuilder.append(mHeaderText); 
    stringBuilder.append(mCounterText); 
    stringBuilder.setSpan(new RoundedBackgroundSpan(), mHeaderText.length()-1, mHeaderText.length()+mCounterText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    return stringBuilder; 
} 

J'ai aussi essayé de passer Html de getPageTitle mais encore extrait la chaîne que par elle. Le style ne s'applique pas au titre des onglets.

Peu importe ce que j'ai passé de getPageTitle, il extrait juste une chaîne simple de celui-ci.

Voici ce que j'ai accompli jusqu'à présent:

enter image description here

J'ai essayé avec SlidingTabLayout qui ne nécessitent gimme du compteur non lu/notification, mais il fonctionne avec 2.3.7 et non avec tout version ci-dessus. 2.3.7

Pourquoi cela ne fonctionnait pas avec la version 2.3.7?

RoundedBackgroundSpan: -

public class RoundedBackgroundSpan extends ReplacementSpan { 
    /* 
    * (non-Javadoc) 
    * 
    * @see android.text.style.ReplacementSpan#draw(android.graphics.Canvas, 
    * java.lang.CharSequence, int, int, float, int, int, int, 
    * android.graphics.Paint) 
    */ 
    @Override 
    public void draw(Canvas canvas, CharSequence text, int start, int end, 
      float x, int top, int y, int bottom, Paint paint) { 
     // TODO Auto-generated method stub 
     RectF rect = new RectF(x, top, 
       x + MeasureText(paint, text, start, end), bottom); 
     paint.setColor(Color.parseColor("#FF0000"));//background color 
     canvas.drawRoundRect(rect, 3, 3, paint); 
     paint.setColor(Color.parseColor("#FFFFFF"));//text color 
     canvas.drawText(text, start, end, x, y, paint); 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.text.style.ReplacementSpan#getSize(android.graphics.Paint, 
    * java.lang.CharSequence, int, int, android.graphics.Paint.FontMetricsInt) 
    */ 
    @Override 
    public int getSize(Paint paint, CharSequence text, int start, int end, 
      FontMetricsInt fm) { 
     // TODO Auto-generated method stub 
     return Math.round(MeasureText(paint, text, start, end)); 
    } 

    private float MeasureText(Paint paint, CharSequence text, int start, 
      int end) { 
     return paint.measureText(text, start, end); 
    } 

} 

Screenshot of 5.0

+0

On dirait une implémentation personnalisée de 'ReplacementSpan'. – Vikram

+0

@Vikram: Essayé avec ReplacementSpan mais toujours il n'en extrait que des cordes. –

+0

Ceci est une supposition .. J'ai vu 'toutes les majuscules 'interférer avec les travées avant. Est-ce que vous définissez ceci dans vos styles de texte? – Vikram

Répondre

0

J'ai légèrement modifié SlidingTabLayout. Voici ce que je l'ai fait: -

protected TextView createDefaultTabView(Context context) { 
     TextView textView = new TextView(context); 
     textView.setGravity(Gravity.CENTER); 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP); 
     textView.setTypeface(Typeface.DEFAULT_BOLD); 

     /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // If we're running on Honeycomb or newer, then we can use the Theme's 
      // selectableItemBackground to ensure that the View has a pressed state 
      TypedValue outValue = new TypedValue(); 
      getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, 
        outValue, true); 
      textView.setBackgroundResource(outValue.resourceId); 
     } 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
      // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style 
      textView.setAllCaps(true); 
     }*/ 

     int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density); 
     textView.setPadding(padding, padding, padding, padding); 

     return textView; 
    } 

SlidingTabLayout with Counter

0

Je l'ai fait par vue personnalisée. Modifiez le fichier PagerSlidingTabStrip.java comme ci-dessous.

private void addIconTab(final int position, int resId) { 
    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.image_tab, null); 
    ImageButton tab = (ImageButton)view.findViewById(R.id.img_icon); 
    tab.setImageResource(resId); 
    addTab(position, view); 
} 

Si vous utilisez un onglet de texte, modifiez la méthode addTextTab().

Ajouter une méthode pour changer le nombre.

public void setIndicatorCount(int index, String count) { 
    RelativeLayout view = (RelativeLayout)tabsContainer.getChildAt(index); 
    View tv = view.getChildAt(1); 
    if(tv instanceof TextView) { 
     ((TextView) tv).setText(count); 
    } 
} 

Appelez-le dans votre activité ou un fragment pour définir le nombre.

Et layout.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:id="@+id/layout_container" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="horizontal" > 
 

 
    <ImageButton 
 
     android:id="@+id/img_icon" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:contentDescription="@null" 
 
     android:layout_centerInParent="true" 
 
     android:background="@null" 
 
     android:clickable="false" /> 
 

 
    <TextView 
 
    android:id="@+id/tv_count" 
 
    android:layout_width="wrap_content" 
 
    android:layout_height="wrap_content" 
 
    android:layout_alignParentRight="true" 
 
    android:layout_marginRight="7dp" 
 
    android:layout_marginTop="7dp" 
 
    android:background="@drawable/bg_number" 
 
    android:gravity="center" 
 
    android:textColor="@android:color/white" /> 
 
</RelativeLayout>