package zjj.bwie.com.shopcar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import zjj.bwie.com.shopcar.adapter.ShopAdapter;
import zjj.bwie.com.shopcar.adapter.ShopCheckedListenter;
import zjj.bwie.com.shopcar.adapter.ShopsCheckedlistener;
import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.common.Api;
import zjj.bwie.com.shopcar.presenter.ShopPresenter;
import zjj.bwie.com.shopcar.view.IShopView;
public class MainActivity extends AppCompatActivity implements IShopView,ShopCheckedListenter {
private XRecyclerView xrecycle_view;
private CheckBox cbox_all;
private TextView tv_money;
private int page=1;
private List<ShopBean.DataBean> list;
private ShopAdapter shopAdapter;
private ShopPresenter shopPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
xrecycle_view = findViewById(R.id.xrecycle_view);
xrecycle_view.setLayoutManager(new LinearLayoutManager(MainActivity.this));
cbox_all = findViewById(R.id.cbox_all);
tv_money = findViewById(R.id.tv_money);
xrecycle_view.setLoadingMoreEnabled(true);
xrecycle_view.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page=1;
initData();
}
@Override
public void onLoadMore() {
page++;
initData();
}
});
cbox_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(cbox_all.isChecked()){
if(list!=null && list.size()>0){
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelected(true);
for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
list.get(i).getList().get(i1).setSelected(true);
}
}
}
}else {
if(list!=null &&list.size()>0){
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelected(false);
for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
list.get(i).getList().get(i1).setSelected(false);
}
}
}
}
shopAdapter.notifyDataSetChanged();
}
});
}
private void initData() {
HashMap<String, String> map = new HashMap<>();
map.put("uid","17403");
map.put("page",page+"");
shopPresenter = new ShopPresenter(this);
shopPresenter.getShops(map,Api.SHOP_SELECT);
}
@Override
public void onsuccess(ShopBean shopBean) {
if(shopBean!=null && shopBean.getData()!=null){
if(page==1){
list = shopBean.getData();
Log.i("aaaaaaa",list+"");
shopAdapter = new ShopAdapter(MainActivity.this, list);
xrecycle_view.setAdapter(shopAdapter);
xrecycle_view.refreshComplete();
}else {
if (shopAdapter!=null){
shopAdapter.addDataPage(list);
}
xrecycle_view.refreshComplete();
}
shopAdapter.setShopCheckedListenter(this);
}
}
@Override
public void onfaliure(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
//全选按钮是否选中的回调
@Override
public void notifyCbox() {
StringBuilder stringBuilder = new StringBuilder();
if (shopAdapter != null) {
for (int i = 0; i < shopAdapter.getList().size(); i++) {
stringBuilder.append(shopAdapter.getList().get(i).isSelected());
for (int i1 = 0; i1 < shopAdapter.getList().get(i).getList().size(); i1++) {
stringBuilder.append(shopAdapter.getList().get(i).getList().get(i1).isSelected());
}
}
}
if (stringBuilder.toString().contains("false")) {
cbox_all.setChecked(false);
} else {
cbox_all.setChecked(true);
}
totalPrice();
}
private void totalPrice() {
double shopsprice=0;
for (int i = 0; i < shopAdapter.getList().size(); i++) {
for (int i1 = 0; i1 < shopAdapter.getList().get(i).getList().size(); i1++) {
//计算总价的关键代码
if(shopAdapter.getList().get(i).getList().get(i1).isSelected()){
ShopBean.DataBean.ListBean listBean = shopAdapter.getList().get(i).getList().get(i1);
shopsprice+=listBean.getBargainPrice()*listBean.getShopsnum();
}
}
}
DecimalFormat decimalFormat = new DecimalFormat();
String format = decimalFormat.format(shopsprice);
tv_money.setText("总价:¥"+format);
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
if (shopPresenter!=null){
shopPresenter.detachView();
}
}
}
//////////////////////////////////////////////////////////////////p层
package zjj.bwie.com.shopcar.presenter;
import android.util.Log;
import java.util.HashMap;
import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.model.IShopModel;
import zjj.bwie.com.shopcar.view.IShopView;
public class ShopPresenter {
private IShopView iShopView;
private IShopModel iShopModel;
public ShopPresenter(IShopView iShopView) {
this.iShopView = iShopView;
this.iShopModel = new IShopModel();
}
public void getShops(HashMap<String,String> map, String url){
iShopModel.getShops(map, url, new IShopModel.ShopsCallBack() {
@Override
public void onSuccess(ShopBean shopBean) {
if(iShopView!=null){
iShopView.onsuccess(shopBean);
}
}
@Override
public void onError(String msg) {
if(iShopView!=null){
iShopView.onfaliure(msg);
}
}
});
}
//解绑
public void detachView(){
this.iShopView=null;
}
}
////////////////////////////////////////////////////////////////m层
package zjj.bwie.com.shopcar.model;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import okhttp3.Response;
import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.common.OkHttpUtils;
public class IShopModel {
Handler handler=new Handler();
public void getShops(HashMap<String,String> map, String url, final ShopsCallBack shopsCallback){
OkHttpUtils.getinstance().post(url, map, new OkHttpUtils.RequestCallBack() {
@Override
public void onSuccess(Response response) {
try {
String s = response.body().string();
if(!TextUtils.isEmpty(s)){
Log.i("ssssss",s+"");
parseResult(s,shopsCallback);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(IOException msg) {
if(shopsCallback!=null){
shopsCallback.onError("网络有异常,请稍后再试");
}
}
});
}
private void parseResult(String s, final ShopsCallBack shopsCallback) {
final ShopBean shopBean = new Gson().fromJson(s, ShopBean.class);
if(shopBean!=null && shopsCallback!=null){
handler.post(new Runnable() {
@Override
public void run() {
shopsCallback.onSuccess(shopBean);
}
});
}
}
public interface ShopsCallBack{
void onSuccess(ShopBean shopBean);
void onError(String msg);
}
}
///////////////////////////////////////////////////////////////////v层
package zjj.bwie.com.shopcar.view;
import zjj.bwie.com.shopcar.bean.ShopBean;
public interface IShopView {
void onsuccess(ShopBean shopBean);
void onfaliure(String msg);
}