MVP+Retrofit实现二级购物车

导依赖


compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'it.sephiroth.android.library.picasso:picasso:2.5.2.4b'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'org.easybatch:easybatch-gson:5.1.0'
compile 'com.youth.banner:banner:1.4.9'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'org.greenrobot:eventbus:3.1.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'




清单文件中加入权限


  1. <uses-permission android:name="android.permission.INTERNET"/> 


配置application的name属性,ImageLoader初始化

  1. <application 

  2.     android:name=".appli.App" 

  3. </application>  

  1. public class App extends Application{ 

  2.     @Override  
  3.     public void onCreate() {  
  4.         super.onCreate(); 

  5.         ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); 

  6.         ImageLoader.getInstance().init(configuration); 

  7.     }  


布局中需要用到的的布局,在drawable下面新建qujiesuan.xml


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <corners android:radius="200dp"/>  
  5.     <solid android:color="#e53e42"/>  
  6.     <size android:height="60dp" android:width="130dp"/> 

  7. </shape> 



drawable下面还需要加入三张图片

HomeActivity

主界面

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_View"
            android:layout_width="match_parent"
            android:layout_height="800dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:padding="10dp">

            <CheckBox
                android:id="@+id/quanxuan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shopcart_unselected"
                android:button="@null" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="全选"
                android:textSize="23sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:orientation="vertical"
                android:padding="10dp">

                <TextView
                    android:id="@+id/total_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="总价 : ¥0"
                    android:textColor="#e53e42"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/total_num"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0件商品"
                    android:textSize="20sp" />

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/qujiesuan"
                android:gravity="center"
                android:text="去结算"
                android:textColor="#fff"
                android:textSize="25sp" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>
//自定义 按钮+ 1  -   布局


自定义组合控件,,

custom_jiajian.xml


custom_jiajian.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_height="wrap_content">

    <Button
        android:background="#fff"
        android:textSize="20sp"
        android:id="@+id/reverse"
        android:text=""
        android:layout_width="50dp"
        android:layout_height="wrap_content" />

    <EditText
        android:textStyle="bold"
        android:textSize="23sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/count"
        />

    <Button
        android:id="@+id/add"
        android:background="#fff"
        android:textSize="25sp"
        android:text="+"
        android:layout_width="50dp"
        android:layout_height="wrap_content" />
</LinearLayout>
 


适配器的条目的布局,recy_cart_item.xml

com.example.day20_mvp_cart.customView.CustomJiaJian 别忘了改成自己的包名下的,否则会报错

recy_cart_item.xml,需要引入 自定义组合控件,在创建了CustomJiaJian类以后才可以引入

recy_cart_item.xml适配器的布局


Adapter 适配器的布局 recy_cart_item.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/shop_checkbox"
            android:layout_width="50dp"
            android:layout_height="50dp" />

        <TextView
            android:layout_marginLeft="20dp"
            android:text="良品铺子"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="23sp"
            android:textStyle="bold"
            android:id="@+id/shop_name"
            />
    </LinearLayout>

    <LinearLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/item_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/item_face"
            android:src="@mipmap/ic_launcher"
            android:layout_width="120dp"
            android:layout_height="120dp" />

        <LinearLayout
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/item_name"
                android:textSize="20sp"
                android:text="三只松鼠"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:layout_height="0dp"
                />

            <TextView
                android:textColor="#f00"
                android:id="@+id/item_price"
                android:textSize="23sp"
                android:text="299"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                />


            <com.example.samsung.myapplication.utlis.CustomJiaJian
                android:id="@+id/custom_jiajian"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                />
        </LinearLayout>

        <ImageView
            android:id="@+id/item_delete"
            android:layout_marginRight="10dp"
            android:src="@drawable/shopcart_delete"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </LinearLayout>
