Android實現(xiàn)購物車及其他功能的角標
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的實現(xiàn)
public static BottomBarView fragment_bottom_bar; fragment_bottom_bar = (BottomBarView) findViewById(R.id.fragment_bottom_bar); //購物車數(shù)量角標數(shù)據(jù) 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)) { //角標數(shù) 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>
其實這個小功能很簡單,只是你們想得太復雜。。。。。。。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- Android實現(xiàn)購物車功能
- Android實現(xiàn)的仿淘寶購物車demo示例
- Android實現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能demo示例
- Android把商品添加到購物車的動畫效果(貝塞爾曲線)
- Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
- Android仿外賣購物車功能
- Android制作簡單的普通購物車
- Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實例詳解)
- Android實現(xiàn)購物車添加物品的動畫效果
- Android實現(xiàn)二級列表購物車功能
相關文章
Kotlin協(xié)程低級api startCoroutine與ContinuationInterceptor
這篇文章主要為大家介紹了Kotlin協(xié)程低級api startCoroutine與ContinuationInterceptor示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-015分鐘快速實現(xiàn)Android爆炸破碎酷炫動畫特效的示例
本篇文章主要介紹了5分鐘快速實現(xiàn)Android爆炸破碎酷炫動效的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12android編程實現(xiàn)系統(tǒng)圖片剪裁的方法
這篇文章主要介紹了android編程實現(xiàn)系統(tǒng)圖片剪裁的方法,涉及Android針對圖片的獲取、修改、保存等操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11Android 判斷當前語言環(huán)境是否是中文環(huán)境
本文主要介紹了Android 判斷當前語言環(huán)境是否是中文環(huán)境的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android 中menu同時顯示圖標和文字的實現(xiàn)
這篇文章主要介紹了Android 中menu同時顯示圖標和文字的實現(xiàn)的相關資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能
這篇文章主要為大家詳細介紹了Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06