<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
RelativeLayout
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"
android
:background=
"#7878"
tools
:context=
"com.example.liuwei20180427.view.MainActivity">
<
RelativeLayout
android
:id=
"@+id/top"
android
:layout_width=
"match_parent"
android
:layout_height=
"50dp">
<
TextView
android
:layout_width=
"wrap_content"
android
:layout_height=
"wrap_content"
android
:layout_centerInParent=
"true"
android
:text=
"订单列表"
android
:textColor=
"#000"
android
:textSize=
"22sp" />
</
RelativeLayout>
<
View
android
:layout_width=
"match_parent"
android
:layout_height=
"1dp"
android
:layout_below=
"@id/top"
android
:background=
"#787878" />
<
android.support.design.widget.TabLayout
android
:id=
"@+id/myTab"
android
:layout_width=
"match_parent"
android
:layout_height=
"50dp"
android
:layout_below=
"@id/top"
android
:layout_marginTop=
"10dp"
app
:tabGravity=
"fill"
app
:tabIndicatorColor=
"@color/colorAccent"
app
:tabMode=
"fixed"
app
:tabSelectedTextColor=
"@color/colorPrimaryDark"
app
:tabTextColor=
"@color/colorPrimary">
</
android.support.design.widget.TabLayout>
<
View
android
:layout_width=
"match_parent"
android
:layout_height=
"1dp"
android
:layout_below=
"@id/myTab"
android
:background=
"#787878" />
<
android.support.v4.view.ViewPager
android
:id=
"@+id/viewPage"
android
:layout_width=
"match_parent"
android
:layout_height=
"match_parent"
android
:layout_below=
"@id/myTab"
android
:layout_marginTop=
"10dp">
</
android.support.v4.view.ViewPager>
</
RelativeLayout>
public class MainActivity
extends AppCompatActivity {
private TabLayout
myTab;
private ViewPager
viewPage;
private String[]
title = {
"待支付",
"已支付",
"已取消"};
private String[]
urlTitle = {
"0",
"1",
"2"};
private MyAdapter
myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
//初始化
initView();
myAdapter =
new MyAdapter(getSupportFragmentManager());
viewPage.setAdapter(
myAdapter);
myTab.setupWithViewPager(
viewPage); }
private void initView() {
myTab = findViewById(R.id.
myTab);
viewPage = findViewById(R.id.
viewPage);
}
private class MyAdapter
extends FragmentPagerAdapter {
public MyAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public CharSequence getPageTitle(
int position) {
return
title[position];
}
@Override
public Fragment getItem(
int position) {
//创建对象并返回
Bundle bundle =
new Bundle();
bundle.putString(
"url",
urlTitle[position]);
//实例化
PayFragment payFragment =
new PayFragment();
payFragment.setArguments(bundle);
return payFragment;
}
@Override
public int getCount() {
return
title.
length;
}
}
//封装的OKHTTP
public class HttpUtils { //单例模式,把构造方法进行私有化 private HttpUtils(){}; static OkHttpClient client; public static OkHttpClient getInstance(){ if (client==null) { //更加安全 synchronized (HttpUtils.class) { //缓存的地方 mnt/sdcard File file = new File(Environment.getExternalStorageDirectory(), "cache11"); client = new OkHttpClient().newBuilder() .readTimeout(3000, TimeUnit.SECONDS) //设置读取超时时间 .connectTimeout(3000, TimeUnit.SECONDS) //设置连接的超时时间 .cache(new Cache(file, 10 * 1024)) .build(); } } return client; } //post请求 public static void doPost(String url, Map<String,String> parms, Callback callback){ //得到客户端的对像 OkHttpClient client = getInstance(); //不是FormBody,而是一个Builder FormBody.Builder body = new FormBody.Builder(); //key value for (String key:parms.keySet()){ //value的值 body.add(key,parms.get(key)); } Request request = new Request.Builder() .url(url) .post(body.build()) .build(); client.newCall(request).enqueue(callback); } }
//m层
public class MyModel
implements MyModelView{
@Override
public void toModel(String uid, String urlTitle,
final MyPresenterView presenterView) {
OkHttpClient instance = HttpUtils.
getInstance();
HashMap<String,String> hashMap =
new HashMap<>();
hashMap.put(
"uid", uid);
if (urlTitle.trim().equals(
"9")) {
}
else {
hashMap.put(
"status", urlTitle); }
HttpUtils.
doPost(
"http://120.27.23.105/product/getOrders", hashMap,
new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response)
throws IOException {
String str = response.body().string();
if (str !=
null) {
Gson gson =
new Gson();
JsonPayBean jsonPayBean = gson.fromJson(str, JsonPayBean.
class);
if (jsonPayBean !=
null) {
List<JsonPayBean.DataBean> data = jsonPayBean.getData();
if (data !=
null) {
presenterView.toPresenter(data);
} } } } });
}}
//p层
public class MyPresenter
implements MyPresenterView {
LogView
logView;
private MyModelView
modelView;
public MyPresenter(LogView logView){
this.
logView = logView;
modelView =
new MyModel();
};
@Override
public void toPresenter(List<JsonPayBean.DataBean> data) {
logView.toBasckView(data);
}
@Override
public void receive(String uid, String urlTitle) {
modelView.toModel(uid,urlTitle,
this);
}
//内存泄漏
public void onDestroy() {
logView =
null; System.
gc();
}
}
//v层
布局
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
RelativeLayout
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
:background=
"#EBEBEB"
android
:layout_width=
"match_parent"
android
:layout_height=
"match_parent"
android
:id=
"@+id/recyclerView"
>
</
android.support.v7.widget.RecyclerView>
</
RelativeLayout>
//主页面
public class PayFragment
extends Fragment
implements LogView{
private MyPresenterView
presenterView;
MyRecyclerViewAdapter
myAdapter;
String
uid=
"71";
private View
view;
private RecyclerView
rcy;
private Handler
handler =
new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
List<JsonPayBean.DataBean> data = (List<JsonPayBean.DataBean>) msg.
obj;
rcy.setLayoutManager(
new LinearLayoutManager(getActivity(), LinearLayoutManager.
VERTICAL,
false));
myAdapter =
new MyRecyclerViewAdapter(getActivity(), data);
rcy.setAdapter(
myAdapter);
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = View.
inflate(getActivity(), R.layout.
activity_pay,
null);
return
view;
}
@Override
public void onActivityCreated(
@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
//初始化数据
Bundle bundle = getArguments();
String urlTitle = bundle.getString(
"url").toString();
presenterView =
new MyPresenter(
this);
presenterView.receive(
uid,urlTitle);
}
private void initView() {
rcy =
view.findViewById(R.id.
recyclerView);
}
@Override
public void toBasckView(List<JsonPayBean.DataBean> data) {
Message msg = Message.
obtain();
msg.
obj = data;
handler.sendMessage(msg);
}
//内存泄漏
@Override
public void onDestroy() {
super.onDestroy();
if(
presenterView!=
null){
presenterView.onDestroy();
presenterView=
null;
System.
gc();
}
}
//适配器
public class MyRecyclerViewAdapter extends RecyclerView.Adapter { Context context; List<JsonPayBean.DataBean> list; public MyRecyclerViewAdapter(Context context, List<JsonPayBean.DataBean> list) { this.context=context; this.list=list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.item_model, null); MyHolder myHolder = new MyHolder(inflate); return myHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyHolder myViewHolder = (MyHolder) holder; final JsonPayBean.DataBean dataBean = list.get(position); myViewHolder.tvTitle.setText(dataBean.getTitle()); int status = dataBean.getStatus(); myViewHolder.tvBt.setText("查看订单"); myViewHolder.tvStatus.setTextColor(Color.parseColor("#000000")); if (status == 0) { myViewHolder.tvStatus.setText("待支付"); myViewHolder.tvBt.setText("取消订单"); myViewHolder.tvStatus.setTextColor(Color.parseColor("#ff0000")); myViewHolder.tvBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://www.zhaoapi.cn/product/") .addConverterFactory(GsonConverterFactory.create()) .build(); Api api = retrofit.create(Api.class); api.doGet("71","2",dataBean.getOrderid()).enqueue(new Callback<QuxiaoBean>() { @Override public void onResponse(Call<QuxiaoBean> call, Response<QuxiaoBean> response) { AlertDialog.Builder builder = new AlertDialog.Builder( context); builder.setTitle("提示"); builder.setMessage("确定取消吗?"); builder.setIcon(R.drawable.ic_launcher_background); 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) { Toast.makeText(context,"不取消了",Toast.LENGTH_SHORT).show(); } }); builder.show(); } @Override public void onFailure(Call<QuxiaoBean> call, Throwable t) { } }); } }); } else if (status == 2) { myViewHolder.tvStatus.setText("已取消"); } else if (status == 1) { myViewHolder.tvStatus.setText("已支付"); } myViewHolder.tvPrice.setText("价格:" + dataBean.getOrderid()); myViewHolder.tvPrice.setTextColor(Color.parseColor("#ff0000")); myViewHolder.tvTime.setText(dataBean.getCreatetime()); } @Override public int getItemCount() { return list.size(); } class MyHolder extends RecyclerView.ViewHolder{ private final TextView tvTitle; private final TextView tvStatus; private final TextView tvPrice; private final TextView tvTime; private final Button tvBt; public MyHolder(View view) { super(view); tvStatus = view.findViewById(R.id.dingdan); tvTitle = view.findViewById(R.id.name); tvPrice = view.findViewById(R.id.price); tvBt = view.findViewById(R.id.quxiao); tvTime = view.findViewById(R.id.time); } }
//适配器布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:id="@+id/price" android:textColor="#ff00" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/price" android:id="@+id/time" android:layout_marginTop="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:id="@+id/quxiao" android:layout_alignParentRight="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看订单" android:layout_alignParentRight="true" android:id="@+id/dingdan" android:layout_below="@id/quxiao" android:layout_marginTop="15dp" /> </RelativeLayout>