ShopShow[
ShopActivity
public class ShopActivity extends AppCompatActivity implements ShopView{
private XRecyclerView xrecyclerview_01;
private Shoppresnter shoppresnter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
//找我们的布局控件
xrecyclerview_01 = (XRecyclerView) findViewById(R.id.xrecyclerview_01);
shoppresnter = new Shoppresnter(this,this);
shoppresnter.jsondata();
}
@Override
public void showshopview(ShopBean shopBean) {
//设置适配器
ShopAdater shopAdater = new ShopAdater(ShopActivity.this, shopBean);
xrecyclerview_01.setAdapter(shopAdater);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
xrecyclerview_01.setLayoutManager(linearLayoutManager);
xrecyclerview_01.setPullRefreshEnabled(false);
//接口回调监听事件
shopAdater.setclick(new ShopAdater.Clicklistener() {
@Override
public void dianjiclick(View view, int position, String pid, String text_title, String text_price, String image) {
//传值
Intent intent = new Intent(ShopActivity.this, AddActivity.class);
intent.putExtra("text_title", text_title);
intent.putExtra("text_price", text_price);
intent.putExtra("pid", pid);
intent.putExtra("image", image);
startActivity(intent);
}
});
}
//销毁
@Override
protected void onDestroy() {
super.onDestroy();
shoppresnter.death();
}
}
ShopAdater
public class ShopAdater extends RecyclerView.Adapter<ShopAdater.viewholder> {
Context context;
ShopBean shopBean;
public ShopAdater(Context context, ShopBean shopBean) {
this.context = context;
this.shopBean = shopBean;
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.shop_layout_01, null);
return new viewholder(view);
}
@Override
public void onBindViewHolder(viewholder holder, final int position) {
final String[] image = shopBean.getData().get(position).getImages().split("\\|");
ImageLoader.getInstance().displayImage(image[0], holder.image_01);
holder.text_price.setText("¥:"+shopBean.getData().get(position).getPrice());
holder.text_title.setText(shopBean.getData().get(position).getSubhead());
//获取pid
final String pid= shopBean.getData().get(position).getPid()+"";
final String text_title=shopBean.getData().get(position).getSalenum()+"";
final String text_price=shopBean.getData().get(position).getSubhead();
//点击事件回调
holder.image_01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicklistener.dianjiclick(view,position,pid,text_price,text_title,image[0]);
}
});
}
@Override
public int getItemCount() {
return shopBean.getData() == null ? 0 : shopBean.getData().size();
}
class viewholder extends RecyclerView.ViewHolder {
private ImageView image_01;
private TextView text_title;
private TextView text_price;
public viewholder(View itemView) {
super(itemView);
image_01 = (ImageView) itemView.findViewById(R.id.image_01);
text_title = (TextView) itemView.findViewById(R.id.text_title);
text_price = (TextView) itemView.findViewById(R.id.text_price);
}
}
Clicklistener clicklistener;
public void setclick(Clicklistener clicklistener) {
this.clicklistener = clicklistener;
}
public interface Clicklistener {
public void dianjiclick(View view,int position,String pid,String text_title,String text_price,String image);
}
}
ShopMmodel
public class ShopMmodel implements Shopmodel {
@Override
public void getshopdta(Callback callback) {
OkHttp3Utils.doGet("http://120.27.23.105/product/searchProducts?keywords=%E6%89%8B%E6%9C%BA&page=1&source=android",callback);
}
}
Shopmodel
public interface Shopmodel {
public void getshopdta(Callback callback);
}
Shoppresnter
public class Shoppresnter {
Context context;
ShopView shopView;
ShopMmodel shopMmodel;
public Shoppresnter(Context context, ShopView shopView) {
this.context = context;
this.shopView = shopView;
shopMmodel = new ShopMmodel();
}
public void jsondata() {
shopMmodel.getshopdta(new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(result, ShopBean.class);
shopView.showshopview(shopBean);
}
});
}
//解绑的操作
public void death(){
shopView=null;
}
}
ShopView
public interface ShopView {
public void showshopview(ShopBean shopBean);
}
]
传值加入购物车
【
AddActivity
public class AddActivity extends AppCompatActivity implements Addview, View.OnClickListener {
private ImageView image_add_view;
private TextView text_add_title;
private TextView text_add_price;
private String pid;
private Button btn_add;
private Button btn_buy;
private Addpresenter addpresenter;
private VideoView videoView;
//是使用handler发送延迟的消息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
Toast.makeText(AddActivity.this, "正在缓冲中。。。。", Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
//找我们的布局控件
image_add_view = (ImageView) findViewById(R.id.image_add_view);
text_add_title = (TextView) findViewById(R.id.text_add_title);
//找我们的布局控件
text_add_price = (TextView) findViewById(R.id.text_add_price);
btn_add = (Button) findViewById(R.id.btn_add);
//找我们的布局控件
btn_add.setOnClickListener(this);
btn_buy = (Button) findViewById(R.id.btn_buy);
btn_buy.setOnClickListener(this);
//接收数据
Intent intent = getIntent();
String name = intent.getStringExtra("text_title");
String price = intent.getStringExtra("text_price");
pid = intent.getStringExtra("pid");
String image = intent.getStringExtra("image");
//设置数据
text_add_title.setText(name);
text_add_price.setText("$:" + price);
//设置图片
Glide.with(AddActivity.this).load(image).into(image_add_view);
//----------播放视频-----------------------------------------------------------
//控件
videoView = (VideoView) this.findViewById(R.id.videoView);
String videoUrl2 = Utils.videoUrl;
//转换
Uri uri = Uri.parse(videoUrl2);
//设置视频控制器
videoView.setMediaController(new MediaController(this));
//播放完成回调
videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());
//设置视频路径
videoView.setVideoURI(uri);
if (videoView != null) {
handler.sendEmptyMessageDelayed(0, 2000);
// Toast.makeText(AddActivity.this, "正在缓冲中。。。。", Toast.LENGTH_SHORT).show();
}
//开始播放视频
videoView.start();
}
class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(AddActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
}
}
@Override
public void showaddview(AddBean addBean) {
//跳转购物车页面
Toast.makeText(this, addBean.getMsg(), Toast.LENGTH_SHORT).show();
if (addBean.getMsg().equals("加购成功")) {
startActivity(new Intent(AddActivity.this, CartShopActivity.class));
}
}
@Override
public void onClick(View view) {//点击事件
switch (view.getId()) {
case R.id.btn_add:
//点击加入购物车
addpresenter = new Addpresenter(this, this);
addpresenter.jsondata(pid);
break;
case R.id.btn_buy:
Toast.makeText(this, "购买成功", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
//视频的网址
public class Utils {
public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
}
//解绑的操作
@Override
protected void onDestroy() {
super.onDestroy();
addpresenter.death();
}
}
AddMmodel
public class AddMmodel implements Addmodel {
@Override
public void getadddata(String pid, Callback callback) {
OkHttp3Utils.doGet("https://www.zhaoapi.cn/product/addCart?pid="+pid+"&uid=3831&token=DED1DEF0B7287F1575BD92B817A6C742&source=android",callback);
}
}
Addmodel
public interface Addmodel {
public void getadddata(String pid, Callback callback);
}
Addpresenter
public class Addpresenter {
Context context;
Addview addview;
AddMmodel addMmodel;
public Addpresenter(Context context, Addview addview) {
this.context = context;
this.addview = addview;
addMmodel = new AddMmodel();
}
public void jsondata(String pid) {
addMmodel.getadddata(pid,new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
AddBean addBean = gson.fromJson(result, AddBean.class);
addview.showaddview(addBean);
}
});
}
//解绑的操作
public void death(){
addview=null;
}
}
Addview
public interface Addview {
public void showaddview(AddBean addBean);
}
CustomVideoView
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//我们重新计算高度
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
@Override
public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
super.setOnPreparedListener(l);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}
】
购物车
【
CartShopActivity
public class CartShopActivity extends AppCompatActivity implements ViewCallBack {
private RecyclerView recyclerView;
private TextView total_price;
private TextView total_num;
private CheckBox quanxuan;
private MyPresenter myPresenter;
private RecyAdapter recyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_shop);
//找我们的布局控件
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);
LinearLayoutManager manager = new LinearLayoutManager(CartShopActivity.this);
//适配器
recyAdapter = new RecyAdapter(this);
myPresenter = new MyPresenter(this);
myPresenter.getData();
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(recyAdapter);
//调用recyAdapter里面的接口,设置 全选按钮 总价 总数量
recyAdapter.setUpdateListener(new RecyAdapter.UpdateListener() {
@Override
public void setTotal(String total, String num, boolean allCheck) {
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
public void success(CartBean cartBean) {
//拿到返回来的数据 ,, 传给适配器数据
recyAdapter.add(cartBean);
}
@Override
public void failure(final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(CartShopActivity.this, "error", Toast.LENGTH_SHORT).show();
}
});
}
//解绑的操作
@Override
protected void onDestroy() {
super.onDestroy();
myPresenter.detach();
}
}
ModelCallBack
public interface ModelCallBack {
public void success(CartBean cartBean);
public void failure(Exception e);
}
MyModel
public class MyModel {
public void getData(final ModelCallBack modelCallBack){
String path = "https://www.zhaoapi.cn/product/getCarts?uid=3831&token=DED1DEF0B7287F1575BD92B817A6C742&source=android";
OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<CartBean>() {
@Override
public void success(CartBean cartBean) {
modelCallBack.success(cartBean);
}
@Override
public void fail(Exception e) {
modelCallBack.failure(e);
}
});
}
}
MyPresenter
public class MyPresenter {
MyModel myModel = new MyModel();
ViewCallBack viewCallBack;
public MyPresenter(ViewCallBack viewCallBack) {
this.viewCallBack = viewCallBack;
}
public void getData() {
myModel.getData(new ModelCallBack() {
@Override
public void success(CartBean cartBean) {
if (viewCallBack != null) {
viewCallBack.success(cartBean);
}
}
@Override
public void failure(Exception e) {
if (viewCallBack != null) {
viewCallBack.failure(e);
}
}
});
}
/**
* 防止内存泄露
*/
public void detach() {
viewCallBack = null;
}
}
RecyAdapter
public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.MyViewHolder> {
Context context;
private List<CartBean.DataBean.ListBean> list;
private Map<String, String> map = new HashMap<>();
public RecyAdapter(Context context) {
this.context = context;
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
}
/**
* 添加数据
*/
public void add(CartBean cartBean) {
if (list == null) {
list = new ArrayList<>();
}
for (CartBean.DataBean shop : cartBean.getData()) {
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) {
list.get(0).setIsFirst(1);
for (int i = 1; i < list.size(); i++) {
if (list.get(i).getSellerid() == list.get(i - 1).getSellerid()) {
list.get(i).setIsFirst(2);
} else {
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) {
/**
* 设置商铺显示或隐藏
* */
if (list.get(position).getIsFirst() == 1) {
holder.shop_checkbox.setVisibility(View.VISIBLE);
holder.shop_name.setVisibility(View.VISIBLE);
holder.shop_checkbox.setChecked(list.get(position).isShop_check());
holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));
} else {
holder.shop_name.setVisibility(View.GONE);
holder.shop_checkbox.setVisibility(View.GONE);
}
String[] split = list.get(position).getImages().split("\\|");
ImageLoader.getInstance().displayImage(split[0], holder.item_face);
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) {
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);
}
});
holder.item_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(position).setItem_check(holder.item_checkbox.isChecked());
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (list.get(i).getSellerid() == list.get(j).getSellerid() && !list.get(j).isItem_check()) {
list.get(i).setShop_check(false);
break;
} else {
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;
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 {
allCheck = false;
}
}
updateListener.setTotal(totalMoney + "", totalNum + "", allCheck);
}
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);
}
}
ViewCallBack
public interface ViewCallBack {
public void success(CartBean cartBean);
public void failure(Exception e);
}
】