簡單實現京東訂單功能
阿新 • • 發佈:2019-02-14
提示 慎重複制
1 網路許可權
2 匯入依賴
compile 'com.android.support:recyclerview-v7:25.1.0' compile 'com.squareup.okhttp3:okhttp:3.4.1'
導gsonjar包
3程式碼
主頁面佈局////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?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" tools:context="com.example.zhangkai20180427.MainActivity" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="訂單" android:layout_weight="9" android:layout_gravity="center" android:gravity="center" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"android:src="@mipmap/ic_launcher" android:layout_weight="1" android:id="@+id/btn" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="待支付" android:id="@+id/onefu" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="已支付" android:id="@+id/twofu" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="已取消" android:id="@+id/threefu" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fram" ></FrameLayout> </LinearLayout> item佈局/////////////////////////////////////////////////////////////////////////////////////<?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:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/titledai" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dai" android:layout_alignParentRight="true" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="價格 : " android:id="@+id/ll" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pricedai" android:layout_toRightOf="@+id/ll" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="建立時間:" android:id="@+id/yy" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/yy" android:id="@+id/timedai" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消訂單" android:layout_alignParentRight="true" android:id="@+id/btndai" /> </RelativeLayout> </LinearLayout>第一個fragment的佈局///////////////////////////////////////////////////////////////////<?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"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycle1" ></android.support.v7.widget.RecyclerView> </LinearLayout>第二個fragment的佈局///////////////////////////////////////////////////////////////////<?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"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycle2" ></android.support.v7.widget.RecyclerView> </LinearLayout>沒錯 就改了個id------第三個fragment的佈局///////////////////////////////////////////////////////////////////<?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"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycle3" ></android.support.v7.widget.RecyclerView> </LinearLayout>
彈出選擇訂單型別的popupwindow////////////////////////////////////////////////////////////////<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="#2510eb" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="待支付" android:id="@+id/dai" android:textColor="#ffffff" android:padding="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已支付" android:id="@+id/yifu" android:textColor="#ffffff" android:padding="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已取消" android:id="@+id/yixiao" android:textColor="#ffffff" android:padding="20dp" /> </LinearLayout> 第一個介面卡///////////////////////////////////////////////////////////////////////////////package com.example.zhangkai20180427.adapter; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.zhangkai20180427.R; import com.example.zhangkai20180427.bean.SuccessBeanorderdd; import com.example.zhangkai20180427.presenter.LoginPresenterorderdd; import com.example.zhangkai20180427.presenter.Presenterorderdd; import com.example.zhangkai20180427.view.LoginVieworderdd; import java.util.List; /** * Created by Administrator on 2018/4/27 0027. */ public class MyAdapterorderdd1 extends RecyclerView.Adapter<MyAdapterorderdd1.ViewHolder> implements LoginVieworderdd { private Context context; private List<SuccessBeanorderdd.DataBean> list; private int Viewk_0=0; private int Viewk_1=1; private int Viewk_2=2; private int Viewk_3=3; private View view; private LoginPresenterorderdd loginpresenter; public MyAdapterorderdd1(Context context, List<SuccessBeanorderdd.DataBean> list){ this.context = context; this.list=list; } /** * * @param parent * @param viewType * @return */ @Override public MyAdapterorderdd1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(context).inflate(R.layout.itemoneorderdd,parent,false); RecyclerView.ViewHolder viewHolder = new ViewHolder(view); Log.d("aaa",viewType+""); return (ViewHolder) viewHolder; } /* 生命自己的一個事件回撥介面*/ public interface OnItemClickListener{ /*條目點選事件 */ void setOnItemClick(View view,int position); /*條目長按事件 */ void setOnItemLongClick(View view,int position); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } @Override public void onBindViewHolder(MyAdapterorderdd1.ViewHolder holder, final int position) { loginpresenter=new Presenterorderdd(this); Log.d("uuu",list.get(position).getStatus()+""); if((list.get(position).getStatus()==0)){ Log.d("ikikik",list.get(0).getPrice()+""); if(null==list.get(position).getTitle()){ holder.titledai.setText(""); }else{ holder.titledai.setText(list.get(position).getTitle().toString()); } holder.dai.setText("待支付"); holder.pricedai.setText(list.get(position).getPrice()+""); holder.timedai.setText(list.get(position).getCreatetime()); } //刪除訂單 holder.btndai.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 通過AlertDialog.Builder這個類來例項化我們的一個AlertDialog的物件 AlertDialog.Builder builder = new AlertDialog.Builder(context); // 設定Title的圖示 builder.setIcon(R.mipmap.ic_launcher); // 設定Title的內容 builder.setTitle("注意"); // 設定Content來顯示一個資訊 builder.setMessage("確定刪除嗎?"); // 設定一個PositiveButton builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //刪除當前訂單 loginpresenter.updatechuan(list.get(position).getUid(),list.get(position).getOrderid(),list.get(position).getStatus()); notifyDataSetChanged(); } }); // 設定一個NegativeButton builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "太好了", Toast.LENGTH_SHORT).show(); } }); // 顯示出該對話方塊 builder.show(); } }); if(null != onItemClickListener) { /*回撥點選事件*/ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.setOnItemClick(v,position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListener.setOnItemLongClick(v,position); return true; } }); } } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ TextView titledai; TextView dai; TextView pricedai; TextView timedai; Button btndai; public ViewHolder(View itemView) { super(itemView); titledai= itemView.findViewById(R.id.titledai); dai=itemView.findViewById(R.id.dai); pricedai=itemView.findViewById(R.id.pricedai); timedai=itemView.findViewById(R.id.timedai); btndai= itemView.findViewById(R.id.btndai); } } @Override public void indentsuccess(SuccessBeanorderdd bean) { } @Override public void indentfailed(SuccessBeanorderdd bean) { } @Override public void updateorder(String d) { Log.d("bgbgbg",d); } @Override public void updateorderfailed(String d) { } } 第二個介面卡//////////////////////////////////////////////////////////////////////////package com.example.zhangkai20180427; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.zhangkai20180427.bean.SuccessBeanorderdd; import java.util.List; /** * Created by Administrator on 2018/4/27 0027. */ public class MyAdapterorderdd2 extends RecyclerView.Adapter<MyAdapterorderdd2.ViewHolder> { private Context context; private List<SuccessBeanorderdd.DataBean> list; private int Viewk_0=0; private int Viewk_1=1; private int Viewk_2=2; private int Viewk_3=3; private View view; public MyAdapterorderdd2(Context context, List<SuccessBeanorderdd.DataBean> list){ this.context = context; this.list=list; } /** * * @param parent * @param viewType * @return */ @Override public MyAdapterorderdd2.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(context).inflate(R.layout.itemoneorderdd,parent,false); RecyclerView.ViewHolder viewHolder = new ViewHolder(view); Log.d("aaa",viewType+""); return (ViewHolder) viewHolder; } /* 生命自己的一個事件回撥介面*/ public interface OnItemClickListener{ /*條目點選事件 */ void setOnItemClick(View view,int position); /*條目長按事件 */ void setOnItemLongClick(View view,int position); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } @Override public void onBindViewHolder(MyAdapterorderdd2.ViewHolder holder, final int position) { Log.d("pcpcpc",list.get(position).getStatus()+""); if((list.get(position).getStatus()==1)){ Log.d("ioioio",list.get(0).getPrice()+""); if(null==list.get(position).getTitle()){ holder.titledai.setText(""); }else{ holder.titledai.setText(list.get(position).getTitle().toString()); } holder.dai.setText("已支付"); holder.pricedai.setText(list.get(position).getPrice()+""); holder.timedai.setText(list.get(position).getCreatetime()); } if(null != onItemClickListener) { /*回撥點選事件*/ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.setOnItemClick(v,position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListener.setOnItemLongClick(v,position); return true; } }); } } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ TextView titledai; TextView dai; TextView pricedai; TextView timedai; Button btndai; public ViewHolder(View itemView) { super(itemView); titledai= itemView.findViewById(R.id.titledai); dai=itemView.findViewById(R.id.dai); pricedai=itemView.findViewById(R.id.pricedai); timedai=itemView.findViewById(R.id.timedai); btndai= itemView.findViewById(R.id.btndai); } } }
第三個介面卡////////////////////////////////////////////////////////////
package com.example.zhangkai2018042