Android源码之购物车的实现
白羽 2018-06-15 来源 :网络 阅读 570 评论 0

摘要:本文将带你了解Android源码之购物车的实现,希望本文对大家学微信有所帮助。




   

//配置清单文件

   


   

< <strong>uses-permission </strong><strong>android </strong><strong>:name = </strong><strong>“android.permission.INTERNET”</strong> />

   

--------
<activityandroid:name =“.view.MainActivity”> activity> <activityandroid:name =“.view.SplashActivity”> <intent-filter> <actionandroid:name =“ android.intent.action.MAIN“/> <categoryandroid:name =”android.intent.category.LAUNCHER“/> intent-filter> activity>
------------- ------------------------------------------


   

//依赖

   


   

编译<strong>'com.squareup.okhttp3:okhttp:3.3.0'

</strong>编译<strong>'com.google.code.gson:GSON:2.2.4'

</strong>编译<strong>'com.squareup.okio:奥基奥:1.5.0'

</strong>编译<strong>“com.github。 bumptech.glide:滑行:3.6.1'

-------------------------------------------------- --- \

utils的层

</strong>

   


   

<strong>公共类</strong> HttpConfig {

     <strong>公共静态</strong>字符串<em>detail_url</em> = <strong>“https://www.zhaoapi.cn/product/getProductDetail”</strong> ;

    <strong>public static</strong> String <em>add_url</em> = <strong>“https://www.zhaoapi.cn/product/addCart”</strong> ;

    <strong>public static</strong> String <em>cartList_url</em> = <strong>“https://www.zhaoapi.cn/product/getCarts”</strong> ;

}

   

-------------------------------------------------- -----


   

<strong>公共类</strong> OkHttpUtils {

        <strong>私有静态</strong> OkHttpUtils <em>okHttpUtils</em> = <strong>null</strong> ;

          <strong>私人</strong> MyHandler <strong>myHandler</strong> = <strong>新的</strong> MyHandler();

          <strong>私人</strong> OkLoadListener <strong>okLoadListener</strong> ;

 

          <em>//单例

 </em><strong>public static</strong> OkHttpUtils getInstance(){

              <strong>if</strong>(<em>okHttpUtils</em> == <strong>null</strong>){

                 <em>okHttpUtils</em> = <strong>new</strong> OkHttpUtils();<em>          </em>

             }

             <strong>返回</strong><em>okHttpUtils</em> ;

          }

 

          <em>/ /获取

 </em><strong>公共无效</strong> OKGet(字符串网址){<em>          </em>

             OkHttpClient client = <strong>new</strong> OkHttpClient.Builder()。addInterceptor(<strong>new</strong> MyInter())。build();

             Request request = <strong>new</strong> Request.Builder()。url(url).build();

             Call call = client.newCall(request);

             call.enqueue(<strong>new</strong> Callback(){

                 @

 Override <strong>public void</strong> onFailure(Call call,IOException e){               

                   Message message = <strong>myHandler</strong> .obtainMessage();

                   信息。<strong>什么</strong> = 0 ;

                   信息。<strong>obj</strong> = e.getMessage();

                   <strong>myHandler</strong> .sendMessage(message);

                }

 

                @覆盖

 <strong>公共无效</strong> onResponse(电话呼叫,应答响应)<strong>抛出</strong> IOException异常{               

                   Message message = <strong>myHandler</strong> .obtainMessage();

                   信息。<strong>什么</strong> = 1 ;

                   信息。<strong>obj</strong> = response.body()。string();

                   <strong>myHandler</strong> .sendMessage(message);

                }

             });

          }

 

          <em>// post

 </em><strong>public void</strong> okPost(String url,Map <string,string> map){<em>          </em>

             OkHttpClient client = <strong>new</strong> OkHttpClient.Builder()。addInterceptor(<strong>new</strong> MyInter())。build();

             <em>//创建FormBody

</em><em>             </em> FormBody.Builder builder = <strong>new</strong> FormBody.Builder();

             <em>//遍历映射

</em><em>             </em> Set <string> keys = map.keySet();

             <strong>for</strong>(String key:keys){

                String value = map.get(key);

                builder.add(key,value + <strong>“”</strong>);

             }

             <em>// build

</em><em>             </em> FormBody body = builder.build();

             Request request = <strong>new</strong> Request.Builder()。url(url).post(body).build();

             Call call = client.newCall(request);

             call.enqueue(<strong>new</strong> Callback(){

                 @

 Override <strong>public void</strong> onFailure(Call call,IOException e){               

                   Message message = <strong>myHandler</strong> .obtainMessage();

                   信息。<strong>什么</strong> = 0 ;

                   信息。<strong>obj</strong> = e.getMessage();

                   <strong>myHandler</strong> .sendMessage(message);

                }

 

                @覆盖

 <strong>公共无效</strong> onResponse(电话呼叫,应答响应)<strong>抛出</strong> IOException异常{               

                   Message message = <strong>myHandler</strong> .obtainMessage();

                   信息。<strong>什么</strong> = 1 ;

                   信息。<strong>obj</strong> = response.body()。string();

                   <strong>myHandler</strong> .sendMessage(message);

                }

             });

          }

 

          <em>//拦截器

 </em><strong>类</strong> MyInter <strong>实现了</strong> Interceptor {

              <strong>private static final</strong> String <strong><em>TAG</em></strong> = <strong>“MyInter”</strong> ;

             @Override

 <strong>public</strong> response intercept(Chain chain)<strong>throws</strong> IOException {

                 <em>//获取原来的主体

</em> Request request = chain.request();<em>          </em>             <em>                </em>

                RequestBody body = request.body();

                <strong>if</strong>(body <strong>instanceof</strong> FormBody){

                    <em>//遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body

</em><em>                   </em> FormBody.Builder newBuilder = <strong>new</strong> FormBody.Builder();

                   <strong>for</strong>(<strong>int</strong> i = 0 ; i <((FormBody)body).size(); i ++){

                      String name =((FormBody)body).name(i);

                      String value =((FormBody)body).value(i);

 

                      <em>//放入新的

</em><em>                      </em> newBuilder.add(name,value);

                   }

                   <em>//在将公共参数添加

</em><em>                   </em>进去newBuilder.add(<strong>“source”</strong>,<strong>“android”</strong>);

                   FormBody newBody = newBuilder.build();

                   <em>//创建新的请求

</em><em>                   </em>请求newRequest = request.newBuilder()。post(newBody).build();

                   响应响应= chain.proceed(newRequest);

                   <strong>回复</strong>回复;

                }

 

                <strong>返回null</strong> ;

             }

          }

 

          <em>//处理程序

 </em><strong>类</strong> MyHandler的<strong>延伸</strong>处理程序{

              @覆盖

 <strong>公共无效</strong>的handleMessage(消息MSG){

                 <strong>开关</strong>(MSG。<strong>什么</strong>){

                    <strong>情况下</strong>0:

                       <em>//失败

</em>串E =(字符串)味精。<strong>obj</strong> ;

                      <strong>okLoadListener</strong> .okLoadError(e);

                      <strong>打破</strong> ;

                   <strong>情况</strong>1:

                       <em>//成功

</em>字符串json =(字符串)味精。<strong>obj</strong> ;

                      <strong>okLoadListener</strong><em>          </em>             <em>                      </em><em>                      </em>.okLoadSuccess(JSON);

                      <strong>打破</strong> ;

                }

             }

          }

 

          <em>//提高外部调用的接口

 </em><strong>public void</strong> setOkLoadListener(OkLoadListener okLoadListener){

              <strong>this</strong>。<strong>okLoadListener</strong> = okLoadListener;<em>          </em>

          }

}</string></string,string>

   

