activity_ding_dan
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="25sp"
android:text="订单详情"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black"/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_wait"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="15sp"
android:layout_height="match_parent"
android:text="待支付"/>
<RadioButton
android:id="@+id/rb_payed"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="15sp"
android:layout_height="match_parent"
android:text="已支付"/>
<RadioButton
android:id="@+id/rb_cancel"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="15sp"
android:layout_height="match_parent"
android:text="已取消"/>
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
DingDanActivity
public class DingDanActivity extends AppCompatActivity {
private RadioGroup rg;
private RadioButton rbAll;
private RadioButton rbWait;
private RadioButton rbPayed;
private RadioButton rbCancel;
private ViewPager viewpager;
private List<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ding_dan);
initView();
MyApapter apapter=new MyApapter(getSupportFragmentManager());
viewpager.setAdapter(apapter);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb_wait:
viewpager.setCurrentItem(0);
break;
case R.id.rb_payed:
viewpager.setCurrentItem(1);
break;
case R.id.rb_cancel:
viewpager.setCurrentItem(2);
break;
}
}
});
}
private void initView() {
rg = (RadioGroup) findViewById(R.id.rg);
rbWait = (RadioButton) findViewById(R.id.rb_wait);
rbPayed = (RadioButton) findViewById(R.id.rb_payed);
rbCancel = (RadioButton) findViewById(R.id.rb_cancel);
viewpager = (ViewPager) findViewById(R.id.viewpager);
list = new ArrayList<>();
list.add(new WaitPayFragment());
list.add(new PayedFragment());
list.add(new CanceldFragment());
}
class MyApapter extends FragmentPagerAdapter {
public MyApapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
CanceldFragment
public class CanceldFragment extends Fragment implements OrderView {
private OrderPresenter presenter;
private List<OrderBean.DataBean> list = new ArrayList<>();
private OrderAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_canceld, container, false);
RecyclerView recy_cancel=view.findViewById(R.id.recyclerView);
presenter = new OrderPresenter(this);
presenter.getOrder("1");
adapter = new OrderAdapter(getContext(),list);
recy_cancel.setAdapter(adapter);
recy_cancel.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
return view;
}
@Override
public void success(OrderBean bean) {
list.clear();
list.addAll(bean.getData());
adapter.notifyDataSetChanged();
}
@Override
public void failure(int code) {
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.dettach();
}
@Override
public void onResume() {
super.onResume();
presenter.getOrder("1");
}
}
OrderView
public interface OrderView {
void success(OrderBean bean);
void failure(int code);
}
OrderModel
public class OrderModel {
//http://120.27.23.105/product/getOrders?uid=71
public void getOrderData(String status,final OrderModelCallback callback){
Map<String,String> map=new HashMap<>();
map.put("uid","100");
map.put("status",status);
RetrofitManager.get("product/getOrders", map, new BaseObserver<OrderBean>() {
@Override
public void success(OrderBean bean) {
callback.success(bean);
}
@Override
public void failure(int code) {
callback.failure(code);
}
@Override
public void onNext(LoginBean bean) {
}
});
}
}
OrderModelCallback
public interface OrderModelCallback {
void success(OrderBean bean);
void failure(int code);
}
OrderPresenter
public class OrderPresenter {
private OrderView view;
private OrderModel model;
public OrderPresenter(OrderView view) {
this.view = view;
model=new OrderModel();
}
public void getOrder(String status){
model.getOrderData(status, new OrderModelCallback() {
@Override
public void success(OrderBean bean) {
view.success(bean);
}
@Override
public void failure(int code) {
view.failure(code);
}
});
}
/**
* 解除绑定
*/
public void dettach(){
this.view=null;
}
}
OrderAdapter
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder>implements UpdateOrderView {
private Context context;
private List<OrderBean.DataBean> list;
private UpdateOrderPresenter presenter=new UpdateOrderPresenter(this);
public OrderAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.order_item, null);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
OrderBean.DataBean dataBean = list.get(position);
holder.orderTime.setText("创建时间:"+dataBean.getCreatetime());
holder.orderTitle.setText(dataBean.getTitle()+"");
holder.orderPrice.setText("优惠价:¥"+dataBean.getPrice()+"");
holder.orderPrice.setTextColor(Color.RED);
final int status = dataBean.getStatus();
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
if(status==0){
holder.orderStatus.setTextColor(Color.parseColor("#ff0000"));
holder.orderStatus.setText("待支付");
holder.orderBtn.setText("取消订单");
}else if(status==1){
holder.orderStatus.setText("已取消");
holder.orderBtn.setText("查看订单");
}else if(status==2){
holder.orderStatus.setText("已支付");
holder.orderBtn.setText("查看订单");
}
holder.orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status==0){
AlertDialog.Builder dialog=new AlertDialog.Builder(context);
dialog.setTitle("提示")
.setMessage("确定取消订单吗?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int orderid = list.get(position).getOrderid();
presenter.updateOrder("1",orderid+"");
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
holder.orderStatus.setText("已取消");
// notifyDataSetChanged();
}
}).create().show();
}else{
Toast.makeText(context, "查看订单", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public void success(UpdateOrderBean bean) {
Toast.makeText(context,bean.getMsg(),Toast.LENGTH_SHORT).show();
}
@Override
public void failure(int code) {
}
class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView orderTitle;
private final TextView orderPrice;
private final TextView orderTime;
private final TextView orderStatus;
private final TextView orderBtn;
public MyViewHolder(View itemView) {
super(itemView);
orderTitle = itemView.findViewById(R.id.order_title);
orderPrice = itemView.findViewById(R.id.order_price);
orderTime = itemView.findViewById(R.id.order_time);
orderStatus = itemView.findViewById(R.id.order_status);
orderBtn = itemView.findViewById(R.id.order_btn);
}
}
}
fragment_canceld
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
order_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/order_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="21dp"
android:text="TextView" />
<TextView
android:id="@+id/order_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/order_title"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="31dp"
android:layout_marginRight="31dp"
android:text="TextView" />
<TextView
android:id="@+id/order_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/order_title"
android:layout_alignStart="@+id/order_title"
android:layout_below="@+id/order_title"
android:layout_marginTop="27dp"
android:text="TextView" />
<TextView
android:id="@+id/order_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/order_price"
android:layout_marginTop="24dp"
android:text="TextView" />
<TextView
android:id="@+id/order_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/order_time"
android:layout_alignEnd="@+id/order_status"
android:layout_alignRight="@+id/order_status"
android:text="TextView" />
</RelativeLayout>
UpdateOrderView
public interface UpdateOrderView {
void success(UpdateOrderBean bean);
void failure(int code);
}
UpdateOrderPresenter
public class UpdateOrderPresenter {
private UpdateOrderView view;
private UpdateOrderModel model;
public UpdateOrderPresenter(UpdateOrderView view) {
this.view = view;
model=new UpdateOrderModel();
}
public void updateOrder(String status,String orderId){
model.updateOrder(status, orderId, new UpdateOrderCallback() {
@Override
public void success(UpdateOrderBean bean) {
view.success(bean);
}
@Override
public void failure(int code) {
view.failure(code);
}
});
}
}
UpdateOrderModel
public class UpdateOrderModel {
//https://www.zhaoapi.cn/product/updateOrder?uid=100&status=2&orderId=5037
public void updateOrder(String status, String orderId,final UpdateOrderCallback callback) {
Map<String, String> map = new HashMap<>();
map.put("uid","100");
map.put("status",status);
map.put("orderId",orderId);
RetrofitManager.get("product/updateOrder", map, new BaseObserver<UpdateOrderBean>() {
@Override
public void success(UpdateOrderBean bean) {
callback.success(bean);
}
@Override
public void failure(int code) {
}
@Override
public void onNext(LoginBean bean) {
}
});
}
}
UpdateOrderCallback
public interface UpdateOrderCallback {
void success(UpdateOrderBean bean);
void failure(int code);
}
PayedFragment
public class PayedFragment extends Fragment implements OrderView {
private OrderPresenter presenter;
private List<OrderBean.DataBean> list = new ArrayList<>();
private OrderAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_payed, container, false);
RecyclerView recy_cancel=view.findViewById(R.id.recyclerView);
presenter = new OrderPresenter(this);
presenter.getOrder("2");
adapter = new OrderAdapter(getContext(),list);
recy_cancel.setAdapter(adapter);
recy_cancel.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
return view;
}
@Override
public void success(OrderBean bean) {
// Toast.makeText(getContext(),bea)
list.clear();
list.addAll(bean.getData());
adapter.notifyDataSetChanged();
}
@Override
public void failure(int code) {
}
}
OrderView
public interface OrderView {
void success(OrderBean bean);
void failure(int code);
}
OrderPresenter
public class OrderPresenter {
private OrderView view;
private OrderModel model;
public OrderPresenter(OrderView view) {
this.view = view;
model=new OrderModel();
}
public void getOrder(String status){
model.getOrderData(status, new OrderModelCallback() {
@Override
public void success(OrderBean bean) {
view.success(bean);
}
@Override
public void failure(int code) {
view.failure(code);
}
});
}
/**
* 解除绑定
*/
public void dettach(){
this.view=null;
}
}
OrderModel
public class OrderModel {
//http://120.27.23.105/product/getOrders?uid=71
public void getOrderData(String status,final OrderModelCallback callback){
Map<String,String> map=new HashMap<>();
map.put("uid","100");
map.put("status",status);
RetrofitManager.get("product/getOrders", map, new BaseObserver<OrderBean>() {
@Override
public void success(OrderBean bean) {
callback.success(bean);
}
@Override
public void failure(int code) {
callback.failure(code);
}
@Override
public void onNext(LoginBean bean) {
}
});
}
}
OrderModelCallback
public interface OrderModelCallback {
void success(OrderBean bean);
void failure(int code);
}
OrderAdapter
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder>implements UpdateOrderView {
private Context context;
private List<OrderBean.DataBean> list;
private UpdateOrderPresenter presenter=new UpdateOrderPresenter(this);
public OrderAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.order_item, null);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
OrderBean.DataBean dataBean = list.get(position);
holder.orderTime.setText("创建时间:"+dataBean.getCreatetime());
holder.orderTitle.setText(dataBean.getTitle()+"");
holder.orderPrice.setText("优惠价:¥"+dataBean.getPrice()+"");
holder.orderPrice.setTextColor(Color.RED);
final int status = dataBean.getStatus();
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
if(status==0){
holder.orderStatus.setTextColor(Color.parseColor("#ff0000"));
holder.orderStatus.setText("待支付");
holder.orderBtn.setText("取消订单");
}else if(status==1){
holder.orderStatus.setText("已取消");
holder.orderBtn.setText("查看订单");
}else if(status==2){
holder.orderStatus.setText("已支付");
holder.orderBtn.setText("查看订单");
}
holder.orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status==0){
AlertDialog.Builder dialog=new AlertDialog.Builder(context);
dialog.setTitle("提示")
.setMessage("确定取消订单吗?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int orderid = list.get(position).getOrderid();
presenter.updateOrder("1",orderid+"");
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
holder.orderStatus.setText("已取消");
// notifyDataSetChanged();
}
}).create().show();
}else{
Toast.makeText(context, "查看订单", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public void success(UpdateOrderBean bean) {
Toast.makeText(context,bean.getMsg(),Toast.LENGTH_SHORT).show();
}
@Override
public void failure(int code) {
}
class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView orderTitle;
private final TextView orderPrice;
private final TextView orderTime;
private final TextView orderStatus;
private final TextView orderBtn;
public MyViewHolder(View itemView) {
super(itemView);
orderTitle = itemView.findViewById(R.id.order_title);
orderPrice = itemView.findViewById(R.id.order_price);
orderTime = itemView.findViewById(R.id.order_time);
orderStatus = itemView.findViewById(R.id.order_status);
orderBtn = itemView.findViewById(R.id.order_btn);
}
}
}
WaitPayFragment
public class WaitPayFragment extends Fragment implements OrderView {
private OrderPresenter presenter;
private List<OrderBean.DataBean> list = new ArrayList<>();
private OrderAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_wait_pay, container, false);
RecyclerView recy_cancel=view.findViewById(R.id.recyclerView);
presenter = new OrderPresenter(this);
presenter.getOrder("0");
adapter = new OrderAdapter(getContext(),list);
recy_cancel.setAdapter(adapter);
recy_cancel.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
return view;
}
@Override
public void success(OrderBean bean) {
list.clear();
list.addAll(bean.getData());
adapter.notifyDataSetChanged();
}
@Override
public void failure(int code) {
}
@Override
public void onDestroy() {
super.onDestroy();
//presenter.dettach();
}
}
OrderAdapter
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder>implements UpdateOrderView {
private Context context;
private List<OrderBean.DataBean> list;
private UpdateOrderPresenter presenter=new UpdateOrderPresenter(this);
public OrderAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.order_item, null);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
OrderBean.DataBean dataBean = list.get(position);
holder.orderTime.setText("创建时间:"+dataBean.getCreatetime());
holder.orderTitle.setText(dataBean.getTitle()+"");
holder.orderPrice.setText("优惠价:¥"+dataBean.getPrice()+"");
holder.orderPrice.setTextColor(Color.RED);
final int status = dataBean.getStatus();
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
if(status==0){
holder.orderStatus.setTextColor(Color.parseColor("#ff0000"));
holder.orderStatus.setText("待支付");
holder.orderBtn.setText("取消订单");
}else if(status==1){
holder.orderStatus.setText("已取消");
holder.orderBtn.setText("查看订单");
}else if(status==2){
holder.orderStatus.setText("已支付");
holder.orderBtn.setText("查看订单");
}
holder.orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status==0){
AlertDialog.Builder dialog=new AlertDialog.Builder(context);
dialog.setTitle("提示")
.setMessage("确定取消订单吗?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int orderid = list.get(position).getOrderid();
presenter.updateOrder("1",orderid+"");
holder.orderStatus.setTextColor(Color.parseColor("#000000"));
holder.orderStatus.setText("已取消");
// notifyDataSetChanged();
}
}).create().show();
}else{
Toast.makeText(context, "查看订单", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public void success(UpdateOrderBean bean) {
Toast.makeText(context,bean.getMsg(),Toast.LENGTH_SHORT).show();
}
@Override
public void failure(int code) {
}
class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView orderTitle;
private final TextView orderPrice;
private final TextView orderTime;
private final TextView orderStatus;
private final TextView orderBtn;
public MyViewHolder(View itemView) {
super(itemView);
orderTitle = itemView.findViewById(R.id.order_title);
orderPrice = itemView.findViewById(R.id.order_price);
orderTime = itemView.findViewById(R.id.order_time);
orderStatus = itemView.findViewById(R.id.order_status);
orderBtn = itemView.findViewById(R.id.order_btn);
}
}
}