先添加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
添加需要用到的第三方的依赖:
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
//布局文件
主布局里面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bwie.com.a1509aweek3.view.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="订单列表"
android:textSize="25sp" />
<ImageView
android:id="@+id/iv_pop"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/lv_icon" />
</RelativeLayout>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="待支付" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="已支付" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="已取消" />
</RadioGroup>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
子布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:layout_weight="1"
android:textColor="#ff0000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
//又侧面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="待支付"></TextView>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="已支付"></TextView>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="已取消"></TextView>
</LinearLayout>
第一步 开始上代码:
net包:
package bwie.com.a1509aweek3.net;
/**
* Created by peng on 2017/11/7.
*/
public interface Api {
public static final String ORDER = "https://www.zhaoapi.cn/product/getOrders";
public static final String CANCLE_ORDER = "https://www.zhaoapi.cn/product/updateOrder";
}
package bwie.com.a1509aweek3.net;
import java.util.Map;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
* Created by peng on 2017/11/7.
* 网络请求工具类
*/
public class HttpUtils {
private static HttpUtils httpUtils;
private final OkHttpClient client;
private HttpUtils() {
//创建OkHttpClient
//创建拦截器
client = new OkHttpClient.Builder()
.addInterceptor(new MyInterceptor())
.build();
}
public static HttpUtils getHttpUtils() {
if (httpUtils == null) {
synchronized (HttpUtils.class) {
if (httpUtils == null) {
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
/**
* GET请求
*
* @param url 请求地址
* @param callback 回调
*/
public void doGet(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
/**
* POST请求
*
* @param url 请求地址
* @param params 请求的参数
* @param callback 回调
*/
public void doPost(String url, Map<String, String> params, Callback callback) {
//这里可以加网络判断
//判断参数
if (params == null || params.size() == 0) {
throw new RuntimeException("params is null!!!");
}
// 创建Request
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
client.newCall(request).enqueue(callback);
}
}
package bwie.com.a1509aweek3.net;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by peng on 2017/11/20.
*/
public class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//获取原始的请求数据
Request orginRequest = chain.request();
//先获取原始的请求参数,然后再拼接起来
FormBody orginBody = (FormBody) orginRequest.body();
FormBody.Builder builder = new FormBody.Builder();
for (int i = 0; i < orginBody.size(); i++) {
String name = orginBody.name(i);
String value = orginBody.value(i);
builder.add(name, value);
}
builder.add("source", "android");
FormBody formBody = builder.build();
Request request = new Request.Builder().url(orginRequest.url()).post(formBody).build();
return chain.proceed(request);
}
}
package bwie.com.a1509aweek3.net;
/**
* Created by peng on 2017/11/7.
* 网络请求回调接口
*/
public interface OnNetListener<T> {
//成功回调
public void onSuccess(T t);
//失败回调
public void onFailure(Exception e);
}
第二步:创建Model层:
package bwie.com.a1509aweek3.model;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import bwie.com.a1509aweek3.net.Api;
import bwie.com.a1509aweek3.net.HttpUtils;
import bwie.com.a1509aweek3.net.OnNetListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/11/21.
*/
public class CancleOrderModel {
public void cancleOrder(String status, String orderId, OnNetListener onNetListener) {
Map<String, String> params = new HashMap<>();
params.put("uid", "71");
params.put("orderId", orderId);
params.put("status", status);
HttpUtils.getHttpUtils().doPost(Api.CANCLE_ORDER, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
}
package bwie.com.a1509aweek3.model;
import bwie.com.a1509aweek3.bean.OrderBean;
import bwie.com.a1509aweek3.net.OnNetListener;
/**
* Created by peng on 2017/11/21.
*/
public interface IOrderModel {
public void getOrder(String status, String page, OnNetListener<OrderBean> onNetListener);
}
package bwie.com.a1509aweek3.model;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import bwie.com.a1509aweek3.bean.OrderBean;
import bwie.com.a1509aweek3.net.Api;
import bwie.com.a1509aweek3.net.HttpUtils;
import bwie.com.a1509aweek3.net.OnNetListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/11/21.
*/
public class OrderModel implements IOrderModel {
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void getOrder(final String status, String page, final OnNetListener<OrderBean> onNetListener) {
Map<String, String> params = new HashMap<>();
params.put("uid", "71");
params.put("status", status);
params.put("page", page);
HttpUtils.getHttpUtils().doPost(Api.ORDER, params, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final OrderBean orderBean = new Gson().fromJson(string, OrderBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(orderBean);
}
});
}
});
}
}
3.创建Presenter层:
package bwie.com.a1509aweek3.presenter;
import bwie.com.a1509aweek3.model.CancleOrderModel;
import bwie.com.a1509aweek3.net.OnNetListener;
/**
* Created by peng on 2017/11/21.
*/
public class CancleOrderPresenter {
private CancleOrderModel cancleOrderModel;
public CancleOrderPresenter() {
this.cancleOrderModel = new CancleOrderModel();
}
public void cancleOrder(String status, String orderId) {
cancleOrderModel.cancleOrder(status, orderId, new OnNetListener() {
@Override
public void onSuccess(Object o) {
}
@Override
public void onFailure(Exception e) {
}
});
}
}
package bwie.com.a1509aweek3.presenter;
import bwie.com.a1509aweek3.bean.OrderBean;
import bwie.com.a1509aweek3.model.OrderModel;
import bwie.com.a1509aweek3.net.OnNetListener;
import bwie.com.a1509aweek3.view.IMainActivity;
/**
* Created by peng on 2017/11/21.
*/
public class MainPresenter {
private OrderModel orderModel;
private IMainActivity iMainActivity;
public MainPresenter(IMainActivity iMainActivity) {
orderModel = new OrderModel();
this.iMainActivity = iMainActivity;
}
public void getOrder(final boolean isRefresh, String status, String page) {
orderModel.getOrder(status, page, new OnNetListener<OrderBean>() {
@Override
public void onSuccess(OrderBean orderBean) {
iMainActivity.showData(isRefresh, orderBean.getData());
}
@Override
public void onFailure(Exception e) {
}
});
}
public void dettach() {
if (iMainActivity != null) {
iMainActivity = null;
}
}
}
//根据需求创建对应的实体类(bean)
View层:
package bwie.com.a1509aweek3.view;
import java.util.List;
import bwie.com.a1509aweek3.bean.OrderBean;
/**
* Created by peng on 2017/11/21.
*/
public interface IMainActivity {
public void showData(boolean isRefresh, List<OrderBean.DataBean> list);
}
package bwie.com.a1509aweek3.view;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
import bwie.com.a1509aweek3.R;
import bwie.com.a1509aweek3.adapter.MyAdapter;
import bwie.com.a1509aweek3.bean.OrderBean;
import bwie.com.a1509aweek3.presenter.MainPresenter;
public class MainActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener {
/**
* 待支付
*/
private RadioButton mRb1;
/**
* 已支付
*/
private RadioButton mRb2;
/**
* 已取消
*/
private RadioButton mRb3;
private RadioGroup mRg;
private XRecyclerView mXrv;
private MainPresenter mainPresenter;
private int page = 1;
private List<OrderBean.DataBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private ImageView mIvPop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mXrv.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyAdapter(this, list);
mXrv.setAdapter(myAdapter);
//想去获取数据,测试一下数据是否获取到
mainPresenter = new MainPresenter(this);
mainPresenter.getOrder(true, "1", page + "");
mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
//刷新
page = 1;
mainPresenter.getOrder(true, "1", page + "");
}
@Override
public void onLoadMore() {
//加载更多
page++;
mainPresenter.getOrder(false, "1", page + "");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mainPresenter.dettach();
}
private void initView() {
mRb1 = (RadioButton) findViewById(R.id.rb1);
mRb2 = (RadioButton) findViewById(R.id.rb2);
mRb3 = (RadioButton) findViewById(R.id.rb3);
mRg = (RadioGroup) findViewById(R.id.rg);
mXrv = (XRecyclerView) findViewById(R.id.xrv);
mIvPop = (ImageView) findViewById(R.id.iv_pop);
mIvPop.setOnClickListener(this);
}
@Override
public void showData(boolean isRefresh, List<OrderBean.DataBean> newList) {
if (isRefresh) {
//如果是刷新
list.clear();
list.addAll(newList);
mXrv.refreshComplete();
} else {
//如果是加载更多
list.addAll(newList);
// mXrv.loadMoreComplete();
// if (list.size() == 总条目数) {
mXrv.setLoadingMoreEnabled(false);
// }
}
myAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.iv_pop:
//弹出popupwindow
View view = View.inflate(MainActivity.this, R.layout.pop_item, null);
TextView tv1 = view.findViewById(R.id.tv1);
TextView tv2 = view.findViewById(R.id.tv2);
TextView tv3 = view.findViewById(R.id.tv3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
PopupWindow popupWindow = new PopupWindow(view, layoutParams.width, layoutParams.height);
popupWindow.showAsDropDown(mIvPop, 0, 30);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "tv1", Toast.LENGTH_SHORT).show();
mainPresenter.getOrder(true, "1", "1");
}
});
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "tv2", Toast.LENGTH_SHORT).show();
}
});
tv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "tv3", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}
适配器:
package bwie.com.a1509aweek3.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import bwie.com.a1509aweek3.R;
import bwie.com.a1509aweek3.bean.OrderBean;
import bwie.com.a1509aweek3.presenter.CancleOrderPresenter;
/**
* Created by peng on 2017/11/21.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<OrderBean.DataBean> list;
private final CancleOrderPresenter cancleOrderPresenter;
public MyAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
cancleOrderPresenter = new CancleOrderPresenter();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final OrderBean.DataBean dataBean = list.get(position);
MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.tv_title.setText(dataBean.getTitle());
int status = dataBean.getStatus();
if (status == 0) {
myViewHolder.tv_status.setText("待支付");
myViewHolder.tv.setText("取消订单");
myViewHolder.tv_status.setTextColor(Color.RED);
} else if (status == 1) {
myViewHolder.tv_status.setText("已支付");
myViewHolder.tv.setText("查看订单");
myViewHolder.tv_status.setTextColor(Color.BLACK);
} else {
myViewHolder.tv_status.setText("已取消");
myViewHolder.tv.setText("查看订单");
myViewHolder.tv_status.setTextColor(Color.BLACK);
}
myViewHolder.tv_price.setText("价格:" + dataBean.getPrice());
myViewHolder.tv_time.setText("创建时间:" + dataBean.getCreatetime());
myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(context)
.setTitle("提示")
.setMessage("确定取消订单吗?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancleOrderPresenter.cancleOrder("1", dataBean.getOrderid() + "");
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView tv_title;
private final TextView tv_status;
private final TextView tv_price;
private final TextView tv_time;
private final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.tv_title);
tv_status = itemView.findViewById(R.id.tv_status);
tv_price = itemView.findViewById(R.id.tv_price);
tv_time = itemView.findViewById(R.id.tv_time);
tv = itemView.findViewById(R.id.tv);
}
}
}