-------------------------------------------------- ---------


   

<strong>公共接口</strong> OkLoadListener {

     <em>//网络请求成功

 </em><strong>void</strong> okLoadSuccess(String json);

    <em>//网络请求失败

 </em><strong>void</strong> okLoadError(String error);<em>    </em><em>    </em>

}

   



-------------------------------------------------- -----
模型层


   

<strong>公共接口</strong> AddCartListener {

//添加成功

 <strong>void</strong> addCartSuccecc(String json);

//添加失败

<strong>void</strong> addCartError(String json);

}

   

-------------------------------------------------- -------


   

<strong>公共接口</strong> CartListLoadListener {

     <strong>void</strong> cartLoadSuccess(String json);

    <strong>void</strong> cartLoadError(String error);

}

   

-------------------------------------------------- ----


   

<strong>public interface</strong> DetailLoadListener {

     <strong>void</strong> detailLoadSuccess(String json);

    <strong>void</strong> detailLoadError(String json);

}

   

-------------------------------------------------- -----


   

<strong>public interface</strong> Imodel {

     <em>//详细页

 </em><strong>void</strong> getDetailData(String url,Map <string,string> map,DetailLoadListener detailLoadListener);

    <em>//添加购物车

 </em><strong>void</strong> addToCart(String url,Map <string,string> map,AddCartListener addCartListener);

    <em>//查询数据

 </em><strong>void</strong> showDataToCart(String url,Map <string,string> map,CartListLoadListener cartListLoadListener);

    <em>//计算总价

 </em><strong>double</strong> countAll(CartBean cartBean);<em>    </em><em>    </em><em>    </em><em>    </em>

}</string,string></string,string></string,string>

   


   