</LinearLayout>
Bean类      在里面添加自己的3个字段

  1. //自己添加的三个字段  
  2.            private int isFirst = 1;//1为显示商铺, 2为隐藏商铺  
  3.            private boolean item_check;//每个商品的选中状态  
  4.            private boolean shop_check;//商店的选中状态  
  5.   
  6.            public int getIsFirst() {  
  7.                return isFirst;  
  8.            }  
  9.   
  10.            public void setIsFirst(int isFirst) {  
  11.                this.isFirst = isFirst;  
  12.            }  
  13.   
  14.            public boolean isItem_check() {  
  15.                return item_check;  
  16.            }  
  17.   
  18.            public void setItem_check(boolean item_check) {  
  19.                this.item_check = item_check;  
  20.            }  
  21.   
  22.            public boolean isShop_check() {  
  23.                return shop_check;  
  24.            }  
  25.   
  26.            public void setShop_check(boolean shop_check) {  
  27.                this.shop_check = shop_check;  
  28.            } 
自定义View类
CustomJiaJian 

package com.example.samsung.myapplication.utlis;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.samsung.myapplication.R;

/**
 * Created by samsung on 2017/12/19.
 */

public class CustomJiaJian extends LinearLayout {
    private Button reverse;
    private Button add;
    private EditText countEdit;
    private int mCount =1;
    public CustomJiaJian(Context context) {
        super(context);
    }

    public CustomJiaJian(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.custom_jiajian,this);

        reverse = (Button) view.findViewById(R.id.reverse);
        add = (Button) view.findViewById(R.id.add);
        countEdit = (EditText) view.findViewById(R.id.count);

        reverse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = countEdit.getText().toString().trim();
                int count = Integer.valueOf(content);

                if(count>1){
                    mCount = count-1;
                    countEdit.setText(mCount+"");
                    //回调给adapter里面
                    if(customListener!=null){
                        customListener.jiajian(mCount);
                    }
                }
            }
        });

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = countEdit.getText().toString().trim();
                int count = Integer.valueOf(content)+1;
                mCount = count;

                countEdit.setText(mCount+"");

                //接口回调给adapter
                if(customListener!=null){
                    customListener.jiajian(mCount);
                }
            }
        });

    }

    public CustomJiaJian(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    CustomListener customListener;
    public void setCustomListener(CustomListener customListener){
        this.customListener = customListener;
    }

    //加减的接口
    public interface CustomListener{
        public void jiajian(int count);
        public void shuRuZhi(int count);
    }

    //这个方法是供recyadapter设置 数量时候调用的
    public void setEditText(int num) {
        if(countEdit !=null) {
            countEdit.setText(num + "");
        }
    }
}


泛型 IPrensenter

package com.example.samsung.myapplication.utlis;

/**
 * Created by samsung on 2017/12/19.
 */

public interface IPresenter<T> {
   void  attch(T view);
    void  datach();
}
//基类 BaseActivity   与  BaseFragment   在fragment使用就用basefragment  在activity里使用 就用BaseActivity

package com.example.samsung.myapplication.utlis;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;

/**
 * Created by samsung on 2017/12/19.
 */

public abstract class BaseActivity<T extends IPresenter> extends Activity {
    T presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        createpresenter();
    }

    protected abstract void createpresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.datach();
        }
    }
} 

网络接口 

ApiService 

package com.example.samsung.myapplication.utlis;

import com.example.samsung.myapplication.Bean.CartBean;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import rx.Observable;

/**
 * Created by samsung on 2017/12/19.
 */

public interface ApiService {
    @GET("product/getCarts?source=android&pid=57&uid=2351")
    Observable<CartBean> getdata();

    Retrofit RETROFIT = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://120.27.23.105/")
            .build();
    ApiService API_SERVICE = RETROFIT.create(ApiService.class);


}

接下来就是MVP了

View层 的接口

package com.example.samsung.myapplication.view;

import com.example.samsung.myapplication.Bean.CartBean;

/**
 * Created by samsung on 2017/12/19.
 */

public interface IbeanView {

    void  showHome(CartBean cartBean);
}

moder层 的接口

package com.example.samsung.myapplication.moderl;

import com.example.samsung.myapplication.Bean.CartBean;

/**
 * Created by samsung on 2017/12/19.
 */

public interface IHome {

    void showMovie(Movie movie);

