欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现购物车及其他功能的角标

程序员文章站 2022-10-24 17:10:44
1.先来张效果图 2.自定义一个角标工具类bottombarview 。 ** * created by administrator on 2016/1...

1.先来张效果图

Android实现购物车及其他功能的角标

2.自定义一个角标工具类bottombarview 。

**
 * created by administrator on 2016/12/27.
 * 角标工具类
 */
public class bottombarview extends relativelayout {
 private context context;
 private textview bar_num;
 private int count = 0;
 public bottombarview(context context) {
 this(context, null);
 }
 public bottombarview(context context, attributeset attrs) {
 this(context, attrs, 0);
 }
 public bottombarview(context context, attributeset attrs, int defstyleattr) {
 super(context, attrs, defstyleattr);
 this.context = context;
 relativelayout rl = (relativelayout) layoutinflater.from(context).inflate(r.layout.bottom_bar_view, this, true);
 bar_num = (textview) rl.findviewbyid(r.id.bar_num);
 bar_num.setvisibility(gone);
 }
 public void add() {
 bar_num.setvisibility(visible);
 count++;
 if (count < 100) {
 bar_num.settext(count + "");
 } else {
 bar_num.settext("99+");
 }
 }
 public void add(int n) throws exception {
 if(n<0){
 throw new exception(bottombarview.class.getsimplename()+" add(int n).the param must be a positive num");
 }
 bar_num.setvisibility(visible);
 count += n;
 if (count < 100) {
 bar_num.settext(count + "");
 } else {
 bar_num.settext("99+");
 }
 }
 public void delete() {
 if (count == 0) {
 bar_num.setvisibility(gone);
 } else {
 count--;
 if (count == 0) {
 bar_num.setvisibility(gone);
 } else if (count > 0 && count < 100) {
 bar_num.setvisibility(visible);
 bar_num.settext(count + "");
 } else {
 bar_num.setvisibility(visible);
 bar_num.settext("99+");
 }
 }
 }
 public void deleteall() {
 count = 0;
 bar_num.setvisibility(gone);
 }
}

3.工具类的一个xml布局。

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 >
 <imageview
 android:id="@+id/imggwc"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centervertical="true"
 android:layout_toleftof="@+id/imggenduo"
android:src="@drawable/chaoshi_shopping_nav_icon" />
 <textview
 android:id="@+id/bar_num"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginleft="-12dp"
 android:layout_torightof="@+id/imggwc"
 android:background="@drawable/red_dot_bg"
 android:text="1"
 android:gravity="center"
 android:textcolor="#ffffff"
 android:textsize="10dp" />
</relativelayout>

4.activity的实现

public static bottombarview fragment_bottom_bar;
fragment_bottom_bar = (bottombarview) findviewbyid(r.id.fragment_bottom_bar);
//购物车数量角标数据
 public static final void gwcsl() {
 map<string, string> map = new hashmap<string, string>();
 map.put(constantutil.token, sputil.get(constantutil.token, ""));
 normalpostrequest npr = new normalpostrequest(myurlutils.getfullurl(baseserverconfig.csgwcsl),
 new response.listener<jsonobject>() {
 @override
 public void onresponse(jsonobject response) {
 try {
 string code = response.getstring("code");
 if (baseserverconfig.code_success.equals(code)) {
 //角标数
 int jiaobiao = integer.parseint(response.getstring("resultcode"));
 try {
  fragment_bottom_bar.deleteall();
  if (jiaobiao > 0) {
  fragment_bottom_bar.add(jiaobiao);
  } else {
  fragment_bottom_bar.deleteall();
  }
 } catch (exception e) {
  e.printstacktrace();
 }
 } else {
 }
 } catch (jsonexception e) {
 }
 }
 }, new response.errorlistener() {
 @override
 public void onerrorresponse(volleyerror error) {
 }
 }, map);
 bzapplication.getrequestqueue().add(npr);
 }

5.activity的xml布局

 <relativelayout
 android:id="@+id/csgwcdj"
 android:layout_width="45dp"
 android:layout_height="match_parent"
 android:layout_toleftof="@+id/relative">
 <com.zjtd.bzcommunity.view.bottombarview
 android:id="@+id/fragment_bottom_bar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centervertical="true" />
 </relativelayout>

其实这个小功能很简单,只是你们想得太复杂。。。。。。。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!