//MainActivity类
public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener{ private XRecyclerView xrc; private Myadapter myadapter; private Presenter presenter; private ImageView iv; private PopupWindow popupWindow; String[] arr={"待支付","已支付","已取消"}; List<Goodbean.DataBean> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化id innitid(); presenter = new Presenter(); presenter.getmv(this,this,new Mymode()); //上啦加载下拉刷新的方法 shuaxinjiazai(); //设置图片的点击事件 iv.setOnClickListener(this); } private void shuaxinjiazai() { xrc.setLoadingListener(new XRecyclerView.LoadingListener() { //刷新的方法 @Override public void onRefresh() { //刷新时从新调用下适配器 Presenter presenter=new Presenter(); presenter.getmv(MainActivity.this,MainActivity.this,new Mymode()); //关闭刷新 xrc.refreshComplete(); } //下拉加载的方法 @Override public void onLoadMore() { Toast.makeText(MainActivity.this,"加载更多",Toast.LENGTH_SHORT).show(); //调用p层加载更多的方法 presenter.pjiazaigengduo(MainActivity.this,MainActivity.this,new Mymode()); xrc.refreshComplete(); } }); } private void innitid() { xrc = findViewById(R.id.xrc); iv = findViewById(R.id.iv); } //默认设置适配器的方法 @Override public void setmyadapter(Context context, List<Goodbean.DataBean> list) { //给全局变量赋值 this.list=list; myadapter = new Myadapter(context,list,this); xrc.setAdapter(myadapter); xrc.setLayoutManager(new LinearLayoutManager(this)); } //上啦加载更多的方法 @Override public void vjiazaigengduo(Context context,List<Goodbean.DataBean> list) { //adapter适配器里面必须有set get方法 List<Goodbean.DataBean> data = myadapter.getList(); data.addAll(list); myadapter.notifyDataSetChanged(); } //d点击图片时 设置适配器的方法 @Override public void settuadapter(Context context, List<Goodbean.DataBean> list) { myadapter = new Myadapter(context,list,this); xrc.setAdapter(myadapter); } //图片的点击事件 @Override public void onClick(View v) { //pop_item 布局为listview View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item,null); ListView listview = view.findViewById(R.id.listview); //为listview设置适配器 listview.setAdapter(new BaseAdapter() { private TextView textView; @Override public int getCount() { return arr.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) { if (convertView==null){ textView = new TextView(MainActivity.this); } textView.setText(arr[position]); textView.setTextSize(30); return textView; } }); // popupWindow显示在底部 popupWindow = new PopupWindow(view, 140, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent))); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(iv,0,10); //设置listview的点击事件 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { List<Goodbean.DataBean> list1=new ArrayList<Goodbean.DataBean>(); @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (arr[position].equals("待支付")){ //遍历集合的数据 for (int j = 0;j<list.size(); j++) { //判断数据的值 ==0 取出来放到新的集合 用新的集合重新设置适配器 if (list.get(j).getStatus()==0.0){ Goodbean.DataBean dataBean = list.get(j); list1.add(dataBean); //从新设置适配器 presenter.diantu(MainActivity.this, MainActivity.this, new Mymode(), list1); } } popupWindow.dismiss(); } if (arr[position].equals("已支付")){ //遍历集合的数据 for (int j = 0;j<list.size(); j++) { //判断数据的值 ==1.0 取出来放到新的集合 用新的集合重新设置适配器 if (list.get(j).getStatus()==1.0){ Goodbean.DataBean dataBean = list.get(j); list1.add(dataBean); //从新设置适配器 presenter.diantu(MainActivity.this,MainActivity.this,new Mymode(),list1); } } popupWindow.dismiss(); } if (arr[position].equals("已取消")){ //遍历集合的数据 for (int j = 0;j<list.size(); j++) { //判断数据的值 ==2.0 取出来放到新的集合 用新的集合重新设置适配器 if (list.get(j).getStatus()==2.0){ Goodbean.DataBean dataBean = list.get(j); list1.add(dataBean); //从新设置适配器 presenter.diantu(MainActivity.this,MainActivity.this,new Mymode(),list1); } } popupWindow.dismiss(); } } }); } }
V层的接口
public interface Iview { //设置适配器的方法 void setmyadapter(Context context,List<Goodbean.DataBean> list); //上拉加载更多的方法 void vjiazaigengduo(Context context,List<Goodbean.DataBean> list); //点击图片设置适配器的方法 void settuadapter(Context context,List<Goodbean.DataBean> list); }
//p层的接口
public interface Ipresenter { void getmv(Context context, Iview iview, Imode imode); //上啦加载更多的方法 void pjiazaigengduo(Context context, Iview iview, Imode imode); //修改的方法 void upgood(Context context, Iview iview, Imode imode,int id); //点击图片时的适配器 void diantu(Context context, Iview iview, Imode imode,List<Goodbean.DataBean> list1); }
//p层的代码
public class Presenter implements Ipresenter { @Override public void getmv(final Context context, final Iview iview, Imode imode) { Map<String,String> map=new HashMap<>(); map.put("uid","71"); map.put("page","1"); imode.getnetjsonbean(JIekou.net, map, new Gouwuchelisteneter() { @Override public void gouwucheListeneter(List<Goodbean.DataBean> data) { //调用v层设置适配器的代码 iview.setmyadapter(context,data); } }); } //上啦加载更多的方法 @Override public void pjiazaigengduo(final Context context, final Iview iview, Imode imode) { int i=1; i++; Map<String,String> map=new HashMap<>(); map.put("uid","71"); map.put("page",""+i); imode.getnetjsonbean(JIekou.net, map, new Gouwuchelisteneter() { @Override public void gouwucheListeneter(List<Goodbean.DataBean> data) { iview.vjiazaigengduo(context,data); } }); } @Override public void upgood(final Context context, final Iview iview, final Imode imode, int id) { Map<String,String> map=new HashMap<>(); map.put("uid","71"); map.put("orderId",id+""); map.put("status","2"); imode.xiugai(JIekou.update, map, new Xiugailisteneter() { @Override public void xiugaiListeneter(UpGoodbean upGoodbean) { //如果成功从新设置适配器 if (upGoodbean.getCode().equals("0")){ Map<String,String> map=new HashMap<>(); map.put("uid","71"); map.put("page","1"); imode.getnetjsonbean(JIekou.net, map, new Gouwuchelisteneter() { @Override public void gouwucheListeneter(List<Goodbean.DataBean> data) { //调用v层设置适配器的代码 iview.setmyadapter(context,data); } }); Log.d("成功", "xiugaiListeneter: 开心"); }else { Log.d("失败", "xiugaiListeneter: 大哭"); } } }); } //点击图片时 从新设置适配器 @Override public void diantu(Context context, Iview iview, Imode imode,List<Goodbean.DataBean> list1) { //调用v层设置适配器的方法 新的适配器的方法 iview.settuadapter(context,list1); } }
//适配器的代码
public class Myadapter extends RecyclerView.Adapter <Myadapter.Myhodler>{ private Context context; private List<Goodbean.DataBean> list; private Iview iview; public Myadapter(Context context, List<Goodbean.DataBean> list,Iview iview) { this.context = context; this.list = list; this.iview=iview; } public Context getContext() { return context; } public void setContext(Context context) { this.context = context; } public List<Goodbean.DataBean> getList() { return list; } public void setList(List<Goodbean.DataBean> list) { this.list = list; } @Override public Myhodler onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.xrc, null); Myhodler myhodler=new Myhodler(view); return myhodler; } @Override public void onBindViewHolder(Myhodler holder, final int position) { holder.title.setText(list.get(position).getTitle()); holder.prive.setText("价格"+list.get(position).getPrice()+""); holder.time.setText("创建时间:"+list.get(position).getCreatetime()+""); if (list.get(position).getStatus()==0.0){ holder.zhi.setTextColor(Color.RED); holder.zhi.setText("待支付"); holder.dan.setText("取消订单"); } if (list.get(position).getStatus()==1.0){ holder.zhi.setText("已支付"); holder.dan.setText("查看订单"); } if (list.get(position).getStatus()==2.0){ holder.zhi.setTextColor(Color.YELLOW); holder.zhi.setText("已取消"); holder.dan.setText("删除订单"); } //如果是待支付的话点击改变取消订单 if (holder.zhi.getText().toString().equals("待支付")) { holder.dan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //弹出框 AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("是否取消?"); AlertDialog alertDialog = builder.create(); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //获取点击时 订单的id double orderid = list.get(position).getOrderid(); Presenter presenter=new Presenter(); presenter.upgood(context,iview,new Mymode(),(int)orderid); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }); } } @Override public int getItemCount() { return list.size(); } class Myhodler extends RecyclerView.ViewHolder{ private TextView title; private TextView prive; private TextView time; private TextView zhi; private Button dan; public Myhodler(View itemView) { super(itemView); this.title=itemView.findViewById(R.id.title); this.prive=itemView.findViewById(R.id.prive); this.time=itemView.findViewById(R.id.time); this.zhi=itemView.findViewById(R.id.zhi); this.dan=itemView.findViewById(R.id.dan); } } }
//m层的方法
public class Mymode implements Imode { //获取购物车数据的方法 Okhttp okhttp=new Okhttp(); @Override public void getnetjsonbean(String uri, Map<String, String> map, final Gouwuchelisteneter gouwuchelisteneter) { okhttp.getnetpost(uri,map); okhttp.setOnOKHttpGetListener(new Okhttp.OkhttpgetListener() { @Override public void error(String error) { } @Override public void success(String json) { Gson gson = new Gson(); Goodbean goodbean = gson.fromJson(json, Goodbean.class); List<Goodbean.DataBean> data = goodbean.getData(); gouwuchelisteneter.gouwucheListeneter(data); } }); } //修改的方法 @Override public void xiugai(String uri, Map<String, String> map, final Xiugailisteneter xiugailisteneter) { okhttp.getnetpost(uri,map); okhttp.setOnOKHttpGetListener(new Okhttp.OkhttpgetListener() { @Override public void error(String error) { } @Override public void success(String json) { Gson gson = new Gson(); UpGoodbean upGoodbean = gson.fromJson(json, UpGoodbean.class); xiugailisteneter.xiugaiListeneter(upGoodbean); } }); } }
//xrc的布局文件
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_marginTop="20px" android:layout_marginLeft="40px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title"/> <TextView android:textColor="#ff0000" android:layout_marginTop="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/prive"/> <TextView android:layout_marginTop="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/time"/> </LinearLayout> <LinearLayout android:layout_marginLeft="280px" android:layout_marginTop="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/zhi" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="查看订单" android:id="@+id/dan" android:layout_marginTop="80px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>