版权声明:本文为宠歆小王子的原创文章,未经宠歆小王子允许不得转载。 https://blog.csdn.net/qq_41698379/article/details/82831279
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView tvShopCartSubmit,tvShopCartSelect,tvShopCartTotalNum;
private RecyclerView rlvShopCart,rlvHotProducts;
private ShopCartAdapter mShopCartAdapter;
private LinearLayout llPay;
private RelativeLayout rlHaveProduct;
private List<ShopBean.DataBean.ListBean> mAllOrderList = new ArrayList<>();
private ArrayList<ShopBean.DataBean.ListBean> mGoPayList = new ArrayList<>();
private List<String> mHotProductsList = new ArrayList<>();
private TextView tvShopCartTotalPrice;
private int mCount,mPosition;
private float mTotalPricel;
private boolean mSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
tvShopCartSelect = findViewById(R.id.tv_shopcart_addselect);
tvShopCartTotalPrice = findViewById(R.id.tv_shopcart_totalprice);
tvShopCartTotalNum = findViewById(R.id.tv_shopcart_totalnum);
rlHaveProduct = findViewById(R.id.rl_shopcart_have);
rlvShopCart = findViewById(R.id.rlv_shopcart);
llPay = findViewById(R.id.ll_pay);
tvShopCartSubmit = findViewById(R.id.tv_shopcart_submit);
rlvShopCart.setLayoutManager(new LinearLayoutManager(this));
mShopCartAdapter = new ShopCartAdapter(this, mAllOrderList);
rlvShopCart.setAdapter(mShopCartAdapter);
//监控全选按钮
mShopCartAdapter.setmOnResfreshListener(new OnResfreshListener(){
@Override
public void onResfresh(boolean isSelect) {
mSelect = isSelect;
if (isSelect){
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
}else {
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
}
//总计
float mTotalPrice = 0;
int mTotalNum = 0;
mTotalPrice = 0;
mGoPayList.clear();
for (int i = 0;i<mAllOrderList.size();i++){
if (mAllOrderList.get(i).getSelected() == 0){
mTotalPrice += mAllOrderList.get(i).getPrice()+mAllOrderList.get(i).getNum();
mTotalNum+=1;
mGoPayList.add(mAllOrderList.get(i));
}
mTotalPricel = mTotalPrice;
tvShopCartTotalPrice.setText("总价:"+mTotalPrice);
tvShopCartTotalNum.setText("共"+mTotalNum+"件商品");
}
}
});
tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//反选
mSelect = !mSelect;
if (mSelect){
//全部选中
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
for (ShopBean.DataBean.ListBean listBean : mAllOrderList){
listBean.setSelected(0);
listBean.setShopSelect(true);
}
}else {
//全部取消
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
for (ShopBean.DataBean.ListBean listBean : mAllOrderList){
listBean.setSelected(1);
listBean.setShopSelect(false);
}
}
mShopCartAdapter.notifyDataSetChanged();
}
});
initData();
}
private void initData() {
//构造数据
OkHttpClient client = new OkHttpClient();
client.newCall(new Request.Builder().url("https://www.zhaoapi.cn/product/getCarts?uid=71").build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(response.body().string(), ShopBean.class);
Log.i(TAG,"shopBean:"+shopBean.getMsg());
for (ShopBean.DataBean dataBean:shopBean.getData()){
for (ShopBean.DataBean.ListBean listBean :dataBean.getList()){
listBean.setShopName(dataBean.getSellerName());
mAllOrderList.add(listBean);
}
}
isSelectFirst(mAllOrderList);
runOnUiThread(new Runnable() {
@Override
public void run() {
mShopCartAdapter.notifyDataSetChanged();
}
});
}
});
}
//判断是不是第一个商品,显示商铺
public static void isSelectFirst(List<ShopBean.DataBean.ListBean> list) {
//判断是否有商品
if (list.size()>0){
list.get(0).setFirst(true);
for (int i=1;i<list.size();i++){
if (list.get(i).getSellerid()==list.get(i-1).getSellerid()){
list.get(i).setFirst(false);
}else {
list.get(i).setFirst(true);
}
}
}
}
}
MainActivity
ShopCartAdapter
public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartHolder> {
private Context context;
private List<ShopBean.DataBean.ListBean> data;
public ShopCartAdapter(Context context, List<ShopBean.DataBean.ListBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public ShopCartHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ShopCartHolder(LayoutInflater.from(context).inflate(R.layout.shop_cart_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ShopCartHolder holder, final int position) {
//商品图片
Glide.with(context).load(data.get(position).getImages().split("\\|")[0]).into(holder.ivShopCartClothPic);
//商品信息
holder.tvShopCartClothName.setText(data.get(position).getShopName());
holder.tvShopCartClothPrice.setText("¥"+data.get(position).getPrice());
holder.etShopCartClothNum.setText(data.get(position).getNum()+"");
//显示前面的选中状态
if (data.get(position).getSelected()==0){
holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
}else {
holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
}
if (data.get(position).getSelected() == 0){
holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
}else {
holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
}
//判断是否显示商铺
if (position>0){
//判断是否是一个商铺的商品
if (data.get(position).getSellerid()==data.get(position-1).getSellerid()){
holder.llShopCartHeader.setVisibility(View.GONE);
}else {
holder.llShopCartHeader.setVisibility(View.VISIBLE);
}
}else {
holder.llShopCartHeader.setVisibility(View.VISIBLE);
}
//判断是否全选和计算
if (mOnResfreshListener !=null){
boolean isSelect = false;
for (int i = 0;i<data.size();i++){
if (data.get(i).getSelected()==1){
isSelect = false;
break;
}else {
isSelect = true;
}
}
mOnResfreshListener.onResfresh(isSelect);
}
//商品数量加
holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setNum(data.get(position).getNum()+1);
notifyDataSetChanged();
}
});
//商品数量减
holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).getNum()>1){
data.get(position).setNum(data.get(position).getNum()-1);
notifyDataSetChanged();
}
}
});
//删除
holder.ivShopCartClothDelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.remove(position);
MainActivity.isSelectFirst(data);
notifyDataSetChanged();
}
});
//单个商品选中状态
holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setSelected(data.get(position).getSelected()==0 ? 1:0);
//通过循环找出不同的第一个商品位置
for (int i = 0;i<data.size();i++){
if (data.get(i).isFirst()){
for (int j = 0;j<data.size();j++){
if (data.get(j).getSellerid() == data.get(i).getSellerid()&&data.get(j).getSelected()==1){
data.get(i).setShopSelect(false);
break;
}else {
data.get(i).setShopSelect(true);
}
}
}
}
notifyDataSetChanged();
}
});
//商铺选中状态
holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).isFirst()){
data.get(position).setShopSelect(!data.get(position).isShopSelect());
for (int i = 0;i<data.size();i++){
if (data.get(i).getSellerid() == data.get(position).getSellerid()){
data.get(i).setSelected(data.get(position).isShopSelect()?0:1);
}
}
notifyDataSetChanged();
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
//刷新接口
private OnResfreshListener mOnResfreshListener;
public void setmOnResfreshListener(OnResfreshListener mOnResfreshListener) {
this.mOnResfreshListener = mOnResfreshListener;
}
}
ShopCartAdapter
event OnResfreshListener
public class OnResfreshListener {
public void onResfresh(boolean isSelect){
}
}
event OnResfreshListener
holder ShopCartHolder
public class ShopCartHolder extends RecyclerView.ViewHolder{
public ImageView ivShopCartShopSel;
public TextView tvShopCartShopName;
public TextView tvShopCartClothName;
public TextView tvShopCartClothPrice;
public TextView etShopCartClothNum;
public TextView tvShopCartClothColor;
public TextView tvShopCartClothSize;
public ImageView ivShopCartClothSel;
public ImageView ivShopCartClothMinus;
public ImageView ivShopCartClothAdd;
public ImageView ivShopCartClothDelect;
public ImageView ivShopCartClothPic;
public LinearLayout llShopCartHeader;
public ShopCartHolder(View itemView) {
super(itemView);
llShopCartHeader = itemView.findViewById(R.id.ll_shopcart_header);
ivShopCartShopSel = itemView.findViewById(R.id.iv_item_shopcart_shopselect);
tvShopCartShopName = itemView.findViewById(R.id.tv_item_shopcart_shopname);
tvShopCartClothName = itemView.findViewById(R.id.tv_item_shopcart_clothname);
tvShopCartClothPrice = itemView.findViewById(R.id.tv_item_shopcart_cloth_price);
etShopCartClothNum = itemView.findViewById(R.id.et_item_shopcart_cloth_num);
tvShopCartClothColor = itemView.findViewById(R.id.tv_item_shopcart_cloth_color);
tvShopCartClothSize = itemView.findViewById(R.id.tv_item_shopcart_cloth_size);
ivShopCartClothSel = itemView.findViewById(R.id.tv_item_shopcart_clothselect);
ivShopCartClothMinus = itemView.findViewById(R.id.iv_item_shopcart_cloth_minus);
ivShopCartClothAdd = itemView.findViewById(R.id.iv_item_shopcart_cloth_add);
ivShopCartClothPic = itemView.findViewById(R.id.iv_item_shopcart_cloth_pic);
ivShopCartClothDelect = itemView.findViewById(R.id.iv_item_shopcart_cloth_delete);
}
}
holder ShopCartHolder
Model MainModel
public interface MainModel {
void onSuccess(ShopBean shopBean);
void onError(int code);
}
Model MainModel
Model MyModel
public class MyModel {
private String url = "https://www.zhaoapi.cn/product/getCarts?uid=71";
public void getData(final MainModel mainModel){
OKHttpUtils.doget(url, new Callback() {
@Override
public void onFailure(Call call, IOException epppnc0) {
mainModel.onError(1);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
//json解析
ShopBean shopBean = new Gson().fromJson(string, ShopBean.class);
mainModel.onSuccess(shopBean);
}
});
}
}
Model MyModel
presenter MainPresenter
public interface MainPresenter {
void desc();
}
presenter MainPresenter
presenter MyPresenter
public class MyPresenter{
private WeakReference<MainModel> modelWeakReference;
private WeakReference<MainView> viewWeakReference;
public void showData(String url){
modelWeakReference.get().hashCode();
}
}
presenter MyPresenter