将未读/通知计数添加到PagerSlidingTabStrip

时间:2015-03-18 19:45:33

标签: android spannablestring pagerslidingtabstrip

我一直在尝试在PagerSlidingTabStrip上添加自定义视图,该视图可用作该页面的未读计数器或通知计数。我正在使用PagerSlidingTabStrip,因为它比SlidingTabLayout更具可定制性。

unread counter

我尝试过以下方法: -

@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;
}

我也尝试从getPageTitle传递Html,但它仍然只从中提取字符串。样式不适用于标题标题。

无论我从getPageTitle传递什么,它只是从中提取出简单的字符串。

以下是我迄今取得的成就:

enter image description here

我尝试使用SlidingTabLayout来获取未读/通知计数器的结果,但它适用于2.3.7,而不是上面的任何版本。 2.3.7

为什么它不适用于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

2 个答案:

答案 0 :(得分:0)

我是通过自定义视图完成的。 编辑PagerSlidingTabStrip.java文件,如下所示。

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

如果使用文本选项卡,请修改addTextTab()方法。

添加方法以更改计数。

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

在您的活动或片段中调用它以设置计数。

和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>

答案 1 :(得分:0)

我稍微调整了SlidingTabLayout。这是我做的: -

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