Android 订单管理,查询,修改,MVP,OkHttp3的二次封装,
来源:互联网 发布:单片机程序软件 编辑:程序博客网 时间:2024/05/17 22:30
一言不合就上图
老套路先注入依赖:
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.liaoinstan.springview:library:1.2.6' compile 'com.github.bumptech.glide:glide:3.7.0'
然后是网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
然后布局:
activity_main.xml:
<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="60dp" android:textColor="#fff000" android:textSize="20sp" android:text="商品列表" android:gravity="center" android:background="#375478" /> <RadioGroup android:gravity="center" android:orientation="horizontal" android:id="@+id/group" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:padding="10dp" android:button="@null" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="全部" android:id="@+id/raido01" android:textSize="18sp" /> <RadioButton android:padding="10dp" android:button="@null" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="待支付" android:id="@+id/raido02" android:textSize="18sp" /> <RadioButton android:textSize="18sp" android:padding="10dp" android:button="@null" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="已支付" android:id="@+id/raido03" /> <RadioButton android:padding="10dp" android:button="@null" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="已取消" android:id="@+id/raido04" android:textSize="18sp" /> </RadioGroup> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager></LinearLayout>
然后是fragmentone.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_marginBottom="10dp" android:background="#fff" android:layout_height="80dp"> <LinearLayout android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:layout_gravity="center" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:id="@+id/titleIt" /> <TextView android:textColor="#f00" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="价格" android:id="@+id/price" /> <TextView android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timeGreate" android:text="时间" /> </LinearLayout> <LinearLayout android:id="@+id/linear" android:layout_marginRight="10dp" android:gravity="center" android:layout_alignParentRight="true" android:layout_gravity="center" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:text="已取消" /> <Button android:layout_marginTop="20dp" android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮" /> </LinearLayout> <TextView android:layout_below="@+id/linear" android:layout_width="match_parent" android:layout_height="1dp" android:background="#f00" /></RelativeLayout>
再然后是fragment_item1.xml:
<?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="match_parent"> <com.liaoinstan.springview.widget.SpringView android:id="@+id/springView" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView01" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </com.liaoinstan.springview.widget.SpringView></LinearLayout>
fragment_item2.xml 和fragment_item2.xml 的布局
然后是fragment_item2.xml 和fragment_item2.xml 的布局和fragment_item1的布局一模一样:
这里就不粘了
然后是:
四个fragment类:
FragmentOne类:
public class FragmentOne extends Fragment implements MyPresenter.PresenterListeren, PresenterChange.PresenterChangeListener { private RecyclerView recyclerView; private SpringView springView; private int num=1; private MyAdapter myAdapter; private MyPresenter myPresenter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item1,container,false); recyclerView = view.findViewById(R.id.recyclerView01); springView = view.findViewById(R.id.springView); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); myPresenter = new MyPresenter(); myPresenter.getData("71","1",this); springView.setHeader(new DefaultHeader(getActivity())); springView.setFooter(new DefaultFooter(getActivity())); springView.setListener(new SpringView.OnFreshListener() { @Override public void onRefresh() { num=1; String s = String.valueOf(num); myPresenter.getData("71",s,FragmentOne.this); springView.onFinishFreshAndLoad(); } @Override public void onLoadmore() { num++; String s = String.valueOf(num); myPresenter.getData("71",s,FragmentOne.this); springView.onFinishFreshAndLoad(); } }); } @Override public void success(List<DataDataBean.DataBean> data) { recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); myAdapter = new MyAdapter(getActivity(),data); myAdapter.setChangeStatus(new MyAdapter.SetStatus() { @Override public void getStatus(String status, String id) { PresenterChange presenterChange = new PresenterChange(FragmentOne.this); presenterChange.getData(status,id); } }); recyclerView.setAdapter(myAdapter); } @Override public void failed() { } @Override public void mySuccess(DataChangeBean dataChangeBean) { myPresenter.getData("71","1",FragmentOne.this); if(dataChangeBean.getCode().equals("0")){ Toast.makeText(getActivity(),dataChangeBean.getMsg()+"",Toast.LENGTH_SHORT).show(); } }}
然后是FragmentTwo类:
public class FragmentTwo extends Fragment implements MyPresenterOther.PresenterListerenOther { private RecyclerView recyclerView; private SpringView springView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item2,container,false); recyclerView = view.findViewById(R.id.recyclerView01); springView = view.findViewById(R.id.springView); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final MyPresenterOther myPresenter = new MyPresenterOther(); myPresenter.getData("71","1","0",this); } @Override public void success(List<DataDataBean.DataBean> data) { recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); MyAdapter myAdapter = new MyAdapter(getActivity(),data); recyclerView.setAdapter(myAdapter); } @Override public void failed() { }}
然后是FragmentThree的类:
public class FragmentThree extends Fragment implements MyPresenterOther.PresenterListerenOther { private RecyclerView recyclerView; private SpringView springView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item3,container,false); recyclerView = view.findViewById(R.id.recyclerView01); springView = view.findViewById(R.id.springView); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final MyPresenterOther myPresenter = new MyPresenterOther(); myPresenter.getData("71","1","1",this); } @Override public void success(List<DataDataBean.DataBean> data) { recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); MyAdapter myAdapter = new MyAdapter(getActivity(),data); recyclerView.setAdapter(myAdapter); } @Override public void failed() { }}
最后是FragmentFour的类:
public class FragmentFour extends Fragment implements MyPresenterOther.PresenterListerenOther { private RecyclerView recyclerView; private SpringView springView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item1,container,false); recyclerView = view.findViewById(R.id.recyclerView01); springView = view.findViewById(R.id.springView); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final MyPresenterOther myPresenter = new MyPresenterOther(); myPresenter.getData("71","1","2",this); } @Override public void success(List<DataDataBean.DataBean> data) { recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); MyAdapter myAdapter = new MyAdapter(getActivity(),data); recyclerView.setAdapter(myAdapter); } @Override public void failed() { }}
然后是每个Bean类:
先是DataChangeBean类:
public class DataChangeBean { /** * msg : 订单状态修改成功 * code : 0 */ private String msg; private String code; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; }}
然后是DataDataBean类:
public class DataDataBean { /** * msg : 请求成功 * code : 0 * data : [{"createtime":"2017-10-19T20:28:43","orderid":20,"price":100,"status":2,"title":"订单标题测试3","uid":71},{"createtime":"2017-10-19T20:44:40","orderid":31,"price":11800,"status":2,"title":"订单标题测试14","uid":71},{"createtime":"2017-10-19T20:44:51","orderid":32,"price":11800,"status":1,"title":"订单标题测试15","uid":71},{"createtime":"2017-10-20T08:02:07","orderid":43,"price":11800,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-10-20T08:02:16","orderid":44,"price":11800,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-10-22T15:14:39","orderid":890,"price":11800,"status":2,"title":"","uid":71},{"createtime":"2017-11-09T09:17:20","orderid":1446,"price":99.99,"status":1,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1447,"price":567,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1448,"price":256.99,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1449,"price":399,"status":2,"title":"订单标题测试","uid":71}] * page : 1 */ private String msg; private String code; private String page; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * createtime : 2017-10-19T20:28:43 * orderid : 20 * price : 100.0 * status : 2 * title : 订单标题测试3 * uid : 71 */ private String createtime; private int orderid; private double price; private int status; private String title; private int uid; public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public int getOrderid() { return orderid; } public void setOrderid(int orderid) { this.orderid = orderid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } }}
工具类
OkHttpUtils的工具类:
public class OkHttpUtils { private static OkHttpUtils okHttpUtils=null; private static OkHttpClient client; private OkHttpUtils(){ } public static OkHttpUtils getInstance(){ if(okHttpUtils == null){ okHttpUtils = new OkHttpUtils(); client = new OkHttpClient().newBuilder() .readTimeout(20, TimeUnit.SECONDS) .connectTimeout(20,TimeUnit.SECONDS) .build(); } return okHttpUtils; } public void doGet(String url, Callback callback){ Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(callback); } public void doPost(String url, Map<String,String> params, Callback callback){ 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); }}
MVP模块
M层:
MyModule类:
public class MyModule { public void getData(String uid,String page, final ModuleListeren moduleListeren){ Map<String,String> map = new HashMap<>(); map.put("uid",uid); map.put("page",page); map.put("source","android"); OkHttpUtils.getInstance().doPost("https://www.zhaoapi.cn/product/getOrders", map, new Callback() { @Override public void onFailure(Call call, IOException e) { if(moduleListeren !=null){ moduleListeren.failed(e); } } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); if(moduleListeren!=null){ moduleListeren.success(string); } } }); } public interface ModuleListeren{ void success(String s); void failed(Exception e); }}
ModuleChange类:
public class ModuleChange { public void getData(String status, String id, final ModuleChangeListener moduleChangeListener){ OkHttpUtils.getInstance().doGet("http://120.27.23.105/product/updateOrder?uid=71&status="+status+"&orderId="+id, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); moduleChangeListener.success(string); } }); } public interface ModuleChangeListener{ void success(String s); }}
MyModuleOther的类:
public class MyModuleOther { public void getData(String uid,String page,String status, final ModuleListeren moduleListeren){ Map<String,String> map = new HashMap<>(); map.put("uid",uid); map.put("page",page); map.put("status",status); map.put("source","android"); OkHttpUtils.getInstance().doPost("https://www.zhaoapi.cn/product/getOrders", map, new Callback() { @Override public void onFailure(Call call, IOException e) { if(moduleListeren !=null){ moduleListeren.failed(e); } } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); if(moduleListeren!=null){ moduleListeren.success(string); } } }); } public interface ModuleListeren{ void success(String s); void failed(Exception e); }}
P层:
MyPresenter类:
public class MyPresenter implements RadioGroup.OnCheckedChangeListener{ MyModule myModule = new MyModule(); Handler handler =new Handler(Looper.getMainLooper()); ContentListeren contentListeren; public void setToast(ContentListeren contentListeren) { this.contentListeren =contentListeren; } public void getData(String uid,String page, final PresenterListeren presenterListeren){ myModule.getData(uid,page, new MyModule.ModuleListeren() { @Override public void success(final String s) { handler.post(new Runnable() { @Override public void run() { DataDataBean dataDataBean = new Gson().fromJson(s, DataDataBean.class); List<DataDataBean.DataBean> data = dataDataBean.getData(); presenterListeren.success(data); } }); } @Override public void failed(Exception e) { } }); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ default:break; case R.id.raido01: contentListeren.setContent(0); break; case R.id.raido02: contentListeren.setContent(1); break; case R.id.raido03: contentListeren.setContent(2); break; case R.id.raido04: contentListeren.setContent(3); break; } } public interface PresenterListeren{ void success( List<DataDataBean.DataBean> data); void failed(); } public interface ContentListeren{ void setContent(int i); }}
PresenterChange的类:
public class PresenterChange { private ModuleChange moduleChange = new ModuleChange(); PresenterChangeListener presenterChangeListener; public PresenterChange(PresenterChangeListener presenterChangeListener) { this.presenterChangeListener = presenterChangeListener; } private Handler handler = new Handler(Looper.getMainLooper()); public void getData(String status,String id){ moduleChange.getData(status, id, new ModuleChange.ModuleChangeListener() { @Override public void success(final String s) { handler.post(new Runnable() { @Override public void run() { DataChangeBean dataChangeBean = new Gson().fromJson(s, DataChangeBean.class); if(presenterChangeListener !=null){ presenterChangeListener.mySuccess(dataChangeBean); } } }); } }); } public interface PresenterChangeListener{ void mySuccess(DataChangeBean dataChangeBean); }}
MyPresenterOther类:
public class MyPresenterOther { MyModuleOther myModule = new MyModuleOther(); Handler handler =new Handler(Looper.getMainLooper()); public void getData(String uid,String page,String status, final PresenterListerenOther presenterListeren){ myModule.getData(uid,page,status, new MyModuleOther.ModuleListeren() { @Override public void success(final String s) { handler.post(new Runnable() { @Override public void run() { DataDataBean dataDataBean = new Gson().fromJson(s, DataDataBean.class); List<DataDataBean.DataBean> data = dataDataBean.getData(); presenterListeren.success(data); } }); } @Override public void failed(Exception e) { } }); } public interface PresenterListerenOther{ void success(List<DataDataBean.DataBean> data); void failed(); }}
适配器:
MyViewpagerAdapter的类:
public class MyViewpagerAdapter extends FragmentPagerAdapter { List<Fragment> data; public MyViewpagerAdapter(FragmentManager fm, List<Fragment> data) { super(fm); this.data=data; } @Override public Fragment getItem(int position) { return data.get(position); } @Override public int getCount() { return data.size(); }}
MyAdapter类:
public class MyAdapter extends RecyclerView.Adapter{ Context context; List<DataDataBean.DataBean> data; public MyAdapter(Context context, List<DataDataBean.DataBean> data) { this.context = context; this.data = data; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.fragmentone,null); return new ViewHolder1(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { ViewHolder1 holder1 = (ViewHolder1) holder; holder1.time.setText("创建时间:"+data.get(position).getCreatetime()); holder1.title.setText(data.get(position).getTitle()); holder1.price.setText("价格:"+data.get(position).getPrice()+""); if(data.get(position).getStatus() ==0){ holder1.textView.setTextColor(Color.RED); holder1.btn.setText("取消订单"); holder1.textView.setText("待支付"); }else if(data.get(position).getStatus()==1){ holder1.btn.setText("查看订单"); holder1.textView.setText("已支付"); }else{ holder1.btn.setText("已取消"); holder1.textView.setText("查看订单"); } //点击事件,接口回调 holder1.btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(setStatus!=null){ setStatus.getStatus(data.get(position).getStatus()+"",data.get(position).getOrderid()+""); notifyDataSetChanged(); } } }); } SetStatus setStatus; public void setChangeStatus(SetStatus setStatus){ this.setStatus = setStatus; } //点击接口回调 public interface SetStatus{ void getStatus(String status,String id); } @Override public int getItemCount() { return data.size(); } class ViewHolder1 extends RecyclerView.ViewHolder { TextView title; TextView price; Button btn; TextView textView; TextView time; public ViewHolder1(View itemView) { super(itemView); time = itemView.findViewById(R.id.timeGreate); title = itemView.findViewById(R.id.titleIt); price = itemView.findViewById(R.id.price); btn = itemView.findViewById(R.id.btn); textView = itemView.findViewById(R.id.textview); } }}
MainActivity主类:
public class MainActivity extends AppCompatActivity { /** * 待支付 */ private RadioButton mRaido01; /** * 已支付 */ private RadioButton mRaido02; /** * 已取消 */ private RadioButton mRaido03; private RadioGroup mGroup; private ViewPager mViewpager; private MyPresenter myPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); myPresenter = new MyPresenter(); mGroup.setOnCheckedChangeListener(myPresenter); List<Fragment> list = new ArrayList<>(); list.add(new FragmentOne()); list.add(new FragmentTwo()); list.add(new FragmentThree()); list.add(new FragmentFour()); myPresenter.setToast(new MyPresenter.ContentListeren() { @Override public void setContent(int i) { switch (i){ default:break; case 0: mViewpager.setCurrentItem(0,true); break; case 1: mViewpager.setCurrentItem(1,true); break; case 2: mViewpager.setCurrentItem(2,true); break; case 3: mViewpager.setCurrentItem(3,true); break; } } }); MyViewpagerAdapter myViewpagerAdapter = new MyViewpagerAdapter(getSupportFragmentManager(),list); mViewpager.setAdapter(myViewpagerAdapter); } private void initView() { mRaido01 = (RadioButton) findViewById(R.id.raido01); mRaido02 = (RadioButton) findViewById(R.id.raido02); mRaido03 = (RadioButton) findViewById(R.id.raido03); mGroup = (RadioGroup) findViewById(R.id.group); mViewpager = (ViewPager) findViewById(R.id.viewpager); }}
ok大功告成
强调几点:
1我的Android studio是2.3.3版本,SDK下-v7:26.+’
compile 'com.android.support:appcompat-v7:26.+'
阅读全文
0 0
- Android 订单管理,查询,修改,MVP,OkHttp3的二次封装,
- Android OkHttp3的二次封装
- 基于MVP架构的OKHttp3的封装
- android okhttp3的封装
- Android——MVP架构OkHttp的二次封装以及RecyclerView的使用
- 订单的查询与修改
- OkHttp3二次封装和拦截器
- Android Volley+OkHttp3+Gson 开源库的封装
- Android Volley+OkHttp3+Gson 开源库的封装
- Android Volley+OkHttp3+Gson 开源库的封装
- Android okhttp3封装
- Lucene查询结果Hits的二次封装
- Lucene查询结果Hits的二次封装
- Lucene查询结果Hits的二次封装 .
- OkHttp3的简单封装
- OkHttp3 的封装
- OKHTTP3的封装
- Android二次封装的网络框架 AndNet
- ajax上传表单(带文件)刷新页面问题
- IT行业风投
- 如何修改MySql数据库的数据文件位置
- 简单的数据结构
- Redis事务和锁的应用详解
- Android 订单管理,查询,修改,MVP,OkHttp3的二次封装,
- 展开二级列表
- PHP关联数组的10个操作技巧
- Redis介绍
- 织梦移动端如何修改熊掌号提交改造分析案例
- 第一篇博文
- linux学习第四十篇:访问日志不记录静态文件,访问日志切割,静态元素过期时间
- 每天一个linux命令(20):find命令之exec
- 高性能jredis服务