Android实现购物车及其他功能的角标
作者:瞳瞳色丶轻烟 时间:2021-12-24 10:54:54
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>
其实这个小功能很简单,只是你们想得太复杂。。。。。。。
来源:http://blog.csdn.net/Android_Cll/article/details/54426579
标签:角标
0
投稿
猜你喜欢
Android截屏保存png图片的实例代码
2022-01-26 16:10:11
SpringBoot使用AOP+注解实现简单的权限验证的方法
2022-07-29 00:59:09
C#多线程系列之线程完成数
2021-12-24 03:07:47
Java原生服务器接收上传文件 不使用MultipartFile类
2023-11-09 19:31:36
springboot远程debug调试全过程
2023-11-25 07:05:56
Android监听键盘状态获取键盘高度的实现方法
2023-12-02 16:44:17
Java数据结构之线段树详解
2022-09-03 08:13:32
通过实例学习JAVA对象转成XML输出
2023-01-14 15:25:35
Java网络通信中ServerSocket的设计优化方案
2023-06-14 08:25:53
深入理解Java并发编程之ThreadLocal
2023-11-21 02:43:42
springboot集成nacos读取nacos配置数据的原理
2021-08-09 13:38:25
mybatis源码解读-Java中executor包的语句处理功能
2023-09-03 06:34:16
Spring Cloud Zuul添加过滤器过程解析
2023-02-01 06:37:25
Unity3D实现相机跟随控制
2021-09-03 08:38:01
使用Java将字符串在ISO-8859-1和UTF-8之间相互转换
2022-09-15 15:05:16
Android实现ListView控件的多选和全选功能实例
2023-01-04 02:26:50
史上最简洁C# 生成条形码图片思路及示例分享
2023-08-24 15:50:25
解析Tomcat 6、7在EL表达式解析时存在的一个Bug
2023-07-03 22:02:50
Mybatis批量插入index out of range错误的解决(较偏的错误)
2022-06-11 01:11:51
winform 实现选择文件和选择文件夹对话框的简单实例
2022-02-21 07:05:12