标签:tail ++ app vco imageview resource serve gone hashmap
//在activity根据arguments完成对fragment的书写,fragment通过不同的arguments来创建去请求不同的数据
package com.example.qrcodesample1.view.activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.qrcodesample1.R;
import com.example.qrcodesample1.view.fragment.OrderFormFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
public class OrderFormActivity extends AppCompatActivity {
@BindView(R.id.fl_order)
FrameLayout mFlOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_form_ativity);
ButterKnife.bind(this);
//构造5个fragment
OrderFormFragment allOrderFragment = OrderFormFragment.creat(0);
OrderFormFragment waitPayOrderFragment = OrderFormFragment.creat(1);
OrderFormFragment waitReiceiveOrderFragment = OrderFormFragment.creat(2);
OrderFormFragment evaluateOrderFragment = OrderFormFragment.creat(3);
OrderFormFragment completeOrderFragment = OrderFormFragment.creat(9);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fl_order,waitPayOrderFragment)
.commit();
}
}
//fragment的代码
package com.example.qrcodesample1.view.fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.qrcodesample1.R;
import com.example.qrcodesample1.bean.OrderFormBean;
import com.example.qrcodesample1.utils.RetrofiManager;
import com.example.qrcodesample1.view.adapter.OrderFormAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
public class OrderFormFragment extends Fragment {
/**
* 根据订单类型创建一个OrderFormFragment对象
*/
public static OrderFormFragment creat(int status) {
OrderFormFragment orderFormFragment = new OrderFormFragment();
Bundle bundle = new Bundle();
bundle.putInt("status", status);
orderFormFragment.setArguments(bundle);
return orderFormFragment;
}
@BindView(R.id.xrv_orderForm)
XRecyclerView mXrvOrderForm;
List<OrderFormBean.OrderListBean> orderListBeans = new ArrayList<>();
private OrderFormAdapter mOrderFormAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View inflate = inflater.inflate(R.layout.frgment_order_form_layout, container, false);
ButterKnife.bind(this, inflate);
return inflate;
}
@SuppressLint("CheckResult")
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//从budle中取出状态值
Bundle bundle = getArguments();
int status = bundle.getInt("status");
mOrderFormAdapter = new OrderFormAdapter(orderListBeans);
//设计订单的一些点击监听
mOrderFormAdapter.setOnOrderFormClickListener(new OrderFormAdapter.onOrderFormClickListener() {
@Override
public void onGoPay(String orderId) {
// TODO: 2019/9/15 调用支付的接口
}
@Override
public void onCancelOrder(String orderId) {
// TODO: 2019/9/15 调用取消订单接口
}
});
mXrvOrderForm.setLayoutManager(new LinearLayoutManager(getContext()));
mXrvOrderForm.setAdapter(mOrderFormAdapter);
//请求头
Map<String, String> headersMap = new HashMap<>();
headersMap.put("userId", "8112");
headersMap.put("sessionId", "15685191578528112");
//get的查询参数
Map<String, Integer> paramsMap = new HashMap<>();
paramsMap.put("status", status);
paramsMap.put("page", 1);
paramsMap.put("count", 10);
RetrofiManager.getInstance().create()
.getOrderFormInfo(headersMap, paramsMap)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<OrderFormBean>() {
@Override
public void accept(OrderFormBean orderFormBean) throws Exception {
if (orderFormBean != null && "0000".equals(orderFormBean.getStatus())) {
Toast.makeText(OrderFormFragment.this.getContext(), "订单获取成功", Toast.LENGTH_SHORT).show();
Log.e("TAG", "accept : " + orderFormBean.toString());
mOrderFormAdapter.setData(orderFormBean.getOrderList());
} else {
Toast.makeText(OrderFormFragment.this.getContext(), "订单获取失败", Toast.LENGTH_SHORT).show();
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("TAG", "error : " + throwable.toString());
}
});
}
}
//对订单具体的操作要放在adapter中
package com.example.qrcodesample1.view.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.qrcodesample1.R;
import com.example.qrcodesample1.bean.OrderFormBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class OrderFormAdapter extends XRecyclerView.Adapter<OrderFormAdapter.OrderViewHolder> {
//待支付
private static final int WAIT_PARY_TYPE = 1;
//待收货
private static final int WAIT_RECEIVE_TYPE = 2;
//待评价
private static final int WAIT_EVALUATE_TYPE = 3;
//已完成
private static final int COMPLETE_TYPE = 9;
//所有的订单
List<OrderFormBean.OrderListBean> mOrderListBeans = new ArrayList<>();
String pat1 = "yyyy-MM-dd HH:mm";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pat1);
public OrderFormAdapter(List<OrderFormBean.OrderListBean> orderListBeans) {
this.mOrderListBeans = orderListBeans;
}
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_order_form_complete_layout, viewGroup, false);
OrderViewHolder viewHolder = new OrderViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull OrderViewHolder completeViewHolder, int i) {
//拿到当前订单的bean类
final OrderFormBean.OrderListBean orderListBean = mOrderListBeans.get(i);
//格式化时间
String time = simpleDateFormat.format(orderListBean.getOrderTime());
//拿到订单中所有的商品
List<OrderFormBean.OrderListBean.DetailListBean> detailList = orderListBean.getDetailList();
//计算总数量和总价格
double totalPrice = 0;
int totalCount = 0;
for (int j = 0; j < detailList.size(); j++) {
OrderFormBean.OrderListBean.DetailListBean detailListBean = detailList.get(j);
totalCount += detailListBean.getCommodityCount();
totalPrice += detailListBean.getCommodityPrice() * detailListBean.getCommodityCount();
}
String format = "共%s件商品,需付款%s元";
String format1 = String.format(format, "" + totalCount, "" + totalPrice);
//订单号
completeViewHolder.mTvOrderId.setText("订单号 " + orderListBean.getOrderId());
//上方的时间
completeViewHolder.mTvOriderTimeTop.setText(time);
//下方的时间
completeViewHolder.mTvOriderTimeBottop.setText(time);
//待支付中的数量和价格
completeViewHolder.mTvTotalPrice.setText(format1);
//带收货中的快递公司
completeViewHolder.mTvExpressCompany.setText(orderListBean.getExpressCompName());
//带收货中的快递号
completeViewHolder.mTvExpressId.setText(orderListBean.getExpressSn());
//去支付
completeViewHolder.mBtnGoPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onOrderFormClickListener != null) {
onOrderFormClickListener.onGoPay(orderListBean.getOrderId());
}
}
});
//取消订单
completeViewHolder.mBtnCancelOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onOrderFormClickListener != null) {
onOrderFormClickListener.onCancelOrder(orderListBean.getOrderId());
}
}
});
//拿到当前的订单
switch (orderListBean.getOrderStatus()) {
case WAIT_PARY_TYPE:
//待支付的控件需要显示
completeViewHolder.mTvOrderId.setVisibility(View.VISIBLE);
completeViewHolder.mTvOriderTimeTop.setVisibility(View.VISIBLE);
completeViewHolder.mTvTotalPrice.setVisibility(View.VISIBLE);
completeViewHolder.mBtnCancelOrder.setVisibility(View.VISIBLE);
completeViewHolder.mBtnGoPay.setVisibility(View.VISIBLE);
//待收货中的快递公司和快递号需要隐藏
completeViewHolder.mTvExpressCompany.setVisibility(View.GONE);
completeViewHolder.mTvExpressId.setVisibility(View.GONE);
completeViewHolder.mTvConfirmReceive.setVisibility(View.GONE);
//待评价的时间和删除按钮需要隐藏
completeViewHolder.mTvOriderTimeBottop.setVisibility(View.GONE);
completeViewHolder.mBtnDeleteOrder.setVisibility(View.GONE);
break;
//当订单状态是待收货的时候
case WAIT_RECEIVE_TYPE:
completeViewHolder.mTvOrderId.setVisibility(View.VISIBLE);
completeViewHolder.mTvOriderTimeTop.setVisibility(View.VISIBLE);
completeViewHolder.mTvExpressCompany.setVisibility(View.VISIBLE);
completeViewHolder.mTvExpressId.setVisibility(View.VISIBLE);
completeViewHolder.mTvConfirmReceive.setVisibility(View.VISIBLE);
//关于待支付的控件需要隐藏
completeViewHolder.mTvTotalPrice.setVisibility(View.GONE);
completeViewHolder.mBtnCancelOrder.setVisibility(View.GONE);
completeViewHolder.mBtnGoPay.setVisibility(View.GONE);
//待评价的时间和删除按钮需要隐藏
completeViewHolder.mBtnDeleteOrder.setVisibility(View.GONE);
completeViewHolder.mTvOriderTimeBottop.setVisibility(View.GONE);
break;
case WAIT_EVALUATE_TYPE:
case COMPLETE_TYPE:
//关于待评价和已完成的删除按钮和下方时间需要显示
completeViewHolder.mBtnDeleteOrder.setVisibility(View.VISIBLE);
completeViewHolder.mTvOriderTimeBottop.setVisibility(View.VISIBLE);
//关于待支付的控件需要隐藏
completeViewHolder.mTvTotalPrice.setVisibility(View.GONE);
completeViewHolder.mBtnCancelOrder.setVisibility(View.GONE);
completeViewHolder.mBtnGoPay.setVisibility(View.GONE);
//关于待收货的控件需要隐藏
completeViewHolder.mTvExpressCompany.setVisibility(View.GONE);
completeViewHolder.mTvExpressId.setVisibility(View.GONE);
completeViewHolder.mTvConfirmReceive.setVisibility(View.GONE);
break;
}
completeViewHolder.mRvOrderProduct.setLayoutManager(new LinearLayoutManager(completeViewHolder.itemView.getContext()));
//设置商品的recyclerview
completeViewHolder.orderFormChildAdapter = new OrderFormChildAdapter(orderListBean.getDetailList());
completeViewHolder.mRvOrderProduct.setAdapter(completeViewHolder.orderFormChildAdapter);
}
@Override
public int getItemCount() {
return mOrderListBeans == null ? 0 : mOrderListBeans.size();
}
public void setData(List<OrderFormBean.OrderListBean> orderList) {
mOrderListBeans.clear();
mOrderListBeans.addAll(orderList);
notifyDataSetChanged();
}
static class OrderViewHolder extends XRecyclerView.ViewHolder {
@BindView(R.id.tv_order_id)
TextView mTvOrderId;
@BindView(R.id.tv_orider_time_top)
TextView mTvOriderTimeTop;
@BindView(R.id.btn_delete_order)
TextView mBtnDeleteOrder;
@BindView(R.id.rv_order_product)
RecyclerView mRvOrderProduct;
@BindView(R.id.tv_total_price)
TextView mTvTotalPrice;
@BindView(R.id.tv_orider_time_bottop)
TextView mTvOriderTimeBottop;
@BindView(R.id.btn_cancel_order)
Button mBtnCancelOrder;
@BindView(R.id.btn_go_pay)
Button mBtnGoPay;
@BindView(R.id.tv_express_company)
TextView mTvExpressCompany;
@BindView(R.id.tv_express_id)
TextView mTvExpressId;
@BindView(R.id.tv_confirm_receive)
Button mTvConfirmReceive;
OrderFormChildAdapter orderFormChildAdapter;
OrderViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
onOrderFormClickListener onOrderFormClickListener;
public void setOnOrderFormClickListener(OrderFormAdapter.onOrderFormClickListener onOrderFormClickListener) {
this.onOrderFormClickListener = onOrderFormClickListener;
}
public interface onOrderFormClickListener{
void onGoPay(String orderId);
void onCancelOrder(String orderId);
}
}
//通过RecyclerView嵌套RecyclerView的方式来实现效果
//子适配器
package com.example.qrcodesample1.view.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.qrcodesample1.R;
import com.example.qrcodesample1.bean.OrderFormBean;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class OrderFormChildAdapter extends RecyclerView.Adapter<OrderFormChildAdapter.FormChildViewHolder> {
List<OrderFormBean.OrderListBean.DetailListBean> mDetailListBeans = new ArrayList<>();
public OrderFormChildAdapter(List<OrderFormBean.OrderListBean.DetailListBean> detailListBeans) {
this.mDetailListBeans = detailListBeans;
}
@NonNull
@Override
public FormChildViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_order_child_complete_layout, viewGroup, false);
FormChildViewHolder viewHolder = new FormChildViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull FormChildViewHolder formChildViewHolder, int i) {
OrderFormBean.OrderListBean.DetailListBean detailListBean = mDetailListBeans.get(i);
formChildViewHolder.mIvOrderChildIcon.setImageResource(R.mipmap.ic_launcher_round);
formChildViewHolder.mTvOrderChildPrice.setText("" +detailListBean.getCommodityPrice());
formChildViewHolder.mTvOrderChildTitle.setText("" +detailListBean.getCommodityName());
}
@Override
public int getItemCount() {
return mDetailListBeans == null ? 0 : mDetailListBeans.size();
}
class FormChildViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.iv_order_child_icon)
ImageView mIvOrderChildIcon;
@BindView(R.id.tv_order_child_title)
TextView mTvOrderChildTitle;
@BindView(R.id.tv_order_child_price)
TextView mTvOrderChildPrice;
public FormChildViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
标签:tail ++ app vco imageview resource serve gone hashmap
原文地址:https://www.cnblogs.com/fybb/p/11523458.html