购物车

添加依赖

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
implementation 'com.github.jwkj:LibZXing:v1.0.4'
implementation 'com.github.xiaohaibin:XBanner:1.6.1'
allprojects {
repositories {
    google()
    jcenter()
    maven { url 'https://jitpack.io' }
}
}

API

public static final String url="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=板鞋&page=1&count=10";

接口

public interface ShowView {
void sendView(JSONArray result);
}

model

public interface OnShowLisenter {
    void onShow(JSONArray result);
}

private OnShowLisenter showLisenter;

public void setOnShowLisenter(OnShowLisenter showLisenter) {
    this.showLisenter = showLisenter;
}

Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
                String json = (String) msg.obj;
                try {
                    JSONObject jsonObject=new JSONObject(json);
                    JSONArray result = jsonObject.getJSONArray("result");
                    if (showLisenter!=null){
                        showLisenter.onShow(result);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
};

public void sendmodel(){
   ShowUtils.doGet(API.url, new Callback() {
       @Override
       public void onFailure(Call call, IOException e) {

       }

       @Override
       public void onResponse(Call call, Response response) throws IOException {
           String gson = response.body().string();
           Message message = new Message();
           message.obj=gson;
           message.what=0;
           handler.sendMessage(message);
       }
   });
}

presenter

ShowView showView;
Model model;

public Presenter(ShowView showView) {
    this.showView = showView;
    model=new Model();
}

public void sendPresenter(){
    model.sendmodel();
    model.setOnShowLisenter(new Model.OnShowLisenter() {
        @Override
        public void onShow(JSONArray result) {
            showView.sendView(result);
        }
    });
}

OKHttp

public static ShowUtils showUtils = null;
public ShowUtils() {
}
public static ShowUtils getInstance() {
    if (showUtils == null) {
        synchronized (ShowUtils.class) {
            if (showUtils == null) {
                showUtils = new ShowUtils();
            }
        }
    }
    return showUtils;
}

public static void doGet(String url, Callback callback) {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            Log.i("xxx", message);
        }
    });
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build();
    Request request = new Request.Builder()
            .url(url)
            .build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(callback);
}

自定义控件

private Button sub, add;
private EditText edit_num;
private int num;
public OnSearchGoods onSearchGoods;

//把商品数量放入接口
public interface OnSearchGoods {
    void search(int num);
}

//定义接口实体类
public void setOnSearchGoods(OnSearchGoods onSearchGoods) {
    this.onSearchGoods = onSearchGoods;
}

public NumView(Context context) {
    super(context);
}

public NumView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.layout_num,this);
    sub=findViewById(R.id.sub);
    edit_num=findViewById(R.id.edit_num);
    add=findViewById(R.id.add);
    add.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            num++;
            edit_num.setText(num + "");

        }
    });
    sub.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (num > 1) {
                num--;
                edit_num.setText(num + "");
            } else {
                Toast.makeText(context, "商品数量不能为0", Toast.LENGTH_SHORT).show();
            }

        }
    });
    if (onSearchGoods != null) {
        onSearchGoods.search(num);
    }
}

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

展示数据

    recyclerView=findViewById(R.id.Recycler);
    checkall = findViewById(R.id.checkbox);
    sum = findViewById(R.id.sum);
    buy = findViewById(R.id.buy);
    edit_num = findViewById(R.id.edit_num);
    showPresenter = new Presenter(this);
    showPresenter.sendPresenter();
    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this);
    linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    checkall.setOnClickListener(this);
}


@Override
public void onClick(View v) {

}

@Override
public void sendView(JSONArray result) {
    final DataAdapter dataAdapter = new DataAdapter(this, result, new DataAdapter.OnCheckListener() {
        @Override
        public void onCheck(boolean isCheck, String price) {
            checkall.setChecked(isCheck);
            sum.setText(price+"");
        }
    });
    checkall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                dataAdapter.notifCheckData(true);
            } else {
                dataAdapter.notifCheckData(false);
            }
        }
    });

    dataAdapter.setLongClickLisenter(new DataAdapter.LongClickLisenter() {
        @Override
        public void LongClickLisenter(int postion) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("删除");
            builder.setMessage("是否删除");
            builder.setNegativeButton("取消", null);
            final int finalPostion = postion;
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dataAdapter.del(finalPostion);
                    Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            });
            builder.show();

        }
    });
    recyclerView.setAdapter(dataAdapter);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值