首先导入依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.github.bumptech.glide:glide:3.6.1' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.jcodecraeer:xrecyclerview:1.3.2'
____________________________________
布局
activity_main.xml
<?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" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.exam.asus.dingdan.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>
____________________________________
adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<OrderBean.DataBean> list; public MyAdapter(Context context, List<OrderBean.DataBean> list) { this.context = context; this.list = list; } @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); final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.tv_title.setText(dataBean.getTitle()); final int status = dataBean.getStatus(); if (status == 0) { myViewHolder.tv_status.setText("待支付"); myViewHolder.tv.setText("取消订单"); myViewHolder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("确定取消订单吗?"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context,"订单已取消",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }); } else if (status == 1) { myViewHolder.tv_status.setText("已支付"); myViewHolder.tv.setText("查看订单"); } else { myViewHolder.tv_status.setText("已取消"); myViewHolder.tv.setText("查看订单"); } myViewHolder.tv_price.setText("价格:" + dataBean.getPrice()); myViewHolder.tv_time.setText("创建时间:" + dataBean.getCreatetime()); myViewHolder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (status == 1){ Toast.makeText(context,"查看订单",Toast.LENGTH_SHORT).show(); }else if (status == 0){ AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("确定取消订单吗?"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context,"订单已取消",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.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); } } }______________________________
model层两个接口
public interface IAddModel { public void doCar(String url, Map<String, String> map, OnNetListener<AddCarBean> onNetListener); }_______
public interface IOrderModel { public void getOrder(String status, String page, OnNetListener<OrderBean> onNetListener); }_______
public class AddModel implements IAddModel { private Handler handler = new Handler(Looper.getMainLooper()); @Override public void doCar(String url, Map<String, String> map, final OnNetListener<AddCarBean> onNetListener) { HttpUtils.getHttpUtils().doPost(url, map, new Callback() { @Override public void onFailure(Call call, IOException e) { onNetListener.OnFailed(e); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final AddCarBean addCarBean = new Gson().fromJson(string, AddCarBean.class); handler.post(new Runnable() { @Override public void run() { onNetListener.OnSuccess(addCarBean); } }); } }); } }______________
public class OrderModel implements IOrderModel { private Handler handler = new Handler(Looper.getMainLooper()); @Override public void getOrder(String status, String page, final OnNetListener<OrderBean> onNetListener) { HashMap<String, String> map = new HashMap<>(); map.put("uid","71"); map.put("status",status); map.put("page",page); HttpUtils.getHttpUtils().doPost(Api.getOrders, map, new Callback() { @Override public void onFailure(Call call, IOException e) { onNetListener.OnFailed(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); } }); } }); } }_____________________________
net
HttpUtils
public class HttpUtils { private static HttpUtils httpUtils; private final OkHttpClient client; public HttpUtils() { 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; } public void doPost(String url, Map<String,String> map, Callback callback){ if (map == null){ throw new RuntimeException("参数为空"); } FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String,String> entry: map.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); } public void doGet(String url,Callback callback){ Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(callback); } }____________
MyApp
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); //Fresco.initialize(this); } }___________
public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); HttpUrl url=original.url().newBuilder() .addQueryParameter("source","android") .build(); //添加请求头 Request request = original.newBuilder() .url(url) .build(); return chain.proceed(request); } }__________
public interface OnNetListener<T> { void OnSuccess(T t); void OnFailed(Exception e); }___________________________________
AddPresenter
public class AddPresenter { private IMainActivity iMainActivity; private final AddModel addModel; public AddPresenter(IMainActivity iMainActivity) { this.iMainActivity = iMainActivity; addModel = new AddModel(); } public void addCar(int uid ){ HashMap<String, String> map = new HashMap<>(); map.put("uid","71"); addModel.doCar(Api.createOrder, map, new OnNetListener<AddCarBean>() { @Override public void OnSuccess(AddCarBean addCarBean) { iMainActivity.addCar(addCarBean); } @Override public void OnFailed(Exception e) { } }); } }_____________
OrderPresenter
public class OrderPresenter { private ISecondActivity iSecondActivity; private final OrderModel orderModel; public OrderPresenter(ISecondActivity iSecondActivity) { this.iSecondActivity = iSecondActivity; orderModel = new OrderModel(); } public void getOrder(final boolean isRefresh, String status, String page){ orderModel.getOrder(status, page, new OnNetListener<OrderBean>() { @Override public void OnSuccess(OrderBean orderBean) { iSecondActivity.showData(isRefresh,orderBean.getData()); } @Override public void OnFailed(Exception e) { } }); } }______________________________
view层
public interface IMainActivity { public void show(String str); public void addCar(AddCarBean addCarBean); }_____
public interface ISecondActivity { public void showData(boolean isRefresh, List<OrderBean.DataBean> list); }_______________________________
MainActivity
public class MainActivity extends AppCompatActivity implements ISecondActivity,IMainActivity, View.OnClickListener{ private ImageView mIvPop; //待支付 private RadioButton mRb1; //已支付 private RadioButton mRb2; //已取消 private RadioButton mRb3; private XRecyclerView mXrv; private int page = 1; private List<OrderBean.DataBean> list = new ArrayList<>(); private MyAdapter myAdapter; private OrderPresenter orderPresenter; private AddPresenter addPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addPresenter = new AddPresenter(this); this.addPresenter.addCar(71); mIvPop = (ImageView) findViewById(R.id.iv_pop); mRb1 = (RadioButton) findViewById(R.id.rb1); mRb2 = (RadioButton) findViewById(R.id.rb2); mRb3 = (RadioButton) findViewById(R.id.rb3); mXrv = (XRecyclerView) findViewById(R.id.xrv); initView(); mXrv.setLayoutManager(new LinearLayoutManager(this)); myAdapter = new MyAdapter(this, list); mXrv.setAdapter(myAdapter); orderPresenter = new OrderPresenter(this); orderPresenter.getOrder(true, "1", page + ""); mXrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page = 1; orderPresenter.getOrder(true, "1", page + ""); } @Override public void onLoadMore() { //加载更多 page++; orderPresenter.getOrder(false, "1", page + ""); } }); } private void initView() { mIvPop.setOnClickListener(this); mRb1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { orderPresenter.getOrder(true, "0", "1"); } }); mRb2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { orderPresenter.getOrder(true, "1", "1"); } }); mRb3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { orderPresenter.getOrder(true, "2", "1"); } }); } @Override public void showData(boolean isRefresh, List<OrderBean.DataBean> newlist) { if (isRefresh) { list.clear(); list.addAll(newlist); mXrv.refreshComplete(); } else { list.addAll(newlist); mXrv.setLoadingMoreEnabled(false); } myAdapter.notifyDataSetChanged(); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.iv_pop: 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); final PopupWindow popupWindow = new PopupWindow(view, layoutParams.width, layoutParams.height); //popupWindow.showAsDropDown(mIvPop, 0, 30); popupWindow.setBackgroundDrawable(new ColorDrawable()); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(mIvPop); //popupWindow.setFocusable(true); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "待支付", Toast.LENGTH_SHORT).show(); orderPresenter.getOrder(true, "0", "1"); popupWindow.dismiss(); } }); tv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "已支付", Toast.LENGTH_SHORT).show(); orderPresenter.getOrder(true, "1", "1"); popupWindow.dismiss(); } }); tv3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show(); orderPresenter.getOrder(true, "2", "1"); popupWindow.dismiss(); } }); break; } } @Override public void show(String str) { } @Override public void addCar(AddCarBean addCarBean) { } }