ViewBadger为其他View添加角标

来源:互联网 发布:蔡英文 知乎 编辑:程序博客网 时间:2024/04/28 18:14

项目地址:https://github.com/jgilfelt/android-viewbadger 

以前都是自己写的,一不小心网上找到这个,觉得不错,以后直接copy了,方便快捷! 


 


 

Java代码  收藏代码
  1. package com.readystatesoftware.viewbadger;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.graphics.Color;  
  6. import android.graphics.Typeface;  
  7. import android.graphics.drawable.ShapeDrawable;  
  8. import android.graphics.drawable.shapes.RoundRectShape;  
  9. import android.util.AttributeSet;  
  10. import android.util.TypedValue;  
  11. import android.view.Gravity;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.view.ViewGroup.LayoutParams;  
  15. import android.view.ViewParent;  
  16. import android.view.animation.AccelerateInterpolator;  
  17. import android.view.animation.AlphaAnimation;  
  18. import android.view.animation.Animation;  
  19. import android.view.animation.DecelerateInterpolator;  
  20. import android.widget.FrameLayout;  
  21. import android.widget.TabWidget;  
  22. import android.widget.TextView;  
  23.   
  24. /** 
  25.  * A simple text label view that can be applied as a "badge" to any given {@link android.view.View}.  
  26.  * This class is intended to be instantiated at runtime rather than included in XML layouts. 
  27.  *  
  28.  * @author Jeff Gilfelt 
  29.  */  
  30. public class BadgeView extends TextView {  
  31.   
  32.     public static final int POSITION_TOP_LEFT = 1;  
  33.     public static final int POSITION_TOP_RIGHT = 2;  
  34.     public static final int POSITION_BOTTOM_LEFT = 3;  
  35.     public static final int POSITION_BOTTOM_RIGHT = 4;  
  36.     public static final int POSITION_CENTER = 5;  
  37.       
  38.     private static final int DEFAULT_MARGIN_DIP = 5;  
  39.     private static final int DEFAULT_LR_PADDING_DIP = 5;  
  40.     private static final int DEFAULT_CORNER_RADIUS_DIP = 8;  
  41.     private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;  
  42.     private static final int DEFAULT_BADGE_COLOR = Color.parseColor("#CCFF0000"); //Color.RED;  
  43.     private static final int DEFAULT_TEXT_COLOR = Color.WHITE;  
  44.       
  45.     private static Animation fadeIn;  
  46.     private static Animation fadeOut;  
  47.       
  48.     private Context context;  
  49.     private View target;  
  50.       
  51.     private int badgePosition;  
  52.     private int badgeMarginH;  
  53.     private int badgeMarginV;  
  54.     private int badgeColor;  
  55.       
  56.     private boolean isShown;  
  57.       
  58.     private ShapeDrawable badgeBg;  
  59.       
  60.     private int targetTabIndex;  
  61.       
  62.     public BadgeView(Context context) {  
  63.         this(context, (AttributeSet) null, android.R.attr.textViewStyle);  
  64.     }  
  65.       
  66.     public BadgeView(Context context, AttributeSet attrs) {  
  67.          this(context, attrs, android.R.attr.textViewStyle);  
  68.     }  
  69.       
  70.     /** 
  71.      * Constructor - 
  72.      *  
  73.      * create a new BadgeView instance attached to a target {@link android.view.View}. 
  74.      * 
  75.      * @param context context for this view. 
  76.      * @param target the View to attach the badge to. 
  77.      */  
  78.     public BadgeView(Context context, View target) {  
  79.          this(context, null, android.R.attr.textViewStyle, target, 0);  
  80.     }  
  81.       
  82.     /** 
  83.      * Constructor - 
  84.      *  
  85.      * create a new BadgeView instance attached to a target {@link android.widget.TabWidget} 
  86.      * tab at a given index. 
  87.      * 
  88.      * @param context context for this view. 
  89.      * @param target the TabWidget to attach the badge to. 
  90.      * @param index the position of the tab within the target. 
  91.      */  
  92.     public BadgeView(Context context, TabWidget target, int index) {  
  93.         this(context, null, android.R.attr.textViewStyle, target, index);  
  94.     }  
  95.       
  96.     public BadgeView(Context context, AttributeSet attrs, int defStyle) {  
  97.         this(context, attrs, defStyle, null0);  
  98.     }  
  99.       
  100.     public BadgeView(Context context, AttributeSet attrs, int defStyle, View target, int tabIndex) {  
  101.         super(context, attrs, defStyle);  
  102.         init(context, target, tabIndex);  
  103.     }  
  104.   
  105.     private void init(Context context, View target, int tabIndex) {  
  106.           
  107.         this.context = context;  
  108.         this.target = target;  
  109.         this.targetTabIndex = tabIndex;  
  110.           
  111.         // apply defaults  
  112.         badgePosition = DEFAULT_POSITION;  
  113.         badgeMarginH = dipToPixels(DEFAULT_MARGIN_DIP);  
  114.         badgeMarginV = badgeMarginH;  
  115.         badgeColor = DEFAULT_BADGE_COLOR;  
  116.           
  117.         setTypeface(Typeface.DEFAULT_BOLD);  
  118.         int paddingPixels = dipToPixels(DEFAULT_LR_PADDING_DIP);  
  119.         setPadding(paddingPixels, 0, paddingPixels, 0);  
  120.         setTextColor(DEFAULT_TEXT_COLOR);  
  121.           
  122.         fadeIn = new AlphaAnimation(01);  
  123.         fadeIn.setInterpolator(new DecelerateInterpolator());  
  124.         fadeIn.setDuration(200);  
  125.   
  126.         fadeOut = new AlphaAnimation(10);  
  127.         fadeOut.setInterpolator(new AccelerateInterpolator());  
  128.         fadeOut.setDuration(200);  
  129.           
  130.         isShown = false;  
  131.           
  132.         if (this.target != null) {  
  133.             applyTo(this.target);  
  134.         } else {  
  135.             show();  
  136.         }  
  137.           
  138.     }  
  139.   
  140.     private void applyTo(View target) {  
  141.           
  142.         LayoutParams lp = target.getLayoutParams();  
  143.         ViewParent parent = target.getParent();  
  144.         FrameLayout container = new FrameLayout(context);  
  145.           
  146.         if (target instanceof TabWidget) {  
  147.               
  148.             // set target to the relevant tab child container  
  149.             target = ((TabWidget) target).getChildTabViewAt(targetTabIndex);  
  150.             this.target = target;  
  151.               
  152.             ((ViewGroup) target).addView(container,   
  153.                     new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  154.               
  155.             this.setVisibility(View.GONE);  
  156.             container.addView(this);  
  157.               
  158.         } else {  
  159.               
  160.             // TODO verify that parent is indeed a ViewGroup  
  161.             ViewGroup group = (ViewGroup) parent;   
  162.             int index = group.indexOfChild(target);  
  163.               
  164.             group.removeView(target);  
  165.             group.addView(container, index, lp);  
  166.               
  167.             container.addView(target);  
  168.       
  169.             this.setVisibility(View.GONE);  
  170.             container.addView(this);  
  171.               
  172.             group.invalidate();  
  173.               
  174.         }  
  175.           
  176.     }  
  177.       
  178.     /** 
  179.      * Make the badge visible in the UI. 
  180.      *  
  181.      */  
  182.     public void show() {  
  183.         show(falsenull);  
  184.     }  
  185.       
  186.     /** 
  187.      * Make the badge visible in the UI. 
  188.      * 
  189.      * @param animate flag to apply the default fade-in animation. 
  190.      */  
  191.     public void show(boolean animate) {  
  192.         show(animate, fadeIn);  
  193.     }  
  194.       
  195.     /** 
  196.      * Make the badge visible in the UI. 
  197.      * 
  198.      * @param anim Animation to apply to the view when made visible. 
  199.      */  
  200.     public void show(Animation anim) {  
  201.         show(true, anim);  
  202.     }  
  203.       
  204.     /** 
  205.      * Make the badge non-visible in the UI. 
  206.      *  
  207.      */  
  208.     public void hide() {  
  209.         hide(falsenull);  
  210.     }  
  211.       
  212.     /** 
  213.      * Make the badge non-visible in the UI. 
  214.      * 
  215.      * @param animate flag to apply the default fade-out animation. 
  216.      */  
  217.     public void hide(boolean animate) {  
  218.         hide(animate, fadeOut);  
  219.     }  
  220.       
  221.     /** 
  222.      * Make the badge non-visible in the UI. 
  223.      * 
  224.      * @param anim Animation to apply to the view when made non-visible. 
  225.      */  
  226.     public void hide(Animation anim) {  
  227.         hide(true, anim);  
  228.     }  
  229.       
  230.     /** 
  231.      * Toggle the badge visibility in the UI. 
  232.      *  
  233.      */  
  234.     public void toggle() {  
  235.         toggle(falsenullnull);  
  236.     }  
  237.       
  238.     /** 
  239.      * Toggle the badge visibility in the UI. 
  240.      *  
  241.      * @param animate flag to apply the default fade-in/out animation. 
  242.      */  
  243.     public void toggle(boolean animate) {  
  244.         toggle(animate, fadeIn, fadeOut);  
  245.     }  
  246.       
  247.     /** 
  248.      * Toggle the badge visibility in the UI. 
  249.      * 
  250.      * @param animIn Animation to apply to the view when made visible. 
  251.      * @param animOut Animation to apply to the view when made non-visible. 
  252.      */  
  253.     public void toggle(Animation animIn, Animation animOut) {  
  254.         toggle(true, animIn, animOut);  
  255.     }  
  256.       
  257.     private void show(boolean animate, Animation anim) {  
  258.         if (getBackground() == null) {  
  259.             if (badgeBg == null) {  
  260.                 badgeBg = getDefaultBackground();  
  261.             }  
  262.             setBackgroundDrawable(badgeBg);  
  263.         }  
  264.         applyLayoutParams();  
  265.           
  266.         if (animate) {  
  267.             this.startAnimation(anim);  
  268.         }  
  269.         this.setVisibility(View.VISIBLE);  
  270.         isShown = true;  
  271.     }  
  272.       
  273.     private void hide(boolean animate, Animation anim) {  
  274.         this.setVisibility(View.GONE);  
  275.         if (animate) {  
  276.             this.startAnimation(anim);  
  277.         }  
  278.         isShown = false;  
  279.     }  
  280.       
  281.     private void toggle(boolean animate, Animation animIn, Animation animOut) {  
  282.         if (isShown) {  
  283.             hide(animate && (animOut != null), animOut);      
  284.         } else {  
  285.             show(animate && (animIn != null), animIn);  
  286.         }  
  287.     }  
  288.       
  289.     /** 
  290.      * Increment the numeric badge label. If the current badge label cannot be converted to 
  291.      * an integer value, its label will be set to "0". 
  292.      *  
  293.      * @param offset the increment offset. 
  294.      */  
  295.     public int increment(int offset) {  
  296.         CharSequence txt = getText();  
  297.         int i;  
  298.         if (txt != null) {  
  299.             try {  
  300.                 i = Integer.parseInt(txt.toString());  
  301.             } catch (NumberFormatException e) {  
  302.                 i = 0;  
  303.             }  
  304.         } else {  
  305.             i = 0;  
  306.         }  
  307.         i = i + offset;  
  308.         setText(String.valueOf(i));  
  309.         return i;  
  310.     }  
  311.       
  312.     /** 
  313.      * Decrement the numeric badge label. If the current badge label cannot be converted to 
  314.      * an integer value, its label will be set to "0". 
  315.      *  
  316.      * @param offset the decrement offset. 
  317.      */  
  318.     public int decrement(int offset) {  
  319.         return increment(-offset);  
  320.     }  
  321.       
  322.     private ShapeDrawable getDefaultBackground() {  
  323.           
  324.         int r = dipToPixels(DEFAULT_CORNER_RADIUS_DIP);  
  325.         float[] outerR = new float[] {r, r, r, r, r, r, r, r};  
  326.           
  327.         RoundRectShape rr = new RoundRectShape(outerR, nullnull);  
  328.         ShapeDrawable drawable = new ShapeDrawable(rr);  
  329.         drawable.getPaint().setColor(badgeColor);  
  330.           
  331.         return drawable;  
  332.           
  333.     }  
  334.       
  335.     private void applyLayoutParams() {  
  336.           
  337.         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  338.           
  339.         switch (badgePosition) {  
  340.         case POSITION_TOP_LEFT:  
  341.             lp.gravity = Gravity.LEFT | Gravity.TOP;  
  342.             lp.setMargins(badgeMarginH, badgeMarginV, 00);  
  343.             break;  
  344.         case POSITION_TOP_RIGHT:  
  345.             lp.gravity = Gravity.RIGHT | Gravity.TOP;  
  346.             lp.setMargins(0, badgeMarginV, badgeMarginH, 0);  
  347.             break;  
  348.         case POSITION_BOTTOM_LEFT:  
  349.             lp.gravity = Gravity.LEFT | Gravity.BOTTOM;  
  350.             lp.setMargins(badgeMarginH, 00, badgeMarginV);  
  351.             break;  
  352.         case POSITION_BOTTOM_RIGHT:  
  353.             lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;  
  354.             lp.setMargins(00, badgeMarginH, badgeMarginV);  
  355.             break;  
  356.         case POSITION_CENTER:  
  357.             lp.gravity = Gravity.CENTER;  
  358.             lp.setMargins(0000);  
  359.             break;  
  360.         default:  
  361.             break;  
  362.         }  
  363.           
  364.         setLayoutParams(lp);  
  365.           
  366.     }  
  367.   
  368.     /** 
  369.      * Returns the target View this badge has been attached to. 
  370.      *  
  371.      */  
  372.     public View getTarget() {  
  373.         return target;  
  374.     }  
  375.   
  376.     /** 
  377.      * Is this badge currently visible in the UI? 
  378.      *  
  379.      */  
  380.     @Override  
  381.     public boolean isShown() {  
  382.         return isShown;  
  383.     }  
  384.   
  385.     /** 
  386.      * Returns the positioning of this badge. 
  387.      *  
  388.      * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER. 
  389.      *  
  390.      */  
  391.     public int getBadgePosition() {  
  392.         return badgePosition;  
  393.     }  
  394.   
  395.     /** 
  396.      * Set the positioning of this badge. 
  397.      *  
  398.      * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER. 
  399.      *  
  400.      */  
  401.     public void setBadgePosition(int layoutPosition) {  
  402.         this.badgePosition = layoutPosition;  
  403.     }  
  404.   
  405.     /** 
  406.      * Returns the horizontal margin from the target View that is applied to this badge. 
  407.      *  
  408.      */  
  409.     public int getHorizontalBadgeMargin() {  
  410.         return badgeMarginH;  
  411.     }  
  412.       
  413.     /** 
  414.      * Returns the vertical margin from the target View that is applied to this badge. 
  415.      *  
  416.      */  
  417.     public int getVerticalBadgeMargin() {  
  418.         return badgeMarginV;  
  419.     }  
  420.   
  421.     /** 
  422.      * Set the horizontal/vertical margin from the target View that is applied to this badge. 
  423.      *  
  424.      * @param badgeMargin the margin in pixels. 
  425.      */  
  426.     public void setBadgeMargin(int badgeMargin) {  
  427.         this.badgeMarginH = badgeMargin;  
  428.         this.badgeMarginV = badgeMargin;  
  429.     }  
  430.       
  431.     /** 
  432.      * Set the horizontal/vertical margin from the target View that is applied to this badge. 
  433.      *  
  434.      * @param horizontal margin in pixels. 
  435.      * @param vertical margin in pixels. 
  436.      */  
  437.     public void setBadgeMargin(int horizontal, int vertical) {  
  438.         this.badgeMarginH = horizontal;  
  439.         this.badgeMarginV = vertical;  
  440.     }  
  441.       
  442.     /** 
  443.      * Returns the color value of the badge background. 
  444.      *  
  445.      */  
  446.     public int getBadgeBackgroundColor() {  
  447.         return badgeColor;  
  448.     }  
  449.   
  450.     /** 
  451.      * Set the color value of the badge background. 
  452.      *  
  453.      * @param badgeColor the badge background color. 
  454.      */  
  455.     public void setBadgeBackgroundColor(int badgeColor) {  
  456.         this.badgeColor = badgeColor;  
  457.         badgeBg = getDefaultBackground();  
  458.     }  
  459.       
  460.     private int dipToPixels(int dip) {  
  461.         Resources r = getResources();  
  462.         float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());  
  463.         return (int) px;  
  464.     }  
  465.   
  466. }  


