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

 更新时间:2017年04月10日 10:39:59   作者:瞳瞳色丶轻烟  
本文主要介绍了Android实现购物车及其他功能的角标的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧

1.先来张效果图

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>

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

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

相关文章

  • Android ListView自动显示隐藏布局的实现方法

    Android ListView自动显示隐藏布局的实现方法

    这篇文章主要介绍了Android ListView自动显示隐藏布局的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android使用个推实现三方应用的推送功能

    Android使用个推实现三方应用的推送功能

    这篇文章主要为大家详细介绍了Android使用个推实现三方应用的推送功能,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Flutter质感设计之模态底部面板

    Flutter质感设计之模态底部面板

    这篇文章主要为大家详细介绍了Flutter质感设计之模态底部面板,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android显示GIF图片实例代码

    Android显示GIF图片实例代码

    这篇文章主要介绍了Android显示GIF图片实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android如何在原生App中嵌入Flutter

    Android如何在原生App中嵌入Flutter

    这篇文章主要介绍了Android如何在原生App中嵌入Flutter,帮助大家更好的理解和学习Android开发,感兴趣的朋友可以了解下
    2021-03-03
  • Android操作系统的架构设计分析

    Android操作系统的架构设计分析

    这篇文章主要介绍了Android操作系统的架构设计分析,Android系统架构分为Linux内核驱动、C/C ++框架、Java框架、Java应用程序,本文分别讲解了它的作用,需要的朋友可以参考下
    2015-06-06
  • Android 10 适配攻略小结

    Android 10 适配攻略小结

    这篇文章主要介绍了Android 10 适配攻略小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Android后端服务器的搭建方法

    Android后端服务器的搭建方法

    本篇文章主要介绍了Android后端服务器的搭建方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 通俗易通讲解Android蓝牙键值适配

    通俗易通讲解Android蓝牙键值适配

    这篇文章介绍了Android蓝牙键值适配的方法,对大家的学习或工作具有一定的参考借鉴价值。需要的朋友可以收藏下,方便下次浏览观看
    2021-12-12
  • android 获取APP的唯一标识applicationId的实例

    android 获取APP的唯一标识applicationId的实例

    下面小编就为大家分享一篇android 获取APP的唯一标识applicationId的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02

最新评论