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
投稿
猜你喜欢
pom文件中${project.basedir}的使用
2021-12-24 04:24:11
总结C#删除字符串数组中空字符串的几种方法
2022-04-14 03:26:42
Android使用RecyclerView实现水平滚动控件
2021-09-10 20:17:54
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
java中计算字符串长度的方法及u4E00与u9FBB的认识
2022-07-15 18:28:20
Java控制台实现猜拳游戏小游戏
2022-04-20 09:52:55
详解微信小程序 同步异步解决办法
2022-08-14 00:08:58
SpringBoot和Swagger结合提高API开发效率
2023-11-25 01:23:16
Android Retrofit实现多图片/文件、图文上传功能
2021-11-01 00:23:47
Java面试必备八股文整理
2023-11-29 12:03:50
解析mybatis-plus中的resultMap简单使用
2021-09-03 03:53:06
Android如何监听屏幕旋转
2021-12-15 06:12:50
Spring代理对象导致的获取不到原生对象注解的解决
2021-12-05 11:44:19
详解Zookeeper基础知识
2023-07-31 08:03:45
C#打印日志的方法总结
2022-02-08 22:32:00
Android组合控件自定义标题栏
2021-11-04 01:12:36
Unity 按钮事件封装操作(EventTriggerListener)
2022-07-08 10:07:08
解决Intellij IDEA 使用Spring-boot-devTools无效的问题
2023-09-17 20:49:47
Java实现仿淘宝滑动验证码研究代码详解
2022-12-28 00:51:11
Java LinkedList实现班级信息管理系统
2021-06-27 04:00:11