用法: 
Java代码  收藏代码
  1. package viewbadger.demo;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Context;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.view.animation.BounceInterpolator;  
  12. import android.view.animation.TranslateAnimation;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.Button;  
  15. import android.widget.ListView;  
  16. import android.widget.TabHost;  
  17. import android.widget.TabWidget;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21. import com.readystatesoftware.viewbadger.BadgeView;  
  22. import com.readystatesoftware.viewbadger.R;  
  23.   
  24. public class DemoActivity extends TabActivity {  
  25.       
  26.     private static final String[] DATA = Cheeses.sCheeseStrings;  
  27.       
  28.     Button btnPosition;  
  29.     Button btnColour;  
  30.     Button btnAnim1;  
  31.     Button btnAnim2;  
  32.     Button btnCustom;  
  33.     Button btnClick;  
  34.     Button btnTab;  
  35.     Button btnIncrement;  
  36.       
  37.     ListView listDemo;  
  38.       
  39.     BadgeView badge1;  
  40.     BadgeView badge2;  
  41.     BadgeView badge3;  
  42.     BadgeView badge4;  
  43.     BadgeView badge5;  
  44.     BadgeView badge6;  
  45.     BadgeView badge7;  
  46.     BadgeView badge8;  
  47.       
  48.     @Override  
  49.     public void onCreate(Bundle savedInstanceState) {  
  50.         super.onCreate(savedInstanceState);  
  51.         setContentView(R.layout.main);  
  52.           
  53.         final TabHost tabHost = getTabHost();  
  54.   
  55.         tabHost.addTab(tabHost.newTabSpec("demos")  
  56.                 .setIndicator("Badge Demos")  
  57.                 .setContent(R.id.tab1));  
  58.   
  59.         tabHost.addTab(tabHost.newTabSpec("adapter")  
  60.                 .setIndicator("List Adapter")  
  61.                 .setContent(R.id.tab2));  
  62.           
  63.         tabHost.addTab(tabHost.newTabSpec("tests")  
  64.                 .setIndicator("Layout Tests")  
  65.                 .setContent(R.id.tab3));  
  66.          
  67.         // *** default badge ***  
  68.           
  69.         View target = findViewById(R.id.default_target);  
  70.         BadgeView badge = new BadgeView(this, target);  
  71.         badge.setText("1");  
  72.         badge.show();  
  73.           
  74.         // *** set position ***  
  75.           
  76.         btnPosition = (Button) findViewById(R.id.position_target);  
  77.         badge1 = new BadgeView(this, btnPosition);  
  78.         badge1.setText("12");  
  79.         badge1.setBadgePosition(BadgeView.POSITION_CENTER);  
  80.         btnPosition.setOnClickListener(new OnClickListener() {  
  81.             @Override  
  82.             public void onClick(View v) {  
  83.                 badge1.toggle();  
  84.             }  
  85.         });  
  86.           
  87.         // *** badge/text size & colour ***  
  88.           
  89.         btnColour = (Button) findViewById(R.id.colour_target);  
  90.         badge2 = new BadgeView(this, btnColour);  
  91.         badge2.setText("New!");  
  92.         badge2.setTextColor(Color.BLUE);  
  93.         badge2.setBadgeBackgroundColor(Color.YELLOW);  
  94.         badge2.setTextSize(12);  
  95.         btnColour.setOnClickListener(new OnClickListener() {  
  96.             @Override  
  97.             public void onClick(View v) {  
  98.                 badge2.toggle();  
  99.             }  
  100.         });  
  101.           
  102.         // *** default animation ***  
  103.           
  104.         btnAnim1 = (Button) findViewById(R.id.anim1_target);  
  105.         badge3 = new BadgeView(this, btnAnim1);  
  106.         badge3.setText("84");  
  107.         btnAnim1.setOnClickListener(new OnClickListener() {  
  108.             @Override  
  109.             public void onClick(View v) {  
  110.                 badge3.toggle(true);  
  111.             }  
  112.         });  
  113.           
  114.         // *** custom animation ***  
  115.           
  116.         btnAnim2 = (Button) findViewById(R.id.anim2_target);  
  117.         badge4 = new BadgeView(this, btnAnim2);  
  118.         badge4.setText("123");  
  119.         badge4.setBadgePosition(BadgeView.POSITION_TOP_LEFT);  
  120.         badge4.setBadgeMargin(1510);  
  121.         badge4.setBadgeBackgroundColor(Color.parseColor("#A4C639"));  
  122.         btnAnim2.setOnClickListener(new OnClickListener() {  
  123.             @Override  
  124.             public void onClick(View v) {  
  125.                 TranslateAnimation anim = new TranslateAnimation(-100000);  
  126.                 anim.setInterpolator(new BounceInterpolator());  
  127.                 anim.setDuration(1000);  
  128.                 badge4.toggle(anim, null);  
  129.             }  
  130.         });  
  131.           
  132.         // *** custom background ***  
  133.           
  134.         btnCustom = (Button) findViewById(R.id.custom_target);  
  135.         badge5 = new BadgeView(this, btnCustom);  
  136.         badge5.setText("37");  
  137.         badge5.setBackgroundResource(R.drawable.badge_ifaux);  
  138.         badge5.setTextSize(16);  
  139.         btnCustom.setOnClickListener(new OnClickListener() {  
  140.             @Override  
  141.             public void onClick(View v) {  
  142.                 badge5.toggle(true);  
  143.             }  
  144.         });  
  145.           
  146.         // *** clickable badge ***  
  147.           
  148.         btnClick = (Button) findViewById(R.id.click_target);  
  149.         badge6 = new BadgeView(this, btnClick);  
  150.         badge6.setText("click me");  
  151.         badge6.setBadgeBackgroundColor(Color.BLUE);  
  152.         badge6.setTextSize(16);  
  153.         badge6.setOnClickListener(new OnClickListener() {  
  154.             @Override  
  155.             public void onClick(View v) {  
  156.                 Toast.makeText(DemoActivity.this"clicked badge", Toast.LENGTH_SHORT).show();  
  157.             }  
  158.         });  
  159.         btnClick.setOnClickListener(new OnClickListener() {  
  160.             @Override  
  161.             public void onClick(View v) {  
  162.                 badge6.toggle();  
  163.             }  
  164.         });  
  165.           
  166.         // *** tab ***  
  167.           
  168.         TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);  
  169.           
  170.         btnTab = (Button) findViewById(R.id.tab_btn);  
  171.         badge7 = new BadgeView(this, tabs, 0);  
  172.         badge7.setText("5");  
  173.         btnTab.setOnClickListener(new OnClickListener() {  
  174.             @Override  
  175.             public void onClick(View v) {  
  176.                 badge7.toggle();  
  177.             }  
  178.         });  
  179.           
  180.         // *** increment ***  
  181.           
  182.         btnIncrement = (Button) findViewById(R.id.increment_target);  
  183.         badge8 = new BadgeView(this, btnIncrement);  
  184.         badge8.setText("0");  
  185.         btnIncrement.setOnClickListener(new OnClickListener() {  
  186.             @Override  
  187.             public void onClick(View v) {  
  188.                 if (badge8.isShown()) {  
  189.                     badge8.increment(1);  
  190.                 } else {  
  191.                     badge8.show();  
  192.                 }  
  193.             }  
  194.         });  
  195.           
  196.         // *** list adapter ****  
  197.           
  198.         listDemo = (ListView) findViewById(R.id.tab2);  
  199.         listDemo.setAdapter(new BadgeAdapter(this));  
  200.           
  201.     }  
  202.   
  203.     @Override  
  204.     protected void onResume() {  
  205.         super.onResume();  
  206.           
  207.         BadgeView badge;  
  208.         View target;  
  209.           
  210.         // *** test linear layout container ***  
  211.           
  212.         target = findViewById(R.id.linear_target);  
  213.         badge = new BadgeView(this, target);  
  214.         badge.setText("OK");  
  215.         badge.show();  
  216.           
  217.         // *** test relative layout container ***  
  218.           
  219.         target = findViewById(R.id.relative_target);  
  220.         badge = new BadgeView(this, target);  
  221.         badge.setText("OK");  
  222.         badge.show();  
  223.           
  224.         // *** test frame layout container ***  
  225.           
  226.         target = findViewById(R.id.frame_target);  
  227.         badge = new BadgeView(this, target);  
  228.         badge.setText("OK");  
  229.         badge.show();  
  230.           
  231.         // *** test table layout container ***  
  232.           
  233.         target = findViewById(R.id.table_target);  
  234.         badge = new BadgeView(this, target);  
  235.         badge.setText("OK");  
  236.         badge.show();  
  237.           
  238.         // *** test linear layout ***  
  239.           
  240.         target = findViewById(R.id.linear_group_target);  
  241.         badge = new BadgeView(this, target);  
  242.         badge.setText("OK");  
  243.         badge.show();  
  244.           
  245.         // *** test relative layout ***  
  246.           
  247.         target = findViewById(R.id.relative_group_target);  
  248.         badge = new BadgeView(this, target);  
  249.         badge.setText("OK");  
  250.         badge.show();  
  251.           
  252.         // *** test frame layout ***  
  253.           
  254.         target = findViewById(R.id.frame_group_target);  
  255.         badge = new BadgeView(this, target);  
  256.         badge.setText("OK");  
  257.         badge.show();  
  258.           
  259.         // *** test table layout ***  
  260.           
  261.         target = findViewById(R.id.tablerow_group_target);  
  262.         badge = new BadgeView(this, target);  
  263.         badge.setText("OK");  
  264.         badge.show();  
  265.           
  266.     }  
  267.       
  268.     private static class BadgeAdapter extends BaseAdapter {  
  269.         private LayoutInflater mInflater;  
  270.         private Context mContext;  
  271.         private static final int droidGreen = Color.parseColor("#A4C639");  
  272.           
  273.         public BadgeAdapter(Context context) {  
  274.             mInflater = LayoutInflater.from(context);  
  275.             mContext = context;  
  276.         }  
  277.   
  278.         public int getCount() {  
  279.             return DATA.length;  
  280.         }  
  281.   
  282.         public Object getItem(int position) {  
  283.             return position;  
  284.         }  
  285.           
  286.         public long getItemId(int position) {  
  287.             return position;  
  288.         }  
  289.   
  290.         public View getView(int position, View convertView, ViewGroup parent) {  
  291.             ViewHolder holder;  
  292.   
  293.             if (convertView == null) {  
  294.                 convertView = mInflater.inflate(android.R.layout.simple_list_item_2, null);  
  295.                 holder = new ViewHolder();  
  296.                 holder.text = (TextView) convertView.findViewById(android.R.id.text1);  
  297.                 holder.badge = new BadgeView(mContext, holder.text);  
  298.                 holder.badge.setBadgeBackgroundColor(droidGreen);  
  299.                 holder.badge.setTextColor(Color.BLACK);  
  300.                 convertView.setTag(holder);  
  301.             } else {  
  302.                 holder = (ViewHolder) convertView.getTag();  
  303.             }  
  304.   
  305.             holder.text.setText(DATA[position]);  
  306.               
  307.             if (position % 3 == 0) {  
  308.                 holder.badge.setText(String.valueOf(position));  
  309.                 holder.badge.show();  
  310.             } else {  
  311.                 holder.badge.hide();  
  312.             }  
  313.               
  314.               
  315.             return convertView;  
  316.         }  
  317.   
  318.         static class ViewHolder {  
  319.             TextView text;  
  320.             BadgeView badge;  
  321.         }  
  322.     }  
  323.       
  324. }  
0 0
原创粉丝点击