    interface Movie {
        void complete(CartBean cartBean);
    }
}
moderl的类

package com.example.samsung.myapplication.moderl;

import com.example.samsung.myapplication.Bean.CartBean;
import com.example.samsung.myapplication.utlis.ApiService;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by samsung on 2017/12/19.
 */

public class HomeModerl implements IHome {
    @Override
    public void showMovie(final Movie movie) {
        ApiService.API_SERVICE.getdata()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CartBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CartBean cartBean) {
                        movie.complete(cartBean);

                    }
                });
    }
}
//presenter层   presenterView

package com.example.samsung.myapplication.presenter;

import com.example.samsung.myapplication.Bean.CartBean;
import com.example.samsung.myapplication.moderl.HomeModerl;
import com.example.samsung.myapplication.moderl.IHome;
import com.example.samsung.myapplication.utlis.IPresenter;
import com.example.samsung.myapplication.view.IbeanView;

import java.lang.ref.SoftReference;

/**
 * Created by samsung on 2017/12/19.
 */

public class PresenterVeiw implements IPresenter<IbeanView> {
    IHome home;
    SoftReference<IbeanView> softReference;

    public PresenterVeiw(IbeanView view) {
        attch(view);
        home = new HomeModerl();
    }

    public void showdata() {
        home.showMovie(new IHome.Movie() {
            @Override
            public void complete(CartBean cartBean) {
                softReference.get().showHome(cartBean);
            }
        });
    }

    @Override
    public void attch(IbeanView view) {
        softReference = new SoftReference<>(view);
    }

    @Override
    public void datach() {
        softReference.clear();
    }


} 


//RecyclerVeiwAdapter适配器   《RecyAdapter》

package com.example.samsung.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.samsung.myapplication.Bean.CartBean;
import com.example.samsung.myapplication.R;
import com.example.samsung.myapplication.utlis.CustomJiaJian;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by samsung on 2017/12/19.
 */