<strong>公共类</strong> ModelImpl <strong>实现</strong> Imodel {

     @覆盖

 <strong>公共无效</strong> getDetailData(字符串URL,地图<字符串,字符串>地图,<strong>最终</strong> DetailLoadListener detailLoadListener){   

        OkHttpUtils实例= OkHttpUtils。getInstance();

        instance.okPost(URL,地图);

        instance.setOkLoadListener(<strong>新</strong> OkLoadListener(){

             @覆盖

 <strong>公共无效</strong> okLoadSuccess(字符串JSON){

                 <em>//Log.d( “请求数据”,JSON);

 </em>detailLoadListener .detailLoadSuccess(JSON);            <em>                </em>

            }

 

            @覆盖

 <strong>公共无效</strong> okLoadError(字符串错误){

                 detailLoadListener .detailLoadError(误差);           

            }

        });

 

    }

 

    @Override

 <strong>public void</strong> addToCart(String url,Map <string,string> map,<strong>final</strong> AddCartListener addCartListener){   

        OkHttpUtils实例= OkHttpUtils。getInstance();

        instance.okPost(URL,地图);

        实例.setOkLoadListener (<strong>新的</strong> OkLoadListener(){

             @

 Override <strong>公共无效</strong> okLoadSuccess(字符串JSON){

                 addCartListener .addCartSuccecc(JSON);           

            }

 

            @覆盖

 <strong>公共无效</strong> okLoadError(字符串错误){

                 addCartListener .addCartError(误差);           

            }

        });

    }

 

    @Override

 <strong>public void</strong> showDataToCart(String url,Map <string,string> map,<strong>final</strong> CartListLoadListener cartListLoadListener){   

        OkHttpUtils实例= OkHttpUtils。getInstance();

        instance.okPost(URL,地图);

        实例.setOkLoadListener (<strong>新的</strong> OkLoadListener(){

             @

 Override <strong>公共无效</strong> okLoadSuccess(字符串JSON){

                 cartListLoadListener .cartLoadSuccess(JSON);           

            }

 

            @覆盖

 <strong>公共无效</strong> okLoadError(字符串错误){

                 cartListLoadListener .cartLoadError(误差);           

            }

        });

    }

 

    @覆盖

 <strong>公共双</strong> countAll(CartBean cartBean){

         <strong>INT</strong> NUM = 0 ;   

        List <cartbean.databean> data = cartBean.getData();

        <strong>for</strong>(<strong>int</strong> i = 0 ; i <data.size(); cartbean.databean.listbean="" i="" list=""> list = data.get(i).getList();

            <strong>for</strong>(<strong>int</strong> j = 0 ; j <list.size(); j="" strong="">if(list.get(j).getChildflag()){

                     <strong>double</strong> gbp = list.get(j).getPrice();

                    NUM + = GBP;

                }

            }

        }

        <strong>返回</strong>数字;

    }

}</list.size();></data.size();></cartbean.databean></string,string></string,string>

   

-------------------------------------------------- --------------------


   

<del>Prosenter层

</del>

   

   

公共接口IProsenter {

    //详情页

    void showDetailToData(Imodel imodel,IMainView iMainView);

    //添加购物车

    void addToCart(Imodel imodel,IMainView iMainView);

    //购物车数据

    void showDataToCart(Context context,Imodel imodel,ICartView iCartView);

    void countAll(Imodel imodel,ICartView iCartView,CartBean cartBean);

}

   

-------------------------------------------------- --------------------


   

