package fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.andy.share.App;
import java.util.ArrayList;
import java.util.List;
import adapter.ShoppingRecycle;
import bean.ShoppingBean;
import commerce.bick.com.jdcommerce.R;
import presenter.Shop2Presenter;
import presenter.ShopPresenter;
import view.ShopView;
/**
* Created by Administrator-02-08 16 : 20
*/
public class Fragment4_shopping extends Fragment implements View.OnClickListener, ShopView,ShoppingRecycle.ZiShangpin {
private View v;
private RecyclerView recycle;
private LinearLayout ll_shop_jiesuan;//结算
private TextView tv_bianji,tv_heji,tv_lijian,tv_count;
private CheckBox cb_xuan;
private ShopPresenter shopPresenter;
private Shop2Presenter shop2Presenter;
private LinearLayoutManager lam;
private ShoppingRecycle ada;
private List<ShoppingBean> list;
private SharedPreferences sp;
private String spuid;
private int xuanCount;
private int fan=0;
private int numaaa=0;
public int sum; //总数
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = View.inflate(getActivity(), R.layout.fragment_shopping,null);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
initData();
}
private void initData() {
spuid = App.spuid.getString("spuid", null);
sp = getActivity().getSharedPreferences("check", Context.MODE_PRIVATE);
shopPresenter=new ShopPresenter(this,getActivity());
if(App.spuid.getString("spuid",null)==null){
Toast.makeText(getActivity(),"您没有登录,所以不能查看购物车",Toast.LENGTH_SHORT).show();
return;
}
else {
shopPresenter.select(spuid);
}
}
private void initView() {
recycle=v.findViewById(R.id.recycle_shop);
ll_shop_jiesuan=v.findViewById(R.id.ll_shop_jiesuan);
ll_shop_jiesuan.setOnClickListener(this);
tv_bianji=v.findViewById(R.id.tv_shop_bianji);
tv_bianji.setOnClickListener(this);
cb_xuan=v.findViewById(R.id.shop_cb_quanxuan);
cb_xuan.setOnClickListener(this);
tv_count=v.findViewById(R.id.tv_shop_count);
tv_heji=v.findViewById(R.id.tv_shop_hejis);
tv_lijian=v.findViewById(R.id.tv_shop_lijian);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.ll_shop_jiesuan:
Toast.makeText(getActivity(),"结算成功~",Toast.LENGTH_SHORT).show();
break;
case R.id.tv_shop_bianji:
break;
case R.id.tv_shop_hejis:break;
case R.id.tv_shop_lijian:break;
case R.id.shop_cb_quanxuan:
if(sp.getBoolean("c",false)==true){
Toast.makeText(getActivity(),"dianji==全选",Toast.LENGTH_SHORT).show();
for (int i = 0; i <list.size() ; i++) {
for (int j = 0; j <list.get(i).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(i).selerlist.get(j);
shop2Presenter=new Shop2Presenter(this,getActivity());
shop2Presenter.updata(spuid,sb.sellerid,sb.pid,sb.num,"1");
xuanCount++;//请求所有商品次数
}
}
sp.edit().putBoolean("c",false).commit();
}else {
//全选
Toast.makeText(getActivity(),"dianji==全不选",Toast.LENGTH_SHORT).show();
for (int i = 0; i <list.size() ; i++) {
for (int j = 0; j <list.get(i).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(i).selerlist.get(j);
shop2Presenter=new Shop2Presenter(this,getActivity());
shop2Presenter.updata(spuid,sb.sellerid,sb.pid,sb.num,"0");
xuanCount++;//请求所有商品次数
}
}
sp.edit().putBoolean("c",true).commit();
}
break;
}
}
@Override
public void selecta(List<ShoppingBean> list ) {
this.list=list;
if(list.size()==0){
Toast.makeText(getActivity(),"购物车为空",Toast.LENGTH_SHORT).show();
tv_heji.setText(0.0+"");// 价格
tv_count.setText("("+0+")");//数量
}
double a=0;//计算总价格
sum=0;//计算请求过来每一天数据的 选中数量
int countSize=0;//计算所有的商品数量
xuanCount=0;//只有用完了 归零
fan=0;
List<Integer> lista=new ArrayList<>();
for (int i = 0; i <list.size() ; i++) {
// int mmm=0;
for (int j = 0; j <list.get(i).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(i).selerlist.get(j);
//System.out.println("price===="+sb.price);
countSize++;
if(sb.selected.equals("1")){
//mmm++;
sum++; //计算哪一个是选中
try {
a+=sb.price;
}catch (Exception e){
e.printStackTrace();
}
}
}
// if(mmm==list.get(i).selerlist.size()){
// //这个tiao目 就是 全选了
// lista.add(1);
// System.out.println("mmmmmmmm==="+mmm);
// }
// lista.add(0);
}
tv_heji.setText(a+"");// 价格
tv_count.setText("("+sum+")");//数量
if(sum==countSize){//全选
System.out.println("商品数量==="+countSize);
cb_xuan.setChecked(true);
}else {
cb_xuan.setChecked(false);
}
lam = new LinearLayoutManager(getActivity());
recycle.setLayoutManager(lam);
ada = new ShoppingRecycle(getActivity(),list,lista);
recycle.setAdapter(ada);
ada.Suibian(this);
}
@Override
public void delete(String msg) {
Toast.makeText(getActivity(),""+msg,Toast.LENGTH_SHORT).show();
shopPresenter.select(spuid);
}
@Override
public void addy(String msg) {
}
@Override
public void update(String msg) {
//修改返回依次
if(numaaa==sum){
}
shopPresenter=new ShopPresenter(this,getActivity());
shopPresenter.select(spuid);
}
//点击外面回调
@Override
public void success(String uid, String sellerid, String pid, String num, String selected) {
shop2Presenter=new Shop2Presenter(this,getActivity());
shop2Presenter.updata(uid,sellerid,pid,num,selected);
}
//点击里面 回调
@Override
public void neiceng(String uid, String sellerid, String pid, String num, String selected) {
numaaa++;
shop2Presenter=new Shop2Presenter(this,getActivity());
shop2Presenter.updata(uid,sellerid,pid,num,selected);
}
@Override
public void onLoingaClick(int pos, final String pid) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("确定删除吗?");
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
zouni(pid);
}
});
builder.create();
builder.show();
}
private void zouni(String pid) {
shopPresenter=new ShopPresenter(this,getActivity());
shopPresenter.delete(spuid,pid);
}
@Override
public void onClicka(int pos,String pid) {
}
}
package adapter;
import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import com.andy.share.App;
import java.util.List;
import bean.ShoppingBean;
import commerce.bick.com.jdcommerce.R;
/**
* User:白二鹏
* Created by Administrator-10-18 18 : 19
*/
public class ShoppingRecycle extends RecyclerView.Adapter<ShoppingRecycle.MyHolder> implements ShoppingRecycleAdapter.OnItemClickListener {
private Activity context;
private List<ShoppingBean> list;
private SharedPreferences spp;
private List<Integer> lista;
public ShoppingRecycle(Activity context, List<ShoppingBean> list, List<Integer> lista) {
this.context = context;
this.list = list;
this.lista=lista;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context,R.layout.item_gw_ry,null);
MyHolder holder=new MyHolder(view);
return holder;
}
public void Suibian(ZiShangpin ziShangpin){
this.ziShangpin=ziShangpin;
}
private ZiShangpin ziShangpin;
@Override
public void onClick(int position,String pid) {
ziShangpin.onClicka(position,pid);
}
@Override
public void onLongClick(int position,String pid) {
ziShangpin.onLoingaClick(position,pid);
}
// @Override
// public void successa(String uid, String sellerid, String pid, String num, String selected) {
// ziShangpin.neiceng( uid, sellerid, pid, num, selected);
// }
public interface ZiShangpin{
void success(String uid,String sellerid,String pid,String num,String selected);
void neiceng(String uid,String sellerid,String pid,String num,String selected);
void onLoingaClick(int pos,String pid);
void onClicka(int pos,String pid);
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
int num=0;
for (int j = 0; j <list.get(position).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(position).selerlist.get(j);
//Toast.makeText(context,"点击了aa"+list.get(position).selerlist.size(),Toast.LENGTH_SHORT).show();
String selected = sb.selected;
if(selected.equals("1")){
num++;
}
//如果子商品都选中 那么 外面的就会选中
if(num==list.get(position).selerlist.size()){
holder.ckb_gw_shopja_s.setChecked(true);//子商品也全部选中
}else {
holder.ckb_gw_shopja_s.setChecked(false);
}
}
/**
* 下标越界异常
*/
// if(lista.get(position)==1){
// holder.ckb_gw_shopja_s.setChecked(true);
// }else {
// holder.ckb_gw_shopja_s.setChecked(false);
// }
holder.ckb_gw_shopja_s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
Toast.makeText(context,"选中了"+position,Toast.LENGTH_SHORT).show();
for (int j = 0; j <list.get(position).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(position).selerlist.get(j);
if(sb.selected.equals("1")){
return;
}
//自商品选中
ziShangpin.success(App.spuid.getString("spuid", null)
,sb.sellerid,sb.pid,sb.num,"1"
);
}
}else {
Toast.makeText(context,"不选中"+position,Toast.LENGTH_SHORT).show();
for (int j = 0; j <list.get(position).selerlist.size() ; j++) {
ShoppingBean.SelerBean sb = list.get(position).selerlist.get(j);
//自商品选中
ziShangpin.success(App.spuid.getString("spuid", null)
,sb.sellerid,sb.pid,sb.num,"0"
);
}
}
}
});
// holder.ckb_gw_shopja_s.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// spp.edit().putBoolean("ww",false).commit();
// if(spp.getBoolean("ww",false)){
// for (int j = 0; j <list.get(position).selerlist.size() ; j++) {
// ShoppingBean.SelerBean sb = list.get(position).selerlist.get(j);
//
// //自商品选中
// ziShangpin.success(App.spuid.getString("spuid", null)
// ,sb.sellerid,sb.pid,sb.num,"0"
// );
// }
//
// spp.edit().putBoolean("ww",false).commit();
//
// }else {
// //点击 子商品全选
// //Toast.makeText(context,"点击了"+position,Toast.LENGTH_SHORT).show();
// for (int j = 0; j <list.get(position).selerlist.size() ; j++) {
// ShoppingBean.SelerBean sb = list.get(position).selerlist.get(j);
//
// //自商品选中
// ziShangpin.success(App.spuid.getString("spuid", null)
// ,sb.sellerid,sb.pid,sb.num,"1"
// );
// }
//
// spp.edit().putBoolean("ww",true).commit();
// }
// }
// });
holder.tv_gw_shopja_s.setText(list.get(position).sellerName);
//meiyige Recyclerview
LinearLayoutManager lin=new LinearLayoutManager(context);
holder.recycler_gw_ry_s.setLayoutManager(lin);
ShoppingRecycleAdapter ada=new ShoppingRecycleAdapter(context,list.get(position).selerlist);
ada.setOnclick(this);
holder.recycler_gw_ry_s.setAdapter(ada);
//ada.Suibianaa(this);
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder{
private CheckBox ckb_gw_shopja_s;
private TextView tv_gw_shopja_s;
private RecyclerView recycler_gw_ry_s;
public MyHolder(View itemView) {
super(itemView);
ckb_gw_shopja_s=itemView.findViewById(R.id.ckb_gw_shopja_s);
tv_gw_shopja_s=itemView.findViewById(R.id.tv_gw_shopja_s);
recycler_gw_ry_s=itemView.findViewById(R.id.recycler_gw_ry_s);
}
}
}
package 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 android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.List;
import bean.ShoppingBean;
import commerce.bick.com.jdcommerce.R;
import utils.AmmountView;
/**
* User:白二鹏
* Created by Administrator-10-18 18 : 19
*/
public class ShoppingRecycleAdapter extends RecyclerView.Adapter<ShoppingRecycleAdapter.MyHolder> {
private Context context;
private List<ShoppingBean.SelerBean> list;
public ShoppingRecycleAdapter(Context context, List<ShoppingBean.SelerBean> list ) {
this.context = context;
this.list = list;
// Toast.makeText(context,"返回结果="+list.size()+"",Toast.LENGTH_SHORT).show();
// for (int i = 0; i < list.size(); i++) {
// Toast.makeText(context,"返回结果="+list.get(i).price,Toast.LENGTH_SHORT).show();
// }
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.item_gw_ry_item,null);
MyHolder holder=new MyHolder(view);
return holder;
}
public void Suibianaa(ZiShangpinaa ziShangpin){
this.ziShangpina=ziShangpin;
}
private ZiShangpinaa ziShangpina;
public interface ZiShangpinaa{
void successa(String uid,String sellerid,String pid,String num,String selected);
}
//接口声明
private OnItemClickListener mOnItemClickListener;
public void setOnclick(OnItemClickListener mOnItemClickListener){
this.mOnItemClickListener=mOnItemClickListener;
}
//点击事件
public interface OnItemClickListener{
//点击事件
void onClick( int position,String pid);
//长按事件
void onLongClick( int position,String pid);
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
final ShoppingBean.SelerBean selerBean = list.get(position);
/**
* 点击事件
*/
int pos = holder.getLayoutPosition();
final int finalPosition = pos;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnItemClickListener.onClick(finalPosition,selerBean.pid);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mOnItemClickListener.onLongClick(finalPosition,selerBean.pid);
return false;
}
});
// holder.ckb_gw_shop_dan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// if(b){
//
// ziShangpina.successa(App.spuid.getString("spuid", null)
// ,selerBean.sellerid,selerBean.pid,selerBean.num,"1");
//
// }else {
//
// ziShangpina.successa(App.spuid.getString("spuid", null)
// ,selerBean.sellerid,selerBean.pid,selerBean.num,"0");
// }
// }
// });
//拿到对象
String selected = selerBean.selected;
if(selected.equals("1")){
holder.ckb_gw_shop_dan.setChecked(true);
}else {
holder.ckb_gw_shop_dan.setChecked(false);
}
Glide.with(context).load(selerBean.images).into(holder.iv_gw_shop_image);
holder.tv_gw_shop_title.setText(selerBean.title);
holder.tv_gw_shop_nei.setText("颜色:橘色配黑色");
holder.tv_gw_shop_price_dan.setText(list.get(pos).price+"");
holder.amount_view.setGoods_storage(50);
holder.amount_view.setAmount(Integer.parseInt(list.get(pos).num));
holder.amount_view.setOnAmountChangeListener(new AmmountView.OnAmountChangeListener() {
@Override
public void onAmountChange(View view, int amount) {
Toast.makeText(context,""+amount,Toast.LENGTH_SHORT).show();
}
});
// TODO: 2017/10/18
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder{
private CheckBox ckb_gw_shop_dan;
private ImageView iv_gw_shop_image;
private TextView tv_gw_shop_title,tv_gw_shop_nei,tv_gw_shop_price_dan;
private AmmountView amount_view;
public MyHolder(View itemView) {
super(itemView);
ckb_gw_shop_dan=itemView.findViewById(R.id.ckb_gw_shop_dan);
iv_gw_shop_image=itemView.findViewById(R.id.iv_gw_shop_image);
tv_gw_shop_title=itemView.findViewById(R.id.tv_gw_shop_title);
tv_gw_shop_nei=itemView.findViewById(R.id.tv_gw_shop_nei);
tv_gw_shop_price_dan=itemView.findViewById(R.id.tv_gw_shop_price_dana);
amount_view=itemView.findViewById(R.id.amount_view);
TODO: 2017/10/18
}
}
}