public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.MyViewHolder>{

    Context context;
    //创建大的集合
    private List<CartBean.DataBean.ListBean> list;

    //存放商家的id和商家的名称的map集合
    private Map<String,String> map = new HashMap<>();

    public RecyAdapter(Context context) {
        this.context = context;
    }

    /**
     * 添加数据并更新显示
     * */
    public void add(CartBean cartBean){
        //传进来的是bean对象
        if(list == null){
            list = new ArrayList<>();
        }
        //第一层遍历商家和商品
        for (CartBean.DataBean shop : cartBean.getData()){
            //把商品的id和商品的名称添加到map集合里 ,,为了之后方便调用
            map.put(shop.getSellerid(),shop.getSellerName());
            //第二层遍历里面的商品
            for (int i=0;i<shop.getList().size();i++){
                //添加到list集合里
                list.add(shop.getList().get(i));
            }
        }
        //调用方法 设置显示或隐藏 商铺名
        setFirst(list);
        notifyDataSetChanged();
    }

    /**
     * 设置数据源,控制是否显示商家
     * */
    private void setFirst(List<CartBean.DataBean.ListBean> list) {

        if(list.size()>0){
            //如果是第一条数据就设置isFirst1
            list.get(0).setIsFirst(1);
            //从第二条开始遍历
            for (int i=1;i<list.size();i++){
                //如果和前一个商品是同一家商店的
                if (list.get(i).getSellerid() == list.get(i-1).getSellerid()){
                    //设置成2不显示商铺
                    list.get(i).setIsFirst(2);
                }else{//设置成1显示商铺
                    list.get(i).setIsFirst(1);
                    //如果当前条目选中,把当前的商铺也选中
                    if (list.get(i).isItem_check()==true){
                        list.get(i).setShop_check(list.get(i).isItem_check());
                    }
                }
            }
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.recy_cart_item,null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        /**
         * 设置商铺的 shop_checkbox和商铺的名字 显示或隐藏
         * */
        if(list.get(position).getIsFirst()==1){
            //显示商家
            holder.shop_checkbox.setVisibility(View.VISIBLE);
            holder.shop_name.setVisibility(View.VISIBLE);
            //设置shop_checkbox的选中状态
            holder.shop_checkbox.setChecked(list.get(position).isShop_check());
            holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));
        }else{//2
            //隐藏商家
            holder.shop_name.setVisibility(View.GONE);
            holder.shop_checkbox.setVisibility(View.GONE);
        }

        //拆分images字段
        String[] split = list.get(position).getImages().split("\\|");
        //设置商品的图片
        ImageLoader.getInstance().displayImage(split[0],holder.item_face);
        //控制商品的item_checkbox,,根据字段改变
        holder.item_checkbox.setChecked(list.get(position).isItem_check());
        holder.item_name.setText(list.get(position).getTitle());
        holder.item_price.setText(list.get(position).getPrice()+"");
        //调用customjiajian里面的方法设置 加减号中间的数字
        holder.customJiaJian.setEditText(list.get(position).getNum());

        //商铺的shop_checkbox点击事件 ,控制商品的item_checkbox
        holder.shop_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //先改变数据源中的shop_check
                list.get(position).setShop_check(holder.shop_checkbox.isChecked());

                for (int i=0;i<list.size();i++){
                    //如果是同一家商铺的 都给成相同状态
                    if(list.get(position).getSellerid()==list.get(i).getSellerid()){
                        //当前条目的选中状态 设置成 当前商铺的选中状态
                        list.get(i).setItem_check(holder.shop_checkbox.isChecked());
                    }
                }
                //刷新适配器
                notifyDataSetChanged();
                //调用求和的方法
                sum(list);
            }
        });

        //商品的item_checkbox点击事件,控制商铺的shop_checkbox
        holder.item_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //先改变数据源中的item_checkbox
                list.get(position).setItem_check(holder.item_checkbox.isChecked());

                //反向控制商铺的shop_checkbox
                for (int i=0;i<list.size();i++){
                    for (int j=0;j<list.size();j++){
                        //如果两个商品是同一家店铺的 并且 这两个商品的item_checkbox选中状态不一样
                        if(list.get(i).getSellerid()==list.get(j).getSellerid() && !list.get(j).isItem_check()){
                            //就把商铺的shop_checkbox改成false
                            list.get(i).setShop_check(false);
                            break;
                        }else{
                            //同一家商铺的商品 选中状态都一样,就把商铺shop_checkbox状态改成true
                            list.get(i).setShop_check(true);
                        }
                    }
                }

                //更新适配器
                notifyDataSetChanged();
                //调用求和的方法
                sum(list);
            }
        });

        //删除条目的点击事件
        holder.item_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);//移除集合中的当前数据
                //删除完当前的条目 重新判断商铺的显示隐藏
                setFirst(list);

                //调用重新求和
                sum(list);
                notifyDataSetChanged();
            }
        });

        //加减号的监听,
        holder.customJiaJian.setCustomListener(new CustomJiaJian.CustomListener() {
            @Override
            public void jiajian(int count) {
                //改变数据源中的数量
                list.get(position).setNum(count);
                notifyDataSetChanged();
                sum(list);
            }

            @Override
            //输入值 求总价
            public void shuRuZhi(int count) {
                list.get(position).setNum(count);
                notifyDataSetChanged();
                sum(list);
            }
        });
    }

    /**
     * 计算总价的方法
     * */
    private void sum(List<CartBean.DataBean.ListBean> list){
        int totalNum = 0;//初始的总价为0
        float totalMoney = 0.0f;
        boolean allCheck = true;
        for (int i=0;i<list.size();i++){
            //把 已经选中的 条目 计算价格
            if (list.get(i).isItem_check()){
                totalNum += list.get(i).getNum();
                totalMoney += list.get(i).getNum() * list.get(i).getPrice();
            }else{
                //如果有个未选中,就标记为false
                allCheck = false;
            }
        }

        //接口回调出去 把总价 总数量 和allcheck 传给view        updateListener.setTotal(totalMoney+"",totalNum+"",allCheck);
    }

    //view层调用这个方法, 点击quanxuan按钮的操作
    public void quanXuan(boolean checked) {
        for (int i=0;i<list.size();i++){
            list.get(i).setShop_check(checked);
            list.get(i).setItem_check(checked);

        }
        notifyDataSetChanged();
        sum(list);
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }



    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private final CheckBox shop_checkbox;
        private final TextView shop_name;
        private final CheckBox item_checkbox;
        private final TextView item_name;
        private final TextView item_price;
        private final CustomJiaJian customJiaJian;
        private final ImageView item_delete;
        private final ImageView item_face;

        public MyViewHolder(View itemView) {
            super(itemView);
            shop_checkbox = (CheckBox) itemView.findViewById(R.id.shop_checkbox);
            shop_name = (TextView) itemView.findViewById(R.id.shop_name);
            item_checkbox = (CheckBox) itemView.findViewById(R.id.item_checkbox);
            item_name = (TextView) itemView.findViewById(R.id.item_name);
            item_price = (TextView) itemView.findViewById(R.id.item_price);
            customJiaJian = (CustomJiaJian) itemView.findViewById(R.id.custom_jiajian);
            item_delete = (ImageView) itemView.findViewById(R.id.item_delete);
            item_face = (ImageView) itemView.findViewById(R.id.item_face);
        }
    }

    UpdateListener updateListener;
    public void setUpdateListener(UpdateListener updateListener){
        this.updateListener = updateListener;
    }
    //接口
    public interface UpdateListener{
        public void setTotal(String total,String num,boolean allCheck);
    }
}