<strong>public class</strong> PreSenterImpl <strong>implements</strong> IProsenter {

     <strong>private static final</strong> String <strong><em>TAG</em></strong> = <strong>“PreSenterImpl”</strong> ;

    @Override

 <strong>public void</strong> showDetailToData(Imodel imodel,<strong>final</strong> IMainView iMainView){

        <em>// Log.d(“请求数据”,“走没走”);

</em>Map <string,string> map = <strong>new</strong> HashMap <string,string>();    <em>        </em>

        map.put(<strong>“pid”</strong>,<strong>“71”</strong>);

        imodel.getDetailData(HttpConfig。<em>detail_url</em>,地图,<strong>新</strong> DetailLoadListener(){

             @覆盖

 <strong>公共无效</strong> detailLoadSuccess(JSON字符串){           

                日志。d(<strong>“请求数据----”</strong>,json);

                Gson gson = <strong>new</strong> Gson();

                DetailBean豆= gson.fromJson(JSON,DetailBean。<strong>类</strong>);

                iMainView .showDetailData(bean);

            }

            @覆盖

 <strong>公共无效</strong> detailLoadError(字符串JSON){           

                日志。d(<strong><em>TAG</em></strong>,<strong>“失败”</strong>);

            }

        });

    }

 

    @Override

 <strong>public void</strong> addToCart(Imodel imodel,<strong>final</strong> IMainView iMainView){   

        Map <string,string> map = <strong>new</strong> HashMap <string,string>();

        map.put(<strong>“pid”</strong>,iMainView.getPid());

        map.put(<strong>“uid”</strong>,<strong>“71”</strong>);

        imodel.addToCart(HttpConfig。<em>add_url</em>,地图,<strong>新</strong> AddCartListener(){

             @覆盖

 <strong>公共无效</strong> addCartSuccecc(JSON字符串){

                 <strong>尝试</strong> {           

                    JSONObject obj = <strong>new</strong> JSONObject(json);

                    String code = obj.getString(<strong>“code”</strong>);

                    <strong>if</strong>(code.equals(<strong>“0”</strong>)){

                         iMainView .showAddSuccsee(json);

                        日志。d(<strong><em>TAG</em></strong>,<strong>“添加数据成功”</strong>);

                    } <strong>其他</strong> {

                         iMainView .showAddError(json);

                        日志。d(<strong><em>TAG</em></strong>,<strong>“添加数据失败”</strong>);

                    }

                } <strong>捕获</strong>(JSONException E){

                    e.printStackTrace();

                }

            }

            @覆盖

 <strong>公共无效</strong> addCartError(字符串JSON){

                 iMainView .showAddError(JSON);           

            }

        });

    }

 

    @Override

 <strong>public void</strong> showDataToCart(<strong>final</strong> Context context,Imodel imodel,<strong>final</strong> ICartView iCartView){   

        Map <string,string> map = <strong>new</strong> HashMap <string,string>();

        map.put(<strong>“uid”</strong>,<strong>“71”</strong>);

        imodel.showDataToCart(HttpConfig。<em>cartList_url</em>,地图,<strong>新</strong> CartListLoadListener(){

             @覆盖

 <strong>公共无效</strong> cartLoadSuccess(JSON字符串){           

                Gson gson = <strong>new</strong> Gson();

                CartBean购物= gson.fromJson(JSON,CartBean。<strong>类</strong>);

                日志。d(<strong>“显示数据”</strong>,cart.toString());

                iCartView .showDataToCart(context,cart);

            }

 

            @覆盖

 <strong>公共无效</strong> cartLoadError(字符串错误){           

 

            }

        });

    }

 

    @覆盖

 <strong>公共无效</strong> countAll(Imodel imodel,ICartView iCartView,CartBean cartBean){

         <strong>双</strong>所有= imodel.countAll(cartBean);   

        日志。d(<strong>“总价格”</strong>,all + <strong>“”</strong>);

            iCartView.showNum(全部);

    }

 

}

</string,string></string,string></string,string></string,string></string,string></string,string>

   

-------------------------------------------------- --------------------------------
//查看层


   

<strong>public class</strong> AnimatorUtils {

     <strong>public static</strong> AnimatorSet setAnimatorSet(Context context,View view){

         <em>//获取屏幕宽度的一半属性动画

 </em><strong>int</strong> pixels = context.getResources()。getDisplayMetrics()。<strong>高度像素</strong> ;<em>        </em>

        ObjectAnimator翻译Y = ObjectAnimator。ofFloat(view,<strong>“translationY”</strong>,0,pixels / 2);

        ObjectAnimator scaleX = ObjectAnimator。ofFloat(view,<strong>“scaleX”</strong>,2.0f,1.0f);

        ObjectAnimator scaleY = ObjectAnimator。ofFloat(view,<strong>“scaleY”</strong>,2.0f,1.0f);

        ObjectAnimator alpha = ObjectAnimator。ofFloat(视图,<strong>“阿尔法”</strong>,0,1.0F);

        ObjectAnimator旋转= ObjectAnimator。ofFloat(视图,<strong>“旋转”</strong>,0,360);

        AnimatorSet set = <strong>new</strong> AnimatorSet();

        set.playTogether(translationY,将scaleX,的scaleY,α,旋转);

        set.setDuration(3000);

        set.start();

        <strong>回报</strong>集;

    }

}

   

-------------------------------------------------- -----------------------------------------


   

<strong>公共类</strong> SplashActivity <strong>扩展</strong> AppCompatActivity {

 

    <strong>私人</strong> ImageView <strong>img</strong> ;

 

    @Override

 <strong>保护无效的</strong> onCreate(Bundle savedInstanceState){

         <strong>超级</strong> .onCreate(savedInstanceState);   

        的setContentView(R.layout <strong><em>activity_splash</em></strong>);

        initViews();

 

    }

    <strong>private void</strong> initViews(){

         <strong>img</strong> =(ImageView)findViewById(<strong><em>R.id。splash_img</em></strong>);

        AnimatorSet set = AnimatorUtils。setAnimatorSet(SplashActivity。<strong>此</strong>,<strong>IMG</strong>);

        set.addListener(<strong>new</strong> AnimatorListenerAdapter(){

             @

 Override <strong>public void</strong> onAnimationEnd(Animator animation){

                 <strong>super</strong> .onAnimationEnd(animation);           

            startActivity(<strong>新的</strong>意向书(SplashActivity。<strong>这</strong>,MainActivity。<strong>类</strong>));

            <em>//加跳转动画

</em><em>                </em> overridePendingTransition(R.anim。<strong><em>输入</em></strong>,R.anim。<strong><em>出来</em></strong>);

                <em>//结束本页面

</em><em>                </em>光洁度();

            }

        });

    }

}

   

