//清单文件
<uses-permission android:name="android.permission.INTERNET" />//依赖
compile 'com.squareup.okhttp3:okhttp:3.3.0' compile 'com.github.bumptech.glide:glide:3.6.1' compile 'com.google.code.gson:gson:2.6.2' compile 'com.jcodecraeer:xrecyclerview:1.5.8' ------------------------------------------------------------------
Model层
public interface IModel { //请求数据 void showData(String url, Mapmap, ModelShowDataListener modelShowDataListener); //刷新 void doRefresh(String url, Map params,ModelShowDataListener modelShowDataListener); //上拉加载 void doLoadMore(String url, Map params,ModelShowDataListener modelShowDataListener); //取消订单 void deleteGoods(String url, Map params,ModelShowDataListener modelShowDataListener); } ------------------------------------------
public interface ModelShowDataListener { void getDataSuccess(String json); void getDataError(String error); }-------------------------------------------
public class ModelImpl implements IModel { //请求数据 @Override public void showData(String url, Map-----------------------------------------------------------------------------------------------------map, final ModelShowDataListener modelShowDataListener) { OkHttpUtils instance = OkHttpUtils.getInstance(); instance.okPost(url,map); instance.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { modelShowDataListener.getDataSuccess(json); } @Override public void okLoadError(String error) { modelShowDataListener.getDataError(error); } }); } //刷新 @Override public void doRefresh(String url, Map map, final ModelShowDataListener modelShowDataListener) { OkHttpUtils instance = OkHttpUtils.getInstance(); instance.okPost(url,map); instance.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { modelShowDataListener.getDataSuccess(json); } @Override public void okLoadError(String error) { modelShowDataListener.getDataError(error); } }); } //上拉加载 @Override public void doLoadMore(String url, Map map, final ModelShowDataListener modelShowDataListener) { OkHttpUtils instance = OkHttpUtils.getInstance(); instance.okPost(url,map); instance.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { modelShowDataListener.getDataSuccess(json); } @Override public void okLoadError(String error) { modelShowDataListener.getDataError(error); } }); } //取消订单 @Override public void deleteGoods(String url, Map map, final ModelShowDataListener modelShowDataListener) { OkHttpUtils instance = OkHttpUtils.getInstance(); instance.okPost(url,map); instance.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { modelShowDataListener.getDataSuccess(json); } @Override public void okLoadError(String error) { modelShowDataListener.getDataError(error); } }); } }
Presenter层 public interface IPresenter { //展示数据 void showData(IModel iModel, IView iView); //刷新 void doRefresh(IModel iModel, IView iView); //上拉加载 void doLoadMore(IModel iModel, IView iView); //取消订单 void deleteGoods(IModel iModel, IView iView,String str); } -------------------------------------------------------
public class PresenterImpl implements IPresenter { @Override public void showData(IModel iModel, final IView iView) { Map--------------------------------------------------------------------------------map=new HashMap (); map.put("uid","71"); iModel.showData(HttpConfig.getOrders_url, map, new ModelShowDataListener() { @Override public void getDataSuccess(String json) { Gson gson = new Gson(); GoodsBean bean = gson.fromJson(json, GoodsBean.class); List data = bean.getData(); iView.showData(data); } private static final String TAG = "PresenterImpl"; @Override public void getDataError(String error) { Log.d(TAG, "获取数据失败 "); } }); } @Override public void doRefresh(IModel iModel, final IView iView) { Map map=new HashMap (); map.put("uid","71"); map.put("page","1"); iModel.doRefresh(HttpConfig.getOrders_url, map, new ModelShowDataListener() { @Override public void getDataSuccess(String json) { Gson gson = new Gson(); GoodsBean bean = gson.fromJson(json, GoodsBean.class); List data = bean.getData(); iView.showData(data); } private static final String TAG = "PresenterImpl"; @Override public void getDataError(String error) { Log.d(TAG, "获取数据失败 "); } }); } private static final String TAG = "PresenterImpl"; int i=1; @Override public void doLoadMore(IModel iModel, final IView iView) { i++; Map map=new HashMap (); map.put("uid","71"); map.put("page",""+i); Log.d(TAG, i+""); iModel.doLoadMore(HttpConfig.getOrders_url, map, new ModelShowDataListener() { @Override public void getDataSuccess(String json) { Gson gson = new Gson(); GoodsBean bean = gson.fromJson(json, GoodsBean.class); List data = bean.getData(); iView.showData(data); } private static final String TAG = "PresenterImpl"; @Override public void getDataError(String error) { Log.d(TAG, "获取数据失败 "); } }); } @Override public void deleteGoods(final IModel iModel, final IView iView, String str) { Map map=new HashMap (); map.put("uid","71"); map.put("orderId",str); map.put("status","2"); iModel.deleteGoods(HttpConfig.updateOrder_url, map, new ModelShowDataListener(){ @Override public void getDataSuccess(String json) { try { JSONObject obj=new JSONObject(json); String code = obj.getString("code"); String msg = obj.getString("msg"); if (code .equals("0")) { new PresenterImpl().showData(iModel,iView); }else{ Log.d(TAG, msg); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void getDataError(String error) { Log.d(TAG, error); } }); } }
public class HttpConfig { public static String getOrders_url = "https://www.zhaoapi.cn/product/getOrders"; public static String updateOrder_url = "https://www.zhaoapi.cn/product/updateOrder"; }---------------------------------------
public class OkHttpUtils { private static OkHttpUtils okHttpUtils = null; private MyHandler myHandler = new MyHandler(); private OkLoadListener okLoadListener; //单例 public static OkHttpUtils getInstance() { if (okHttpUtils == null) { okHttpUtils = new OkHttpUtils(); } return okHttpUtils; } //get public void okGet(String url) { OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyInter()).build(); Request request = new Request.Builder().url(url).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message message = myHandler.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } //post public void okPost(String url, Map--------------------------------------------------------------map) { OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyInter()).build(); //创建FormBody FormBody.Builder builder = new FormBody.Builder(); //遍历map Set keys = map.keySet(); for (String key : keys) { String value = map.get(key); builder.add(key, value+""); } //build FormBody body = builder.build(); Request request = new Request.Builder().url(url).post(body).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message message = myHandler.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } //拦截器 class MyInter implements Interceptor { private static final String TAG = "MyInter"; @Override public Response intercept(Chain chain) throws IOException { //获取原来的body Request request = chain.request(); RequestBody body = request.body(); if (body instanceof FormBody) { //遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body FormBody.Builder newBuilder = new FormBody.Builder(); for (int i = 0; i < ((FormBody) body).size(); i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); //放入新的 newBuilder.add(name, value); } //在将公共参数添加进去 newBuilder.add("source", "android"); FormBody newBody = newBuilder.build(); //创建新的请求 Request newRequest = request.newBuilder().post(newBody).build(); Response response = chain.proceed(newRequest); return response; } return null; } } //handler class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: //失败 String e = (String) msg.obj; okLoadListener.okLoadError(e); break; case 1: //成功 String json = (String) msg.obj; okLoadListener.okLoadSuccess(json); break; } } } //提高外部调用的接口 public void setOkLoadListener(OkLoadListener okLoadListener) { this.okLoadListener = okLoadListener; } }
public interface OkLoadListener { //网络请求成功 void okLoadSuccess(String json); //网络请求失败 void okLoadError(String error); }----------------------------------------------------------------------------------------------------
public interface IView { //显示数据 void showData(Listlist); //刷新 void doRefresh(List list); //上拉加载 void doLoadMore(List list); }
------------------------------------------------ //条目分割线 可有可无
public class MyDecoration extends RecyclerView.ItemDecoration { private Context mContext; private Drawable mDivider; private int mOrientation; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; //我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置 public static final int[] ATRRS = new int[]{ android.R.attr.listDivider }; public MyDecoration(Context context, int orientation) { this.mContext = context; final TypedArray ta = context.obtainStyledAttributes(ATRRS); this.mDivider = ta.getDrawable(0); ta.recycle(); setOrientation(orientation); } //设置屏幕的方向 public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if (mOrientation == HORIZONTAL_LIST) { drawVerticalLine(c, parent, state); } else { drawHorizontalLine(c, parent, state); } } //画横线, 这里的parent其实是显示在屏幕显示的这部分 public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); //获得child的布局信息 final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); //Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i); } } //画竖线 public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) { int top = parent.getPaddingTop(); int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); //获得child的布局信息 final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicWidth(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } //由于Divider也有长宽高,每一个Item需要向下或者向右偏移 @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if (mOrientation == HORIZONTAL_LIST) { //画横线,就是往下偏移一个分割线的高度 outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { //画竖线,就是往右偏移一个分割线的宽度 outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } }------------------------------------------------------
public class MainActivity extends AppCompatActivity implements IView,View.OnClickListener{ private ImageView image; private XRecyclerView xRecyler; private String[] item = new String[]{"已支付","待支付","已取消"}; private PresenterImpl presenter; private List---------------------------------------------------------list; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化也没 initViews(); //调用 presenter = new PresenterImpl(); presenter.showData(new ModelImpl(),this); } private void initViews() { image = (ImageView) findViewById(R.id.image); xRecyler = (XRecyclerView) findViewById(R.id.xRecylerView); xRecyler = findViewById(R.id.xRecylerView); //设置 xRecyler.setLayoutManager(new LinearLayoutManager(this)); xRecyler.addItemDecoration(new MyDecoration(this, MyDecoration.VERTICAL_LIST)); //设置 xRecyler.setPullRefreshEnabled(true);//下拉刷新 xRecyler.setLoadingMoreEnabled(true);//上拉加载 xRecyler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//样式 xRecyler.setLoadingMoreProgressStyle(ProgressStyle.Pacman); //监听 xRecyler.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { //调用presenter里面的刷新方法 presenter.doRefresh(new ModelImpl(), MainActivity.this); //让进度条缩回去 xRecyler.refreshComplete(); } @Override public void onLoadMore() { //调用presenter里面的加载更多方法 presenter.doLoadMore(new ModelImpl(), MainActivity.this); //让进度条缩回去 xRecyler.refreshComplete(); } }); //pop image = findViewById(R.id.image); image.setOnClickListener(this); } @Override public void showData(List list) { //设置适配器 this.list=list; MyAdapter myAdapter = new MyAdapter(MainActivity.this, list, this); xRecyler.setAdapter(myAdapter); } @Override public void doRefresh(List list) { //设置适配器 MyAdapter myAdapter = new MyAdapter(MainActivity.this, list, this); xRecyler.setAdapter(myAdapter); } @Override public void doLoadMore(List list) { //设置适配器 MyAdapter myAdapter = new MyAdapter(MainActivity.this, list, this); xRecyler.setAdapter(myAdapter); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.image: View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item,null); //设置view里面的数据 ListView listView = view.findViewById(R.id.listView); listView.setAdapter(new BaseAdapter() { @Override public int getCount() { return item.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(MainActivity.this); textView.setText(item[position]); textView.setTextSize(20); return textView; } }); final List goods=new ArrayList<>(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(MainActivity.this,item[position],Toast.LENGTH_SHORT).show(); if (item[position] == "已支付") { goods.clear(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getStatus() ==2) { goods.add(list.get(i)); MyAdapter myAdapter = new MyAdapter(MainActivity.this, goods, MainActivity.this); xRecyler.setAdapter(myAdapter); } } popupWindow.dismiss(); }else if (item[position] == "待支付"){ goods.clear(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getStatus() ==0) { goods.add(list.get(i)); MyAdapter myAdapter = new MyAdapter(MainActivity.this, goods, MainActivity.this); xRecyler.setAdapter(myAdapter); } } popupWindow.dismiss(); }else { goods.clear(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getStatus() ==1) { goods.add(list.get(i)); MyAdapter myAdapter = new MyAdapter(MainActivity.this, goods, MainActivity.this); xRecyler.setAdapter(myAdapter); } } popupWindow.dismiss(); } } }); popupWindow = new PopupWindow(view, 200, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent))); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(image,0,10); break; } } }
public class MyAdapter extends RecyclerView.Adapter-----------------------------------------------------------------{ private final Context context; private final List list; private final IView iView; public MyAdapter(Context context, List list, IView iView) { this.context=context; this.list=list; this.iView=iView; } @Override public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item, parent,false); MyViewHoder myViewHoder = new MyViewHoder(view); return myViewHoder; } @Override public void onBindViewHolder(MyViewHoder holder, final int position) { //赋值 holder.title.setText(list.get(position).getTitle() + ""); holder.price.setText(list.get(position).getPrice() + ""); holder.time.setText(list.get(position).getCreatetime() + ""); String bt_text = ""; String tv_text = ""; if (list.get(position).getStatus() == 0) { bt_text = "取消订单"; tv_text="待支付"; //实现状态改变 holder.bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //弹出AlertDailog AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("是否取消?"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //修改Bean的数据的状态 //请求接口,取消订单 int orderid = (int) list.get(position).getOrderid(); PresenterImpl presenter = new PresenterImpl(); presenter.deleteGoods(new ModelImpl(),iView,orderid+""); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }); } else if (list.get(position).getStatus() == 1) { bt_text = "查看订单"; tv_text="已取消"; } else { bt_text = "查看订单"; tv_text="已支付"; } holder.status.setText(tv_text); holder.bt.setText(bt_text); } @Override public int getItemCount() { return list.size(); } class MyViewHoder extends RecyclerView.ViewHolder{ public TextView title; public TextView status; public TextView price; public TextView time; public Button bt; public MyViewHoder(View itemView) { super(itemView); title = itemView.findViewById(R.id.title); status = itemView.findViewById(R.id.status); price = itemView.findViewById(R.id.price); time = itemView.findViewById(R.id.time); bt = itemView.findViewById(R.id.bt); } } }
xml version="1.0" encoding="utf-8"?>//main的xml文件 <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="com.ljn.myapplication.view.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="订单列表" android:textSize="25sp"/> <ImageView android:id="@+id/image" android:layout_width="30dp" android:layout_height="30dp" android:src="@mipmap/ic_launcher"/> LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.75dp" android:background="#000"/> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xRecylerView" android:layout_width="match_parent" android:layout_height="wrap_content"> com.jcodecraeer.xrecyclerview.XRecyclerView> LinearLayout>-----------------------
xml version="1.0" encoding="utf-8"?>//item的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="标题" android:textSize="20sp"/> <TextView android:id="@+id/status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="状态" android:textSize="20sp"/> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="jiage" android:textColor="#F00" android:textSize="20sp"/> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="创建时间" android:textSize="20sp"/> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消订单"/> LinearLayout> LinearLayout>-------------------
xml version="1.0" encoding="utf-8"?>//pop的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#999999" android:orientation="vertical"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content">ListView> LinearLayout>