自定义电商中购车加减控件
第一步定义布局文件
custom_cart_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/custom_btn_cut_back"
android:layout_width="74dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:text="-"
android:background="@drawable/btn_bg"/>
<TextView
android:id="@+id/custom_text_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="17dp"
android:paddingRight="17dp"
android:text="1"
android:textSize="17sp"/>
<Button
android:id="@+id/custom_btn_insert"
android:layout_width="74dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:text="+"
android:background="@drawable/btn_bg"/>
</LinearLayout>
</LinearLayout>
用到的drawable文件btn_bg
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
</selector>
然后创建一个类名为CustomCartView让它继承自FrameLayout并实现里面的三个方法在init方法中我们使用LayoutInflater去加载custom_cart_layout布局然后把inflate通过addView添加进去
在cutBackQuantity();方法中需要注意的是如果当前有多个商品时可以进行减少,那么当商品数量只剩下一个的时候就不能再让它减少,这时当它小于1的时候直接让它等于1就行了。
insertQuantity();方法当我们直接去添加商品时直接让商品数量++就可以了。
updateGoodsQuantity();方法是用来更新商品数量的更新完之后直接显示在TextView中
package com.ranlegeran.customizeshoppingcart;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CustomCartView extends FrameLayout {
@BindView(R.id.custom_btn_cut_back)
Button mBtnCutBack;
@BindView(R.id.custom_text_quantity)
TextView mTextQuantity;
@BindView(R.id.custom_btn_insert)
Button mBtnInsert;
private int mGoodsQuantity = 1; //商品默认数量
//获取商品数量
public int getGoodsQuantity() {
return mGoodsQuantity;
}
//设置商品数量
public void setGoodsQuantity(int goodsQuantity) {
mGoodsQuantity = goodsQuantity;
}
//定义商品数量监听器
private UpdateGoodsQuantityListener mUpdateGoodsQuantityListener;
public void setUpdateGoodsQuantityListener(UpdateGoodsQuantityListener listener) {
this.mUpdateGoodsQuantityListener = listener;
}
public CustomCartView(Context context) {
this(context,null);
}
public CustomCartView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CustomCartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(); //初始化
}
private void init() {
View inflate = LayoutInflater.from(getContext()).inflate(R.layout.custom_cart_layout, this, false);
ButterKnife.bind(this,inflate); //绑定ButterKnife
addView(inflate);
}
@OnClick({R.id.custom_btn_cut_back,R.id.custom_btn_insert})
public void toClick(View v) {
switch (v.getId()) {
case R.id.custom_btn_cut_back: //减少
cutBackQuantity();
break;
case R.id.custom_btn_insert: //添加
insertQuantity();
break;
}
updateGoodsQuantity(); //更新商品数量
}
/**
* 商品数量更新
*/
private void updateGoodsQuantity() {
mTextQuantity.setText(String.valueOf(mGoodsQuantity));
if (mUpdateGoodsQuantityListener != null) {
mUpdateGoodsQuantityListener.updateGoodsQuantity(mGoodsQuantity);
}
}
private void cutBackQuantity() {
mGoodsQuantity = (mGoodsQuantity - 1 < 1) ? 1 : mGoodsQuantity - 1;
if (mGoodsQuantity <= 1) {
Toast.makeText(getContext(), "已是最小数量", Toast.LENGTH_SHORT).show();
}
Log.e("TAG","--->>>>>>"+mGoodsQuantity);
}
private void insertQuantity() {
++mGoodsQuantity;
Log.e("TAG","+++>>>>>>"+mGoodsQuantity);
}
/**
* 定义点击事件接口
*/
public interface UpdateGoodsQuantityListener {
void updateGoodsQuantity(int quantity);
}
}
最后我们在MainActivity中使用
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<com.ranlegeran.customizeshoppingcart.CustomCartView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.ranlegeran.customizeshoppingcart.CustomCartView>
</LinearLayout>
效果图
本想上传成动态的但手机录屏之后没有成功。。。