HomeActivity  主界面

package com.example.samsung.myapplication;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.samsung.myapplication.Bean.CartBean;
import com.example.samsung.myapplication.adapter.RecyAdapter;
import com.example.samsung.myapplication.presenter.PresenterVeiw;
import com.example.samsung.myapplication.utlis.BaseActivity;
import com.example.samsung.myapplication.view.IbeanView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity<PresenterVeiw> implements IbeanView {


    PresenterVeiw presenter;
    @BindView(R.id.recycler_View)
    RecyclerView recyclerView;
    @BindView(R.id.quanxuan)
    CheckBox quanxuan;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.total_num)
    TextView totalNum;
    private TextView total_price;
    private TextView total_num;
    private RecyAdapter recyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        //http://120.27.23.105/product/getCarts?uid=100

        recyclerView = (RecyclerView) findViewById(R.id.recycler_View);
        total_price = (TextView) findViewById(R.id.total_price);
        total_num = (TextView) findViewById(R.id.total_num);
        quanxuan = (CheckBox) findViewById(R.id.quanxuan);

        quanxuan.setTag(1);//1为不选中
        //new出适配器
        recyAdapter = new RecyAdapter(this);

        presenter = new PresenterVeiw(this);
        //调用presenter里面的请求数据的方法
        presenter.showdata();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(recyAdapter);

        //调用recyAdapter里面的接口,设置 全选按钮 总价 总数量
        recyAdapter.setUpdateListener(new RecyAdapter.UpdateListener() {
            @Override
            public void setTotal(String total, String num, boolean allCheck) {
                //设置ui的改变
                total_num.setText(""+num+"件商品");//总数量
                total_price.setText("总价 :¥"+total+"");//总价
                if(allCheck){
                    quanxuan.setTag(2);
                    quanxuan.setBackgroundResource(R.drawable.shopcart_selected);
                }else{
                    quanxuan.setTag(1);
                    quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
                }
                quanxuan.setChecked(allCheck);
            }
        });

        //这里只做ui更改, 点击全选按钮,,调到adapter里面操作
        quanxuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用adapter里面的方法 ,,把当前quanxuan状态传递过去

                int tag = (int) quanxuan.getTag();
                if(tag==1){
                    quanxuan.setTag(2);
                    quanxuan.setBackgroundResource(R.drawable.shopcart_selected);
                }else{
                    quanxuan.setTag(1);
                    quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
                }

                recyAdapter.quanXuan(quanxuan.isChecked());
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    //实现接口,重写的方法
    @Override
    public void showHome(CartBean cartBean) {
        //拿到返回来的数据 ,, 传给适配器数据
        recyAdapter.add(cartBean);
    }

    @Override
    protected void createpresenter() {
      presenter=new PresenterVeiw(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //调用p层的解除绑定
        presenter.datach();
    }
}