-------------------------------------------------- --------------------------------------------


<strong>公共接口</strong> ICartView {

     <strong>void</strong> showDataToCart(Context context,CartBean cartBean);

    <strong>void</strong> showNum(<strong>double</strong> num);

    CartBean getCartBean();

}

   


-------------------------------------------------- -----------------------------------------


   

<strong>公共类</strong> CartActivity <strong>扩展</strong> AppCompatActivity <strong>实现</strong> ICartView {

 

    <strong>private</strong> ExpandableListView <strong>exp</strong> ;

    <strong>私人</strong> CheckBox <strong>框</strong> ;

    <strong>私人</strong> TextView <strong>priceAll</strong> ;

    <strong>私人</strong> CartBean <strong>cartBean</strong> ;

    <strong>私人</strong> MyAdapter <strong>myAdapter</strong> ;

 

    @Override

 <strong>保护无效的</strong> onCreate(Bundle savedInstanceState){

         <strong>超级</strong> .onCreate(savedInstanceState);   

        的setContentView(R.layout <strong><em>activity_cart</em></strong>);

        initViews();

        PreSenterImpl preSenter = <strong>new</strong> PreSenterImpl();

        preSenter.showDataToCart(<strong>this</strong>,<strong>new</strong> ModelImpl(),<strong>this</strong>);

    }

 

    <strong>private void</strong> initViews(){

         <strong>exp</strong> =(ExpandableListView)findViewById(<strong><em>R.id。cart_exp</em></strong>);

        <strong>box</strong> =(CheckBox)findViewById(<strong><em>R.id。cart_box</em></strong>);

        <strong>priceAll</strong> =(TextView)findViewById(<strong><em>R.id。cart_priceAll</em></strong>);

        <strong>box</strong> .setOnClickListener(<strong>new</strong> View.OnClickListener(){ @

 Override

             <strong>public void</strong> onClick(View v){           

                List <cartbean.databean> data = <strong>cartBean</strong> .getData();

                <strong>for</strong>(<strong>int</strong> i = 0 ; i <data.size(); cartbean.databean.listbean="" i="" list=""> list = data.get(i).getList();

                    <strong>for</strong>(<strong>int</strong> j = 0 ; j <list.size(); j="" strong="">myAdapter .notifyDataSetChanged();

                PreSenterImpl preSenter = <strong>new</strong> PreSenterImpl();

                preSenter.countAll(<strong>新</strong> ModelImpl(),CartActivity。<strong>这</strong>,<strong>cartBean</strong>);

            }

        });

    }

        <em>//显示数据

 </em>@Override

 <strong>public void</strong> showDataToCart(Context context,CartBean cartBean){

         <strong>this</strong>。<strong>cartBean</strong> = cartBean;

        <strong>myAdapter</strong> = <strong>new</strong> MyAdapter(context,cartBean,<strong>this</strong>);

        <strong>exp</strong> .setAdapter(<strong>myAdapter</strong>);

        <em>//展开所有的列表

 </em><strong>int</strong> childCount = <strong>myAdapter</strong> .getGroupCount();

        <strong>for</strong>(<strong>int</strong> i = 0 ; i <childcount; i="" strong="">exp .expandGroup(i);<em>    </em>    <em>       </em>

        }

    }

 

    @覆盖

 <strong>公共无效</strong> showNum(<strong>双</strong> NUM){

         <strong>priceAll</strong> .setText( <strong>“ </strong><strong>总价:”</strong> + NUM);   

    }

 

    @覆盖

 <strong>公共</strong> CartBean getCartBean(){

         <strong>返回</strong><strong>cartBean</strong> ;   

    }

}

</childcount;></list.size();></data.size();></cartbean.databean>

   

-------------------------------------------------- ----------------------------


   

<strong>公共接口</strong> IMainView {

     <strong>无效</strong> showDetailData(DetailBean豆);

    <strong>void</strong> addToCart();

    String getPid();

    <strong>void</strong> showAddSuccsee(String json);

    <strong>void</strong> showAddError(String error);

}

   

-------------------------------------------------- ------


   

<strong>公共类</strong> MainActivity <strong>扩展</strong> AppCompatActivity <strong>实现</strong> IMainView {

 

    <strong>私人</strong> ImageView <strong>img</strong> ;

    <strong>私人</strong> TextView <strong>名称</strong> ;

    <strong>私人</strong> TextView <strong>价格</strong> ;

    <strong>私人</strong> TextView <strong>price_to</strong> ;

    <strong>私人</strong> TextView <strong>添加</strong> ;

    <strong>私人</strong> TextView <strong>购物车</strong> ;

    <strong>私人</strong> PreSenterImpl <strong>preSenter</strong> ;

    <strong>私人</strong> DetailBean <strong>豆</strong> ;

 

    @Override

 <strong>保护无效的</strong> onCreate(Bundle savedInstanceState){

         <strong>超级</strong> .onCreate(savedInstanceState);   

        的setContentView(R.layout <strong><em>activity_main</em></strong>);

        initView();

        <strong>preSenter</strong> = <strong>new</strong> PreSenterImpl();

        <strong>preSenter</strong> .showDetailToData(<strong>new</strong> ModelImpl(),<strong>this</strong>);

    }

 

    <strong>private void</strong> initView(){

         <strong>img</strong> =(ImageView)findViewById(<strong><em>R.id。main_img</em></strong>);

        <strong>name</strong> =(TextView)findViewById(<strong><em>R.id。main_name</em></strong>);

        <strong>price</strong> =(TextView)findViewById(<strong><em>R.id。main_price</em></strong>);

        <strong>price_to</strong> =(TextView)findViewById(<strong><em>R.id。main_priceto</em></strong>);

        <strong>add</strong> =(TextView)findViewById(<strong><em>R.id.main_add</em></strong>);

        <strong>cart</strong> =(TextView)findViewById(<strong><em>R.id.main_cart</em></strong>);

        <strong>cart</strong> .setOnClickListener(<strong>new</strong> View.OnClickListener(){ @

 Override

                        <strong>public void</strong> onClick(View v){

                startActivity(<strong>新的</strong>意向书(MainActivity。<strong>这</strong>,CartActivity。<strong>类</strong>));

                overridePendingTransition(R.anim <strong><em>输入</em></strong>,R.anim。<strong><em>出来</em></strong>);

                完();

            }

        });

        <strong>添加</strong> .setOnClickListener(<strong>新</strong> View.OnClickListener(){

             @覆盖

 <strong>公共无效</strong>的onClick(视图v){

                 <strong>主持人</strong> .addToCart(<strong>新</strong> ModelImpl(),MainActivity。<strong>此</strong>);           

            }

        });

    }

 

    <strong>private static final</strong> String <strong><em>TAG</em></strong> = <strong>“MainActivity”</strong> ;

    @Override

 <strong>public void</strong> showDetailData(DetailBean bean){

         <strong>this</strong>。<strong>bean</strong> = bean;   

        日志。d(<strong><em>TAG</em></strong>,<strong>“--- shuju ====”</strong> + bean);

        String images = bean.getData()。getImages();

        String image = images.split(<strong>“.jpg”</strong>)[ 0 ] + <strong>“.jpg”</strong> ;

        滑行。与(MainActivity。<strong>此</strong>).load(图像).into(<strong>IMG</strong>);

        <strong>name</strong> .setText(bean.getData()。getTitle());

        <strong>price</strong> .setText(<strong>“原价:”</strong> + bean.getData()。getPrice());

        <strong>price_to .setText</strong>(<strong>“折扣价”</strong> + bean.getData()。getBargainPrice());

    }

 

    @Override

 <strong>public void</strong> addToCart(){   

 

    }

 

    @Override

 <strong>public</strong> String getPid(){

         <strong>return </strong><strong>bean</strong> .getData()。getPid()+ <strong>“”</strong> ;   

    }

 

    @覆盖

 <strong>公共无效</strong> showAddSuccsee(字符串JSON){   

 

    }

 

    @覆盖

 <strong>公共无效</strong> showAddError(字符串错误){   

 

    }

}

   

-------------------------------------------------- ------------------


   

<strong>公共类</strong> MyAdapter <strong>扩展</strong> BaseExpandableListAdapter {

     <strong>private final</strong>上下文<strong>上下文</strong> ;

    <strong>私人终极</strong> CartBean <strong>cartBean</strong> ;

    <strong>私人最终</strong> ICartView <strong>iCartView</strong> ;

    <strong>私人最终</strong>列表<cartbean.databean> <strong>列表</strong> ;

    <strong>私人</strong> GroupViewHolder <strong>持有人</strong> ;

    <strong>私人</strong> ChildViewHolder <strong>holder1</strong> ;

 

    <strong>公共</strong> MyAdapter(上下文上下文,CartBean cartBean,ICartView iCartView){

     <strong>this</strong>。<strong>context</strong> = context;

    <strong>这个</strong>。<strong>cartBean</strong> = cartBean;

    <strong>这个</strong>。<strong>iCartView</strong> = iCartView;

    <strong>这个</strong>。<strong>list</strong> = cartBean.getData();

    }

 

    @Override

 <strong>public int</strong> getGroupCount(){

         <strong>return </strong><strong>list</strong> .size();   

    }

 

    @Override

 <strong>public int</strong> getChildrenCount(<strong>int</strong> groupPosition){

         <strong>return </strong><strong>list</strong> .get(groupPosition).getList()。size();   

    }

 

    @Override

 <strong>public</strong> Object getGroup(<strong>int</strong> groupPosition){

         <strong>return </strong><strong>list</strong> .get(groupPosition);   

    }

 

    @Override

 <strong>public</strong> Object getChild(<strong>int</strong> groupPosition,<strong>int</strong> childPosition){

         <strong>return </strong><strong>list</strong> .get(groupPosition).getList()。get(childPosition);   

    }

 

    @Override

 <strong>public long</strong> getGroupId(<strong>int</strong> groupPosition){

         <strong>return</strong> groupPosition;   

    }

 

    @Override

 <strong>public long</strong> getChildId(<strong>int</strong> groupPosition,<strong>int</strong> childPosition){

         <strong>return</strong> childPosition;   

    }

 

    @覆盖

 <strong>公共布尔</strong> hasStableIds(){

         <strong>返回false</strong> ;   

    }

 

    @Override

 <strong>public</strong> View getGroupView(<strong>final int</strong> groupPosition,<strong>boolean</strong> isExpanded,View convertView,ViewGroup parent){

         <strong>if</strong>(convertView == <strong>null</strong>){   

            convertView = View。膨胀(<strong>上下文</strong>。,R.layout <strong><em>groupitem</em></strong>,<strong>空</strong>);

            CheckBox group_box = convertView.findViewById(<strong><em>R.id。group_box</em></strong>);

            TextView的组名= convertView.findViewById(R.id <strong><em>GROUP_NAME</em></strong>);

            <strong>holder</strong> = <strong>new</strong> GroupViewHolder(group_box,group_name);

            convertView.setTag(<strong>持有者</strong>);

        } <strong>else</strong> {

              <strong>holder</strong> =(GroupViewHolder)convertView.getTag();

        }

        <strong>持有者</strong> .getGroup_box()。setChecked(<strong>list</strong> .get(groupPosition).getGroupflag());

        <strong>持有者</strong> .getGroup_name()。setText(<strong>list</strong> .get(groupPosition).getSellerName());

        <strong>保持器</strong>。.getGroup_box()setOnClickListener(<strong>新</strong> View.OnClickListener(){

             @覆盖

 <strong>公共无效</strong>的onClick(视图v){

                 <strong>布尔</strong> isCleked = <strong>列表</strong>获得(groupPosition).getGroupflag();           

                isCleked = isCleked!;

                <strong>list</strong> .get(groupPosition).setGroupflag(isCleked);

                List <cartbean.databean.listbean> beans = MyAdapter。<strong>这个</strong>。<strong>list</strong> .get(groupPosition).getList();

                <strong>for</strong>(<strong>int</strong> i = 0 ; i <beans.size(); i="" presenter="<strong" presenterimpl="">new PreSenterImpl();

                preSenter.countAll(<strong>new</strong> ModelImpl(),<strong>iCartView</strong>,<strong>cartBean</strong>);

            }

        });

        <strong>返回</strong> convertView;

    }

 

    @Override

 <strong>public</strong> View getChildView(<strong>final int</strong> groupPosition,<strong>final int</strong> childPosition,<strong>boolean</strong> isLastChild,View convertView,ViewGroup parent){

        <strong>if</strong>(convertView == <strong>null</strong>){   

           convertView = View。膨胀(<strong>上下文</strong>。,R.layout <strong><em>childitem</em></strong>,<strong>空</strong>);

           CheckBox child_box = convertView.findViewById(<strong><em>R.id。child_box</em></strong>);

           ImageView child_img = convertView.findViewById(<strong><em>R.id。child_img</em></strong>);

           TextView child_subhead = convertView.findViewById(<strong><em>R.id。child_subhead</em></strong>);

           TextView child_price = convertView.findViewById(<strong><em>R.id。child_price</em></strong>);

           <strong>holder1</strong> = <strong>新的</strong> ChildViewHolder(child_box,child_img,child_subhead,child_price);

           convertView.setTag(<strong>holder1</strong>);

       } <strong>else</strong> {

             <strong>holder1</strong> =(ChildViewHolder)convertView.getTag();

       }

       <strong>holder1</strong> .getChild_box()。setChecked(<strong>list</strong> .get(groupPosition).getList()。get(childPosition).getChildflag());

        String images = <strong>list</strong> .get(groupPosition).getList()。get(childPosition).getImages();

        String image = images.split(<strong>“.jpg”</strong>)[ 0 ] + <strong>“.jpg”</strong> ;

        日志。d(<strong>“图片”</strong>,图片);

        滑行。与(<strong>上下文</strong>).load(图像).into(<strong>holder1</strong> .getChild_img());

       <strong>holder1</strong> .getChild_subhead()。setText(<strong>list</strong> .get(groupPosition).getList()。get(childPosition).getSubhead());

       <strong>holder1</strong> .getChild_price()。setText(<strong>list</strong> .get(groupPosition).getList()。get(childPosition).getPrice()+ <strong>“”</strong>);

 

        <strong>holder1</strong> .getChild_box()。setOnClickListener(<strong>new</strong> View.OnClickListener(){ @

 Override

             <strong>public void</strong> onClick(View v){

                 <strong>boolean</strong> isCleked = <strong>list</strong> .get(groupPosition).getList()。get(childPosition).getChildflag();           

                isCleked = isCleked!;

                <strong>list</strong> .get(groupPosition).getList()。get(childPosition).setChildflag(isCleked);

                List <cartbean.databean.listbean> beans = MyAdapter。<strong>这个</strong>。<strong>list</strong> .get(groupPosition).getList();

                <strong>布尔</strong>标志= <strong>真</strong> ;

                <strong>for</strong>(<strong>int</strong> i = 0 ; i <beans.size(); i="" strong="">if(!beans.get(i).getChildflag()){

                        flag = <strong>false</strong> ;

                    }

                }

                <strong>list</strong> .get(groupPosition).setGroupflag(flag);

                notifyDataSetChanged();

                PreSenterImpl preSenter = <strong>new</strong> PreSenterImpl();

                preSenter.countAll(<strong>new</strong> ModelImpl(),<strong>iCartView</strong>,<strong>cartBean</strong>);

            }

        });

        <strong>返回</strong> convertView;

    }

 

    @覆盖

 <strong>公共布尔</strong> isChildSelectable(<strong>INT</strong> groupPosition,<strong>INT</strong> childPosition){

         <strong>返回false</strong> ;   

    }

    <strong>class</strong> GroupViewHolder {

         <strong>private</strong> CheckBox <strong>group_box</strong> ;

        <strong>私人</strong> TextView <strong>组名</strong> ;

 

        <strong>public</strong> GroupViewHolder(CheckBox group_box,TextView group_name){

             <strong>this</strong>。<strong>group_box</strong> = group_box;

            <strong>这个</strong>。<strong>group_name</strong> = group_name;

        }

 

        <strong>public</strong> CheckBox getGroup_box(){

             <strong>return </strong><strong>group_box</strong> ;

        }

 

        <strong>public void</strong> setGroup_box(CheckBox group_box){

             <strong>this</strong>。<strong>group_box</strong> = group_box;

        }

 

        <strong>public</strong> TextView getGroup_name(){

             <strong>return </strong><strong>group_name</strong> ;

        }

 

        <strong>public void</strong> setGroup_name(TextView group_name){

             <strong>this</strong>。<strong>group_name</strong> = group_name;

        }

    }

    <strong>class</strong> ChildViewHolder {

        <strong>private</strong> CheckBox <strong>child_box</strong> ;

        <strong>私人</strong> ImageView <strong>child_img</strong> ;

        <strong>私人</strong> TextView的<strong>child_subhead</strong> ;

        <strong>私人</strong> TextView的<strong>child_price</strong> ;

 

        <strong>public</strong> ChildViewHolder(CheckBox child_box,ImageView child_img,TextView child_subhead,TextView child_price){

             <strong>this</strong>。<strong>child_box</strong> = child_box;

            <strong>这个</strong>。<strong>child_img</strong> = child_img;

            <strong>这个</strong>。<strong>child_subhead</strong> = child_subhead;

            <strong>这个</strong>。<strong>child_price</strong> = child_price;

        }

 

        <strong>public</strong> CheckBox getChild_box(){

             <strong>return </strong><strong>child_box</strong> ;

        }

 

        <strong>public void</strong> setChild_box(CheckBox child_box){

             <strong>this</strong>。<strong>child_box</strong> = child_box;

        }

 

        <strong>public</strong> ImageView getChild_img(){

             <strong>return </strong><strong>child_img</strong> ;

        }

 

        <strong>public void</strong> setChild_img(ImageView child_img){

             <strong>this</strong>。<strong>child_img</strong> = child_img;

        }

 

        <strong>public</strong> TextView getChild_subhead(){

             <strong>return </strong><strong>child_subhead</strong> ;

        }

 

        <strong>public void</strong> setChild_subhead(TextView child_subhead){

             <strong>this</strong>。<strong>child_subhead</strong> = child_subhead;

        }

 

        <strong>public</strong> TextView getChild_price(){

             <strong>return </strong><strong>child_price</strong> ;

        }

 

        <strong>public void</strong> setChild_price(TextView child_price){

             <strong>this</strong>。<strong>child_price</strong> = child_price;

        }

    }

}</beans.size();></cartbean.databean.listbean></beans.size();></cart

   

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程