购物车实现
基类presenter
public class Basepresenter <V extends Baseview> { private V v; public V getV() { return v; } public void attachview(V v){ this.v = v; } public void detachview(){ v = null; } }
基类activity
public abstract class BaseActivity <P extends Basepresenter> extends AppCompatActivity implements Baseview { private P presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(initlayout()); findid(); presenter = initpresenter(); presenter.attachview(this); initdata(); } protected abstract int initlayout(); protected abstract void findid(); protected abstract P initpresenter(); protected abstract void initdata(); public P getPresenter() { return presenter; }
工具类
public class Netutil { private static Netutil netutil = null; public Apiservice apiservice; public static Netutil getInstance(){ if (netutil == null){ synchronized (Netutil.class){ netutil = new Netutil(); } } return netutil; } private Netutil(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://172.17.27.50/moni/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); apiservice = retrofit.create(Apiservice.class); } }
API类
-
public interface Apiservice {
-
-
@GET("z4month8shopcart")
-
Observable<Shopbean> list();
-
-
@Headers({"sessionId:163065986266694674","userId:94674"})
-
@GET("small/order/verify/v1/findOrderListByStatus")
-
Observable<Orderbean> orde(@QueryMap Map<String,Integer> map);
-
-
-
-
}
cona类
public class Maincona { public interface Mainview extends Baseview{ void onsuccess(List<ResultBean> result); void onerror(String localizedMessage); } public interface Orderview extends Baseview{ void onsuuccess(List<OrderListBean> orderList); void onerror(String localizedMessage); } }
主页面presenter
public class Mainpresenter extends Basepresenter<Maincona.Mainview> { public void shopdata(){ Netutil.getInstance() .apiservice .list() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Shopbean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Shopbean shopbean) { getV().onsuccess(shopbean.getData().getResult()); } @Override public void onError(Throwable e) { getV().onerror(e.getLocalizedMessage()); } @Override public void onComplete() { } }); } }
主页面
public class MainActivity extends BaseActivity<Mainpresenter> implements Maincona.Mainview { @BindView(R.id.recy_main) RecyclerView recyMain; @BindView(R.id.checkall) CheckBox checkall; @BindView(R.id.num_price) TextView numPrice; @BindView(R.id.zhifu) Button zhifu; @BindView(R.id.bianji) Button bianji; private Shangpupter adpter; private List<ResultBean> list; private OkHttpClient client; private boolean b = true; private int totalprice = 0; private int totalcount = 0; @Override protected int initlayout() { return R.layout.activity_main; } @Override protected void findid() { ButterKnife.bind(this); } @Override protected Mainpresenter initpresenter() { return new Mainpresenter(); } @Override protected void initdata() { getPresenter().shopdata(); client = new OkHttpClient(); EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX); recyMain.setLayoutManager(new LinearLayoutManager(this)); adpter = new Shangpupter(this); recyMain.setAdapter(adpter); } @Subscribe(threadMode = ThreadMode.MAIN) public void onno(Integer i) { if (i == 1000) { price(); } } private void price() { boolean a = true; totalprice = 0; totalcount = 0; for (int i = 0; i < list.size(); i ) { list.get(i).setCheck(true); //设置选中 for (int j = 0; j < list.get(i).getShoppingCartList().size(); j ) { boolean ischeck = list.get(i).getShoppingCartList().get(j).isIscheck(); //获取每一个选中的状态 if (!ischeck) { a = false; //如果一个没选中商铺为否,全选也为否 list.get(i).setCheck(false); } else { totalprice = list.get(i).getShoppingCartList().get(j).getPrice() * list.get(i).getShoppingCartList().get(j).getCount(); //计算每一次价格 totalcount = list.get(i).getShoppingCartList().get(j).getCount(); } } } checkall.setChecked(a); //设置是否为全选 numPrice.setText("$" totalprice); //设置价格 adpter.notifyDataSetChanged(); zhifu.setText("结算(" totalcount ")"); } @Override public void onsuccess(List<ResultBean> result) { list = result; adpter.setList(result); } @Override public void onerror(String localizedMessage) { } @Override protected void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override protected void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } @Override protected void onDestroy() { super.onDestroy(); getPresenter().detachview(); } @OnClick({R.id.bianji, R.id.checkall, R.id.zhifu}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.bianji: if (b){ bianji.setText("编辑"); Myapp.ao = 0; b = false; }else { bianji.setText("完成"); Myapp.ao =1; b = true; } adpter.notifyDataSetChanged(); break; case R.id.checkall: boolean checked = checkall.isChecked(); for (int i = 0; i < list.size(); i ) { list.get(i).setCheck(checked); for (int j = 0; j < list.get(i).getShoppingCartList().size(); j ) { list.get(i).getShoppingCartList().get(j).setIscheck(checked); } } price(); break; case R.id.zhifu: qryorderinfo(); zhifu.setEnabled(false); break; } } private void qryorderinfo() { Request request = new Request.Builder() .url("http://172.17.27.50/moni/z4month8alipay?totalPrice=" totalprice) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { } @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { if (response.isSuccessful()) { String ordedata = response.body().string(); Abean abean = new Gson().fromJson(ordedata, Abean.class); if (abean.isSuccess()) { goalipay(abean.getData().getOrderinfo()); } } } }); } private void goalipay(String orderinfo) { Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask(MainActivity.this); Map<String,String> result = alipay.payV2(orderinfo,true); runOnUiThread(new Runnable() { @Override public void run() { if (result.get("resultStatus").equals("9000")) { Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show(); } } }); } }; // 必须异步调用 Thread payThread = new Thread(payRunnable); payThread.start(); } }
商铺适配器
-
public class Shangpupter extends RecyclerView.Adapter<Shangpupter.A> {
-
-
-
private Context context;
-
private List<ResultBean> list = new ArrayList<>();
-
-
public Shangpupter(Context context) {
-
this.context = context;
-
}
-
-
public void setList(List<ResultBean> list) {
-
this.list = list;
-
notifyDataSetChanged();
-
}
-
-
-
-
-
public A onCreateViewHolder(int viewType) { ViewGroup parent,
-
return new A(LayoutInflater.from(context).inflate(R.layout.shangpu, null));
-
}
-
-
-
public void onBindViewHolder(int position) { A holder,
-
holder.nameShangpu.setText(list.get(position).getCategoryName());
-
holder.recyShangpu.setLayoutManager(new LinearLayoutManager(context));
-
Shopadpter adpter = new Shopadpter(context);
-
adpter.setListBeans(list.get(position).getShoppingCartList());
-
holder.recyShangpu.setAdapter(adpter);
-
-
holder.checkShangpu.setChecked(list.get(position).isCheck());
-
holder.checkShangpu.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View view) {
-
boolean checked = holder.checkShangpu.isChecked();
-
list.get(position).setCheck(checked);
-
-
for (int i = 0; i < list.get(position).getShoppingCartList().size(); i ) {
-
list.get(position).getShoppingCartList().get(i).setIscheck(checked);
-
}
-
-
EventBus.getDefault().post(1000);
-
-
}
-
});
-
}
-
-
-
public int getItemCount() {
-
return list.size();
-
}
-
-
class A extends RecyclerView.ViewHolder {
-
-
CheckBox checkShangpu;
-
-
TextView nameShangpu;
-
-
RecyclerView recyShangpu;
-
public A( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this, itemView);
-
}
-
}
-
}
商品适配器
-
public class Shopadpter extends RecyclerView.Adapter<Shopadpter.A> {
-
-
-
-
private Context context;
-
private List<ShoppingCartListBean> listBeans = new ArrayList<>();
-
-
public void setListBeans(List<ShoppingCartListBean> listBeans) {
-
this.listBeans = listBeans;
-
notifyDataSetChanged();
-
}
-
-
public Shopadpter(Context context) {
-
this.context = context;
-
}
-
-
-
-
-
public A onCreateViewHolder(int viewType) { ViewGroup parent,
-
return new A(LayoutInflater.from(context).inflate(R.layout.shop, null));
-
}
-
-
-
public void onBindViewHolder(int position) { A holder,
-
if (Myapp.ao == 0) {
-
holder.addview.setVisibility(View.GONE);
-
holder.shanchu.setVisibility(View.GONE);
-
holder.nameShop.setVisibility(View.VISIBLE);
-
} else {
-
holder.addview.setVisibility(View.VISIBLE);
-
holder.nameShop.setVisibility(View.GONE);
-
holder.shanchu.setVisibility(View.VISIBLE);
-
}
-
-
holder.imgShop.setImageURI(listBeans.get(position).getPic());
-
holder.nameShop.setText(listBeans.get(position).getCommodityName());
-
holder.priceShop.setText("$" listBeans.get(position).getPrice());
-
holder.checkShop.setChecked(listBeans.get(position).isIscheck());
-
holder.addview.setListBean(listBeans.get(position));
-
holder.countShop.setText("X" listBeans.get(position).getCount());
-
holder.shanchu.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View view) {
-
listBeans.remove(listBeans.get(position));
-
-
notifyDataSetChanged();
-
}
-
});
-
-
-
holder.checkShop.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View view) {
-
boolean checked = holder.checkShop.isChecked();
-
-
listBeans.get(position).setIscheck(checked);
-
EventBus.getDefault().post(1000);
-
-
}
-
});
-
-
}
-
-
-
public int getItemCount() {
-
return listBeans.size();
-
}
-
-
class A extends RecyclerView.ViewHolder {
-
-
SimpleDraweeView imgShop;
-
-
TextView nameShop;
-
-
TextView priceShop;
-
-
CheckBox checkShop;
-
-
Addview addview;
-
-
Button shanchu;
-
-
TextView countShop;
-
public A( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this, itemView);
-
}
-
}
-
-
}
订单页面
-
public class Ordeactivity extends BaseActivity<Orderpresenter> implements Maincona.Orderview {
-
-
-
id.tab_order)(R.
-
TabLayout tabOrder;
-
id.order_recy)(R.
-
RecyclerView orderRecy;
-
private String title[] = {"全部","待支付","待收货","待评价","已完成"};
-
private int status[]={0,1,2,3,9};
-
private int page =1,count = 5;
-
private Orderadpter adpter;
-
-
-
protected int initlayout() {
-
return R.layout.activity_ordeactivity;
-
}
-
-
-
protected void findid() {
-
ButterKnife.bind(this);
-
}
-
-
-
protected Orderpresenter initpresenter() {
-
return new Orderpresenter();
-
}
-
-
-
protected void initdata() {
-
-
inittab();
-
-
tabOrder.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
-
-
public void onTabSelected(TabLayout.Tab tab) {
-
requestdata(status[tab.getPosition()]);
-
}
-
-
-
public void onTabUnselected(TabLayout.Tab tab) {
-
-
}
-
-
-
public void onTabReselected(TabLayout.Tab tab) {
-
-
}
-
});
-
-
orderRecy.setLayoutManager(new LinearLayoutManager(this));
-
adpter = new Orderadpter(this);
-
orderRecy.setAdapter(adpter);
-
-
}
-
-
private void requestdata(int status) {
-
getPresenter().ordedata(status,page,count);
-
}
-
-
private void inittab() {
-
for (int i = 0; i < title.length; i ) {
-
View view = LayoutInflater.from(this).inflate(R.layout.tab_order, null);
-
-
TextView text = view.findViewById(R.id.name_tab);
-
-
text.setText(title[i]);
-
-
tabOrder.addTab(tabOrder.newTab().setCustomView(view));
-
-
-
}
-
-
}
-
-
-
public void onsuuccess(List<OrderListBean> orderList) {
-
adpter.setList(orderList);
-
}
-
-
-
public void onerror(String localizedMessage) {
-
-
}
-
-
-
}
订单适配器
-
public class Orderadpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
-
-
-
private Context context;
-
private List<OrderListBean> list = new ArrayList<>();
-
-
private static final int ORDER_TYPE_ALL = 0,
-
ORDER_TYPE_PAY = 1, ORDER_TYPE_COMMENT = 2,
-
ORDER_TYPE_RECEIVE = 3, ORDER_TYPE_FINISH = 9;
-
-
public Orderadpter(Context context) {
-
this.context = context;
-
}
-
-
public void setList(List<OrderListBean> list) {
-
this.list = list;
-
notifyDataSetChanged();
-
}
-
-
-
-
public int getItemViewType(int position) {
-
switch (list.get(position).getOrderStatus()) {
-
case 1:
-
return ORDER_TYPE_PAY;
-
case 2:
-
return ORDER_TYPE_COMMENT;
-
case 3:
-
return ORDER_TYPE_RECEIVE;
-
case 9:
-
return ORDER_TYPE_FINISH;
-
-
-
}
-
return ORDER_TYPE_ALL;
-
}
-
-
-
-
public RecyclerView.ViewHolder onCreateViewHolder(int viewType) { ViewGroup parent,
-
switch (viewType) {
-
case ORDER_TYPE_PAY:
-
return new Payholder(LayoutInflater.from(context).inflate(R.layout.pay_item, null));
-
case ORDER_TYPE_COMMENT:
-
return new Commentholder(LayoutInflater.from(context).inflate(R.layout.com_item,null));
-
case ORDER_TYPE_RECEIVE:
-
return new Reciveholder(LayoutInflater.from(context).inflate(R.layout.rec_item,null));
-
case ORDER_TYPE_FINISH:
-
return new Finsholder(LayoutInflater.from(context).inflate(R.layout.fish_item,null));
-
-
}
-
return null;
-
}
-
-
-
public void onBindViewHolder(int position) { RecyclerView.ViewHolder holder,
-
if (holder instanceof Payholder){
-
loadpaydata((Payholder)holder,list.get(position));
-
}else if (holder instanceof Commentholder){
-
loadcomdata((Commentholder)holder,list.get(position));
-
}else if (holder instanceof Reciveholder){
-
loadrecdata((Reciveholder)holder,list.get(position));
-
}else if (holder instanceof Finsholder){
-
loadfindata((Finsholder)holder,list.get(position));
-
}
-
-
}
-
-
private void loadfindata(Finsholder holder, OrderListBean listBean) {
-
holder.numOrder.setText(listBean.getOrderId());
-
holder.recyOrder.setLayoutManager(new LinearLayoutManager(context));
-
Ordershop order = new Ordershop(context,listBean.getDetailList());
-
holder.recyOrder.setAdapter(order);
-
-
}
-
-
private void loadrecdata(Reciveholder holder, OrderListBean listBean) {
-
holder.numOrder.setText(listBean.getOrderId());
-
holder.recyOrder.setLayoutManager(new LinearLayoutManager(context));
-
Ordershop order = new Ordershop(context,listBean.getDetailList());
-
holder.recyOrder.setAdapter(order);
-
-
}
-
-
private void loadcomdata(Commentholder holder, OrderListBean listBean) {
-
holder.numOrder.setText(listBean.getOrderId());
-
holder.recyOrder.setLayoutManager(new LinearLayoutManager(context));
-
Ordershop order = new Ordershop(context,listBean.getDetailList());
-
holder.recyOrder.setAdapter(order);
-
-
}
-
-
private void loadpaydata(Payholder holder,OrderListBean listBean) {
-
holder.numOrder.setText(listBean.getOrderId());
-
holder.recyOrder.setLayoutManager(new LinearLayoutManager(context));
-
Ordershop order = new Ordershop(context,listBean.getDetailList());
-
holder.recyOrder.setAdapter(order);
-
}
-
-
-
public int getItemCount() {
-
return list.size();
-
}
-
-
-
class Finsholder extends RecyclerView.ViewHolder{
-
-
TextView numOrder;
-
-
RecyclerView recyOrder;
-
-
public Finsholder( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this,itemView);
-
}
-
}
-
-
class Reciveholder extends RecyclerView.ViewHolder{
-
-
-
TextView numOrder;
-
-
RecyclerView recyOrder;
-
public Reciveholder( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this,itemView);
-
}
-
}
-
-
class Commentholder extends RecyclerView.ViewHolder{
-
-
TextView numOrder;
-
-
RecyclerView recyOrder;
-
-
public Commentholder( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this,itemView);
-
}
-
}
-
-
class Payholder extends RecyclerView.ViewHolder {
-
-
-
TextView numOrder;
-
-
RecyclerView recyOrder;
-
public Payholder( { View itemView)
-
super(itemView);
-
ButterKnife.bind(this, itemView);
-
}
-
}
-
}
订单里层
-
public class Ordershop extends RecyclerView.Adapter<Ordershop.A> {
-
-
-
private Context context;
-
private List<DetailListBean> list;
-
-
public Ordershop(Context context, List<DetailListBean> list) {
-
this.context = context;
-
this.list = list;
-
}
-
-
-
-
public A onCreateViewHolder( ViewGroup parent, int viewType) {
-
return new A(LayoutInflater.from(context).inflate(R.layout.ordershop, null));
-
}
-
-
-
public void onBindViewHolder(A holder, int position) {
-
holder.imgOrder.setImageURI(list.get(position).getCommodityPic().split(",")[0]);
-
holder.nameOrder.setText(list.get(position).getCommodityName());
-
holder.priceOrder.setText("$" list.get(position).getCommodityPrice());
-
}
-
-
-
public int getItemCount() {
-
return list.size();
-
}
-
-
class A extends RecyclerView.ViewHolder {
-
R.id.img_order)(
-
SimpleDraweeView imgOrder;
-
R.id.name_order)(
-
TextView nameOrder;
-
R.id.price_order)(
-
TextView priceOrder;
-
-
public A( View itemView) {
-
super(itemView);
-
ButterKnife.bind(this, itemView);
-
}
-
}
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhigjgkj
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22