首先将购物车中确认订单的钱数传递到ConfirmActivity
接着购物车SecondActivity开始
package com.example.peng.goodscard_1510d.view;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.example.peng.goodscard_1510d.R;
import com.example.peng.goodscard_1510d.adapter.ElvAdapter;
import com.example.peng.goodscard_1510d.bean.GetCartBean;
import com.example.peng.goodscard_1510d.bean.PriceAndCount;
import com.example.peng.goodscard_1510d.presenter.GetCartPresenter;
import java.util.List;
public class SecondActivity extends AppCompatActivity implements ISecondListener {
private GetCartPresenter getCartPresenter;
private ExpandableListView mElv;
/**
* 全选
*/
private CheckBox mCb;
/**
* 合计:
*/
private TextView mTvTotal;
/**
* 去结算(0)
*/
private TextView mTvCount;
private ElvAdapter elvAdapter;
private PriceAndCount priceAndCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
getCartPresenter = new GetCartPresenter(this);
getCartPresenter.getCart();
mCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
elvAdapter.AllOrNone(mCb.isChecked());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
getCartPresenter.dettach();
}
@Override
public void show(List<GetCartBean.DataBean> group, List<List<GetCartBean.DataBean.ListBean>> child) {
elvAdapter = new ElvAdapter(this, group, child);
mElv.setGroupIndicator(null);
mElv.setAdapter(elvAdapter);
for (int i = 0; i < group.size(); i++) {
mElv.expandGroup(i);
}
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mCb = (CheckBox) findViewById(R.id.cb);
mTvTotal = (TextView) findViewById(R.id.tvTotal);
mTvCount = (TextView) findViewById(R.id.tvCount);
mTvCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到确认订单页面
Intent intent = new Intent(SecondActivity.this, ConfirmActivity.class);
if (priceAndCount != null) {
intent.putExtra("money", priceAndCount.getPrice() + "");
}
startActivity(intent);
}
});
}
public void setPriceAndCount(PriceAndCount priceAndCount) {
this.priceAndCount = priceAndCount;
mTvTotal.setText("合计:" + priceAndCount.getPrice());
mTvCount.setText("去结算(" + priceAndCount.getCount() + ")");
}
public void setAllChecked(boolean bool) {
mCb.setChecked(bool);
}
}
---------------然后ConfirmActivity这个页面
package com.example.peng.goodscard_1510d.view;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.peng.goodscard_1510d.R;
public class ConfirmActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTvLeft;
/**
* 立即下单
*/
private Button mBt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm);
//接收传过来实付款
Intent intent = getIntent();
String money = intent.getStringExtra("money");
initView();
mTvLeft.setText("实付款:¥" + money);
}
private void initView() {
mTvLeft = (TextView) findViewById(R.id.tvLeft);
mBt = (Button) findViewById(R.id.bt);
mBt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bt:
//跳转到订单页面
Intent intent = new Intent(ConfirmActivity.this, OrderActivity.class);
startActivity(intent);
break;
}
}
}
---------------activity_confirm布局文件
<?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.peng.goodscard_1510d.view.ConfirmActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#33000000">
<TextView
android:id="@+id/tvLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#ff0000"
android:text="立即下单" />
</RelativeLayout>
</LinearLayout>
-----------------------------接着OrderActivity页面
package com.example.peng.goodscard_1510d.view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.peng.goodscard_1510d.R;
import com.example.peng.goodscard_1510d.fragment.AllFragment;
import com.example.peng.goodscard_1510d.fragment.WaitFragment;
import java.util.ArrayList;
import java.util.List;
public class OrderActivity extends AppCompatActivity {
/**
* 全部
*/
private TextView mTvAll;
/**
* 待支付
*/
private TextView mTvWait;
private LinearLayout mLl;
private ViewPager mVp;
private List<Fragment> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
initView();
list.add(new AllFragment());
list.add(new WaitFragment());
mVp.setAdapter(new MyAdapter(getSupportFragmentManager()));
}
private void initView() {
mTvAll = (TextView) findViewById(R.id.tvAll);
mTvWait = (TextView) findViewById(R.id.tvWait);
mLl = (LinearLayout) findViewById(R.id.ll);
mVp = (ViewPager) findViewById(R.id.vp);
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
-------------------------activity_order布局文件
<?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"
android:orientation="vertical"
tools:context="com.example.peng.goodscard_1510d.view.OrderActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAll"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="全部" />
<TextView
android:id="@+id/tvWait"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="待支付" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
-----------all_fragment——订单列表中的ViewPage中的fragment,然后设置一个RecycleView
<?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:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
---------给RecycleView设置条目order_item
<?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="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="21dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="31dp"
android:layout_marginRight="31dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_below="@+id/textView"
android:layout_marginTop="27dp"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView3"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="24dp"
android:text="TextView" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignEnd="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:text="TextView" />
</RelativeLayout>
------------------RecycleView的适配器RvAllAdapter
package com.example.peng.goodscard_1510d.adapter;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.peng.goodscard_1510d.R;
import com.example.peng.goodscard_1510d.bean.OrderBean;
import java.util.List;
/**
* Created by peng on 2017/12/20.
*/
public class RvAllAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<OrderBean.DataBean> list;
public RvAllAdapter(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.order_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myViewHolder = (MyViewHolder) holder;
OrderBean.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"));
} else if (status == 1) {
myViewHolder.tvStatus.setText("已取消");
} else if (status == 2) {
myViewHolder.tvStatus.setText("已支付");
}
myViewHolder.tvPrice.setText("价格:" + dataBean.getPrice());
myViewHolder.tvPrice.setTextColor(Color.parseColor("#ff0000"));
myViewHolder.tvTime.setText(dataBean.getCreatetime());
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
private final TextView tvStatus;
private final TextView tvPrice;
private final TextView tvTime;
private final TextView tvBt;
public MyViewHolder(View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.textView);
tvStatus = itemView.findViewById(R.id.textView2);
tvPrice = itemView.findViewById(R.id.textView3);
tvTime = itemView.findViewById(R.id.textView4);
tvBt = itemView.findViewById(R.id.textView5);
}
}
}
-------------Fragment包~有两个类AllFragment,WaitFragment
AllFragment类(这里调用RvAllAdapter适配器)
package com.example.peng.goodscard_1510d.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.peng.goodscard_1510d.R;
import com.example.peng.goodscard_1510d.adapter.RvAllAdapter;
import com.example.peng.goodscard_1510d.bean.OrderBean;
import com.example.peng.goodscard_1510d.net.OkHttpUtils;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/12/20.
*/
public class AllFragment extends Fragment {
private RecyclerView rv;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all, null);
rv = view.findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
String url = "https://www.zhaoapi.cn/product/getOrders?uid=71";
OkHttpUtils.getOkHttpUtils().doGet(url, 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();
final OrderBean orderBean = new Gson().fromJson(string, OrderBean.class);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
RvAllAdapter adapter = new RvAllAdapter(getContext(), orderBean.getData());
rv.setAdapter(adapter);
}
});
}
});
return view;
}
}
WaitFragment先空着