//
OkHttp3Util拦截器请参考以下网址
// http://blog.csdn.net/wei844067872/article/details/79059394
//MainActivity实现Main为了MVP解析数据
public interface Main {
//MyDingDanBean这个是bean的类名void getDingDanBean(MyDingDanBean myDingDanBean);}
//
MainActivity先创建fragment类
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView image_btn;
private TextView text_daizhi;
private TextView text_yizhi;
private TextView text_yiqu;
private FrameLayout frame_layout;
private View item_popup;
private View view;
private PopupWindow popupWindow;
private TextView btn_daizhi;
private TextView btn_yizhi;
private TextView btn_yiqu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_btn = findViewById(R.id.image_btn);
text_daizhi = findViewById(R.id.text_daizhi);
text_yizhi = findViewById(R.id.text_yizhi);
text_yiqu = findViewById(R.id.text_yiqu);
frame_layout = findViewById(R.id.frame_layout);
//https://www.zhaoapi.cn/product/updateOrder?uid=4427&status=1
//https://www.zhaoapi.cn/product/updateOrder?uid=4427&orderId=
//https://www.zhaoapi.cn/product/getOrders?uid=4427&status2
text_daizhi.setOnClickListener(this);
text_yizhi.setOnClickListener(this);
text_yiqu.setOnClickListener(this);
Frag_Daizhi frag_daizhi = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi).commit();
item_popup = View.inflate(this, R.layout.item_popup, null);
view = View.inflate(this, R.layout.activity_main, null);
popupWindow = new PopupWindow(item_popup
, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
btn_daizhi = item_popup.findViewById(R.id.btn_daizhi);
btn_yizhi = item_popup.findViewById(R.id.btn_yizhi);
btn_yiqu = item_popup.findViewById(R.id.btn_yiqu);
btn_daizhi.setOnClickListener(this);
btn_yizhi.setOnClickListener(this);
btn_yiqu.setOnClickListener(this);
image_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
popupWindow.showAsDropDown(image_btn);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.text_daizhi:
Frag_Daizhi frag_daizhi = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi).commit();
break;
case R.id.text_yizhi:
Frag_YiZhi frag_yiZhi = new Frag_YiZhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiZhi).commit();
break;
case R.id.text_yiqu:
Frag_YiQu frag_yiQu = new Frag_YiQu();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiQu).commit();
break;
case R.id.btn_daizhi:
Frag_Daizhi frag_daizhi2 = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi2).commit();
popupWindow.dismiss();
break;
case R.id.btn_yizhi:
Frag_YiZhi frag_yiZhi2 = new Frag_YiZhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiZhi2).commit();
popupWindow.dismiss();
break;
case R.id.btn_yiqu:
Frag_YiQu frag_yiQu2 = new Frag_YiQu();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiQu2).commit();
popupWindow.dismiss();
break;
}
}
}
//fragment 另外两个自己建里面只有个布局
public class Frag_Daizhi extends Fragment implements Main{
private List<MyDingDanBean.DataBean> list = new ArrayList<>();
private View view;
private RecyclerView recycler_daizhi;
private Presenter presenter;
private RelativeLayout relative_bar;
private SmartRefreshLayout smart_refresh;
private int page=0;
private MyDingDanAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_daizhi, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recycler_daizhi = view.findViewById(R.id.recycler_daizhi);
relative_bar = view.findViewById(R.id.relative_bar);
smart_refresh = view.findViewById(R.id.smart_refresh);
presenter = new Presenter(this);
}
@Override
public void onResume() {
super.onResume();
relative_bar.setVisibility(View.VISIBLE);
presenter.getDingUrl(ApiUtil.dingdan,page);
}
@Override
public void getDingDanBean(final MyDingDanBean myDingDanBean) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (myDingDanBean!=null){
list.addAll(myDingDanBean.getData());
myAdapter();
relative_bar.setVisibility(View.GONE);
smart_refresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
list.clear();
list.addAll(0,myDingDanBean.getData());
myAdapter();
smart_refresh.finishRefresh();
}
});
smart_refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
list.clear();
page++;
presenter.getDingUrl(ApiUtil.dingdan,page);
onResume();
list.addAll(myDingDanBean.getData());
myAdapter();
smart_refresh.finishLoadmore();
}
});
}
}
});
}
public void myAdapter(){
if (adapter==null){
recycler_daizhi.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
adapter = new MyDingDanAdapter(getActivity(), list,presenter,page);
recycler_daizhi.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
//RecyclerView适配器
public class MyDingDanAdapter extends RecyclerView.Adapter<MyDingDanHolder> {
private final Context context;
private final List<MyDingDanBean.DataBean> list;
private Presenter presenter;
private int page;
public MyDingDanAdapter(Context context, List<MyDingDanBean.DataBean> list, Presenter presenter, int page) {
this.context = context;
this.list = list;
this.presenter = presenter;
this.page = page;
}
@Override
public MyDingDanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_dingdan,parent, false);
MyDingDanHolder holder = new MyDingDanHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyDingDanHolder holder, final int position) {
holder.text_title.setText(list.get(position).getTitle());
holder.text_price.setText("价格: "+list.get(position).getPrice());
holder.text_tame.setText(list.get(position).getCreatetime());
if (list.get(position).getStatus()==0){
holder.text_daizhifu.setText("待支付");
holder.text_daizhifu.setTextColor(Color.RED);
holder.text_btn.setText("取消订单");
holder.text_btn.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) {
Map<String, String> params=new HashMap<>();
params.put("uid","3690");
params.put("orderId", String.valueOf(list.get(position).getOrderid()));
params.put("status", String.valueOf(2));
OkHttp3Util.doPost(ApiUtil.genxin, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
list.get(position).setStatus(2);
((MainActivity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
} } }); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }); }else if (list.get(position).getStatus()==1){ holder.text_daizhifu.setText("已支付"); holder.text_daizhifu.setTextColor(Color.BLACK); holder.text_btn.setText("查看订单"); }else if (list.get(position).getStatus()==2){ holder.text_daizhifu.setText("已取消"); holder.text_daizhifu.setTextColor(Color.BLACK); holder.text_btn.setText("查看订单"); holder.text_btn.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) { Map<String, String> params = new HashMap<>(); params.put("uid", "3690"); params.put("orderId", String.valueOf(list.get(position).getOrderid())); params.put("status", String.valueOf(0)); OkHttp3Util.doPost(ApiUtil.genxin, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()) {
list.get(position).setStatus(0);
((MainActivity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
} } }); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); } }); } } @Override public int getItemCount() { return list.size(); }}
//适配器里的ViewHolder
public class MyDingDanHolder extends RecyclerView.ViewHolder {
public TextView text_title;
public TextView text_daizhifu;
public TextView text_price;
public TextView text_tame;
public TextView text_btn;
public MyDingDanHolder(View itemView) {
super(itemView);
text_title = itemView.findViewById(R.id.text_title);
text_daizhifu = itemView.findViewById(R.id.text_daizhifu);
text_price = itemView.findViewById(R.id.text_price);
text_tame = itemView.findViewById(R.id.text_tame);
text_btn = itemView.findViewById(R.id.text_btn);
}
}
//MVP中间者的接口
public interface PresenterPort {
//MyDingDanBean这个是bean的类名
void getDingDanBean(MyDingDanBean myDingDanBean);
}
//MVP中间者类
public class Presenter implements PresenterPort {
private Main main;
private final Model model;
public Presenter(Main main) {
model = new Model(this);
this.main = main;
}
@Override
public void getDingDanBean(MyDingDanBean myDingDanBean) {
main.getDingDanBean(myDingDanBean);
}
public void getDingUrl(String dingdan, int page) {
model.getDingUrl(dingdan,page);
}
}
//Model数据逻辑的处理层
public class Model {
private Presenter presenter;
public Model(Presenter presenter) {
this.presenter = presenter;
}
public void getDingUrl(String dingdan, int page) {
Map<String, String> params=new HashMap<>();
params.put("uid","3690");
params.put("page",page+"");
OkHttp3Util.doPost(dingdan, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String json = response.body().string();
MyDingDanBean myDingDanBean = new Gson().fromJson(json, MyDingDanBean.class);
presenter.getDingDanBean(myDingDanBean);
}
}
});
}
}
//路径类
public class ApiUtil {
//uid 用户id字段 String类型 必传
//status 订单状态 String类型 非必传(筛选订单列表时,必传)
public static String dingdan="https://www.zhaoapi.cn/product/getOrders";
//uid 用户id字段 String类型 必传
//orderId 订单id参数 String类型 必传
//status 订单状态 String类型 必传
public static String genxin="https://www.zhaoapi.cn/product/updateOrder";
}
//实现控件边框
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0.2dp" android:color="#000"/>
<solid android:color="#fff"/>
</shape>
//点击变色
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/beijing"/>
<item android:state_pressed="false" android:drawable="@color/baise"/>
<item android:drawable="@color/baise"/>
</selector>
//colors颜色
<color name="beijing">#69C3D7</color>
<color name="baise">#ffffff</color>
<color name="hongde">#ff0000</color>
//布局网址
http://blog.csdn.net/wei844067872/article/details/79059442