尚硅谷Android项目之_硅谷商城项目全套源码解析(七、购物车)

一、简介

承接上文:尚硅谷Android项目之_硅谷商城项目全套源码解析(六、个人中心) 

上篇博客概括的介绍了硅谷商城项目的个人中心模块技术要点。本篇内容给大家讲解硅谷商城项目购物车,购物车模块采用的技术包括:采用自定义技术实现购物车的加减号、采用CartStorage实现购物车业务的处理、采用popupwindow实现购物车的对话框、实现联系客服功能、

采用HTML5Android互调技术实现详情页面、集成支付宝支付功能。


 二、详细资源地址

由于篇幅所限,详情情况见如下地址视频和笔记

项目免费视频讲解下载地址:http://www.atguigu.com/download.shtml

github地址:https://github.com/atguigu01/Shopping

作者:大海哥


三、效果演示



四、技术详解
1、采用自定义技术实现购物车的加减号;
1)布局
<?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:gravity="center_vertical"
   
android:orientation="horizontal"
>

    <ImageView
       
android:id="@+id/iv_sub"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:src="@drawable/goods_sub_btn"
/>

    <TextView
       
android:id="@+id/tv_value"
       
android:layout_width="50dp"
       
android:layout_height="50dp"
       
android:gravity="center"
       
android:text="1"
       
android:textColor="#55000000"
       
android:textSize="20sp"
/>

    <ImageView
       
android:id="@+id/iv_add"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
        
android:src="@drawable/goods_add_btn"
/>
</LinearLayout>
2)属性文件
<?xml version="1.0"encoding="utf-8"?>
<resources>
    <
declare-styleablename="AddSubView">
        <
attr name="value"format="integer|reference"/>
        <
attr name="minValue"format="integer|reference"/>
        <
attr name="maxValue"format="integer|reference"/>
        <
attr name="numberAddBackground"format="reference"/>
        <
attr name="numberSubBackground"format="reference"/>

    </
declare-styleable>
</
resources>
3)代码

public classAddSubViewextends LinearLayoutimplementsView.OnClickListener {
    private ImageView iv_sub;
    private ImageView iv_add;
    private TextView tv_value;
    private Context mContext;
    private int value=1;
    private int minValue=1;
    private int maxValue=10;

    public int getValue() {
        return value;
    }

    public void setValue(intvalue) {
        this.value= value;

       tv_value.setText(value+"");


    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(intminValue) {
        this.minValue= minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(intmaxValue) {
        this.maxValue= maxValue;
    }

    public AddSubView(Contextcontext, AttributeSet attrs) {
        super(context, attrs);
        this.mContext= context;

        View.inflate(mContext,R.layout.add_sub_view,this);
       iv_sub = (ImageView) findViewById(R.id.iv_sub);
        iv_add = (ImageView)findViewById(R.id.iv_add);
        tv_value = (TextView)findViewById(R.id.tv_value);

        //设置点击事件
       
iv_sub.setOnClickListener(this);
        iv_add.setOnClickListener(this);

        if (attrs != null) {
            //取出属性
           
TintTypedArray tintTypedArray = TintTypedArray.obtainStyledAttributes(context,attrs, R.styleable.AddSubView);
            int value =tintTypedArray.getInt(R.styleable.AddSubView_value,0);
            if (value > 0) {
                setValue(value);
            }
            int minValue =tintTypedArray.getInt(R.styleable.AddSubView_minValue,0);
            if (value > 0) {
                setMinValue(minValue);
            }
            int maxValue =tintTypedArray.getInt(R.styleable.AddSubView_maxValue,0);
            if (value > 0) {
                setMaxValue(maxValue);
            }
            Drawable addDrawable =tintTypedArray.getDrawable(R.styleable.AddSubView_numberAddBackground);
            if (addDrawable != null) {
                iv_add.setImageDrawable(addDrawable);
            }
            Drawable subDrawable =tintTypedArray.getDrawable(R.styleable.AddSubView_numberSubBackground);
            if (subDrawable != null) {
                iv_sub.setImageDrawable(subDrawable);
            }
        }
    }

    @Override
   
public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_sub:
                subNumber();
                break;
            case R.id.iv_add:
                addNumber();
                break;
        }

        //回调接口
       
if(changeListener!=null){
            changeListener.numberChange(value);
        }
    }

    private void addNumber() {
        if(value  <maxValue){
            value++;
        }
        tv_value.setText(value+"");

    }

    /**
     *

    
*/
   
private void subNumber() {
        if(value>minValue){
            value--;
        }
        tv_value.setText(value+"");
    }


    public interface OnNumberChangeListener{
        /**
         * 
当按钮被点击的时候回调
        
*/
       
public void numberChange(intvalue);
    }

    private OnNumberChangeListenerchangeListener;

    /**
     *
设置监听数据变化
    
* @param
l
    
*/
   
public void setOnNumberChangeListener(OnNumberChangeListenerl) {
        this.changeListener= l;
    }
}


2、联系客服功能;
1)布局

<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout 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"
>

    <WebView
       
android:id="@+id/webview"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
/>
</RelativeLayout>

2)代码

public classCallCenterActivityextends Activity {
    private WebView webview;

    @Override
   
protected void onCreate(BundlesavedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_center);
        webview = (WebView)findViewById(R.id.webview);
        setWebView(Constants.CALL_CENTER);
    }

    private void setWebView(String url) {

        if (url != null) {
            webview.loadUrl(url);
            //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
           
webview.setWebViewClient(newWebViewClient() {
                @Override
               
public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                   
view.loadUrl(url);
                    return true;
                }
            });
            //启用支持javascript
           
WebSettings settings = webview.getSettings();
           settings.setJavaScriptEnabled(true);

            //优先使用缓存           webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
    }
}

3)连接客服地址

http://www6.53kf.com/webCompany.php?arg=10007377&style=1&kflist=off&[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]&zdkf_type=1&language=zh-cn&charset=gbk&referer=http%3A%2F%2Fwww.atguigu.com%2Fcontant.shtml&keyword=&tfrom=1&tpl=crystal_blue&timeStamp=1479001706368&ucust_id=";

注:其他技术功能详见github中代码。



猜你喜欢

转载自blog.csdn.net/atguigu_com/article/details/53880347