飞思卡尔智能车竞赛是一项备受关注的科技赛事,旨在激发学生的创新和实践能力,尤其是在嵌入式系统、自动控制和机器人技术等关键领域。其中的“电磁组”要求参赛队伍设计并搭建一辆能够自主导航的智能车,通过电磁感应线圈感知赛道路径。本压缩包文件提供了一套完整的电磁组智能车程序,这是一套经过实战验证的代码,曾在校级比赛中获得第二名的优异成绩。 该程序的核心内容可能涉及以下关键知识点: 传感器处理:文件名“4sensor”表明车辆配备了四个传感器,用于获取环境信息。这些传感器很可能是电磁感应传感器,用于探测赛道上的导电线圈。通过分析传感器信号的变化,车辆能够判断自身的行驶方向和位置。 数据采集与滤波:在实际运行中,传感器读数可能受到噪声干扰,因此需要进行数据滤波以提高精度。常见的滤波算法包括低通滤波、高斯滤波和滑动平均滤波等,以确保车辆对赛道的判断准确无误。 路径规划:车辆需要根据传感器输入实时规划行驶路径。这可能涉及PID(比例-积分-微分)控制、模糊逻辑控制或其他现代控制理论方法,从而确保车辆能够稳定且快速地沿赛道行驶。 电机控制:智能车的驱动通常依赖于直流电机或无刷电机,电机控制是关键环节。程序中可能包含电机速度和方向的调节算法,如PWM(脉宽调制)控制,以实现精准的运动控制。 嵌入式系统编程:飞思卡尔智能车的控制器可能基于飞思卡尔微处理器(例如MC9S12系列)。编程语言通常为C或C++,需要掌握微控制器的中断系统、定时器和串行通信等功能。 软件架构:智能车软件通常具有清晰的架构,包括任务调度、中断服务程序和主循环等。理解和优化这一架构对于提升整体性能至关重要。 调试与优化:程序能够在比赛中取得好成绩,说明经过了反复的调试和优化。这可能涉及代码效率提升、故障排查以及性能瓶颈的识别和解决。 团队协作与版本控制:在项目开发过程中,团队协作和版本控制工具(如Git)的应用不可或缺,能够保
双闭环直流电机调速系统是一种高效且应用广泛的直流调速技术。通过设置转速环和电流环两个闭环,系统能够对电机的转速和电流进行精准控制,从而提升动态响应能力和稳定性,广泛应用于工业自动化领域。 主电路设计:主电路采用三相全控桥整流电路,将交流电转换为可调节的直流电,为电机供电。晶闸管作为核心元件,通过调节控制角α实现输出电压的调节。 元部件设计:包括整流变压器、晶闸管、电抗器等元件的设计与参数计算,这些元件的性能直接影响系统的稳定性和效率。 保护电路:设计过载保护、短路保护等保护电路,确保系统安全运行。 驱动电路:设计触发电路和脉冲变压器,触发电路用于触发晶闸管导通,脉冲变压器用于传递触发信号。 控制器设计:系统核心为转速调节器(ASR)和电流调节器(ACR),分别对转速和电流进行调控。检测电路用于采集实际转速和电流值并反馈给调节器。 仿真分析:利用MATLAB/SIMULINK等工具对系统进行仿真分析,验证其稳定性和性能指标是否达标。 方案确定与框图绘制:明确系统构成及各模块连接方式。 主电路设计:选择整流电路形式,设计整流变压器、晶闸管等元部件并计算参数。 驱动电路设计:设计触发电路和脉冲变压器,确保晶闸管准确触发。 控制器设计: 转速调节器(ASR):根据转速指令调整实际转速。 电流调节器(ACR):根据ASR输出指令调整电流,实现快速响应。 参数计算:计算给定电压、调节器、检测电路、触发电路和稳压电路的参数。 仿真分析:通过软件模拟系统运行状态,评估性能。 电气原理图绘制:完成调速控制电路的电气原理图绘制。 双闭环控制策略:转速环在外,电流环在内,形成嵌套结构,提升动态响应能力。 晶闸管控制角调节:通过改变控制角α调节输出电压,实现转速平滑调节。 仿真分析:借助专业软件验证设计的合理性和有效性。 双闭环直流电机调速系统设计涉及主电路、驱动电路和控制器设计等多个环节,通过仿
《编译原理》是计算机科学中一门极为重要的课程,主要探讨如何将高级程序设计语言转换成机器可执行的指令。清华大学的张素琴教授在这一领域有着深厚的学术造诣,其编译原理课后习题答案对于学习者而言是非常珍贵的资源。这份压缩文件详细解析了课程中所涉及的概念、理论和方法的实践应用,目的是帮助学生更好地理解编译器设计的核心内容。 编译原理的核心知识点主要包括以下几点: 词法分析:作为编译过程的首要环节,词法分析器会扫描源代码,识别出一个个称为“标记”(Token)的最小语法单位。通常借助正则表达式来定义各种标记的模式。 语法分析:基于词法分析产生的标记流,语法分析器依据文法规则构建语法树。上下文无关文法(CFG)是编译器设计中常用的一种形式化工具。 语义分析:这一步骤用于理解程序的意义,确保程序符合语言的语义规则。语义分析可分为静态语义分析和动态语义分析,前者主要检查类型匹配、变量声明等内容,后者则关注运行时的行为。 中间代码生成:编译器通常会生成一种高级的中间表示,如三地址码或抽象语法树,以便于后续的优化和目标代码生成。 代码优化:通过消除冗余计算、改进数据布局等方式提升程序的执行效率,同时不改变程序的语义。 目标代码生成:根据中间代码生成特定机器架构的目标代码,这一阶段需要考虑指令集体系结构、寄存器分配、跳转优化等问题。 链接:将编译后的模块进行合并,解决外部引用,最终形成一个可执行文件。 错误处理:在词法分析、语法分析和语义分析过程中,编译器需要能够检测并报告错误,例如语法错误、类型错误等。 张素琴教授的课后习题答案覆盖了上述所有核心知识点,并可能包含实际编程练习,比如实现简单的编译器或解释器,以及针对特定问题的解题策略。通过解答这些习题,学生可以加深对编译原理的理解,提升解决问题的能力,为今后参与编译器开发或软件工程实践奠定坚实的基础。这份资源不仅是学习编译原理的有力辅助材料,也是
内容概要:本文介绍了一种基于环形展开架构的全异步7位逐次逼近寄存器(SAR)模数转换器(ADC),其采样速率为1.75 GS/s,采用3纳米CMOS工艺制造。为了降低功耗并减少设计复杂度,提出了一种无存储器的全异步SAR架构,并引入了双尾反馈(DTFB)动态比较器来满足所需速度并最小化热噪声。该ADC实现了37/49 dB的信噪比(SNDR)/无杂散动态范围(SFDR),面积为0.00055平方毫米,功耗仅为0.69毫瓦。此外,它具有最佳的Walden品质因数(FoMw)为6.9 fJ/转换步长,适用于224 Gb/s PAM4 SerDes接收器中的64路时间交织(TI) ADC系统。 适合人群:从事高速模拟电路设计、SerDes接口开发以及对高精度ADC有研究兴趣的专业人士和研究人员。 使用场景及目标:①适用于需要高带宽、低延迟和低功耗的数据中心网络通信设备;②支持大规模时间交织ADC阵列应用,如高速光纤通信系统;③优化ADC性能,特别是针对PAM4信号处理和高速数据传输的应用。 其他说明:本文详细介绍了ADC的关键技术细节,包括但不限于环形展开SAR架构、DTFB比较器设计、门控提升采样保持开关以及各种校准机制。此外,还展示了实测结果并与现有先进技术进行了对比,证明了所提出的ADC在性能和能效方面的优势。该设计方案不仅在单通道ADC中表现出色,在多通道应用场景下同样具备显著的竞争优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值