购物车订单实现

UTils工具类

APIService类

public interface APIService {
    @GET("product/getOrders?uid=71")
    Observable<OrderBean> order();
    @GET("product/updateOrder")
    Call<ResponseBody> update(@Query("uid") int uid,@Query("status") int status,@Query("orderId") int orderId);
}

MyInterceptor类

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();

        //&source=android这个是get带参数
        //?source=android 这个是只能get不带传参,还有就是post
        String url = request.url().toString()+"&source=android";

        Request request1 = request.newBuilder().url(url).build();

        Response response = chain.proceed(request1);
        return response;
    }
}

OkHttpUtils类

public class OkHttpUtils {


    private static OkHttpClient client = null ;


    public static OkHttpClient getInstance(){

        if(client == null){
            synchronized (OkHttpUtils.class){
                if(client == null){
                    client = new OkHttpClient.Builder()
                            .connectTimeout(20000, TimeUnit.SECONDS)
                            .writeTimeout(20000,TimeUnit.SECONDS)
                            .readTimeout(20000,TimeUnit.SECONDS)
                            //.addInterceptor(new MyInterceptor())
                            .build();
                }
            }
        }
        return client;
    }
}


RetrofitUtils类

public class RetrofitUtils {


    private static APIService service  = null ;


    public static APIService getInstance(){
        if(service == null){
            synchronized (RetrofitUtils.class){

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://www.zhaoapi.cn/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        //.client(OkHttpUtils.getInstance())
                        .build();
                service = retrofit.create(APIService.class);
            }
        }
        return service;
    }

}


view层接口

public interface IOrderView {
    void showOrder(List<OrderBean.DataBean> list);
}

model层方法

model接口

public interface IOrderModel {
    void showDate(Order order);
    interface Order{
        void complate(List<OrderBean.DataBean> list);
    }
}

model类

public class OrderModel implements IOrderModel {

    @Override
    public void showDate(final Order order) {
        RetrofitUtils.getInstance().order()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("=========", "onError: "+e.toString());
                    }

                    @Override
                    public void onNext(OrderBean orderBean) {
                        List<OrderBean.DataBean> list = orderBean.getData();
                        order.complate(list);
                    }
                });
    }
}

Presenter层方法

IPresenter接口

public interface IPreesenter<T> {
    void attach(T view);
    void deach();
}

OrderPresenter类

public class OrderPresenter implements IPreesenter<IOrderView>{
    IOrderModel model;
    SoftReference<IOrderView> softReference;

    public OrderPresenter(IOrderView view) {
        attach(view);
        model = new OrderModel();
    }
    public void DateOrder(){
        model.showDate(new IOrderModel.Order() {
            @Override
            public void complate(List<OrderBean.DataBean> list) {
                softReference.get().showOrder(list);
            }
        });
    }
    @Override
    public void attach(IOrderView view) {
        softReference = new SoftReference<IOrderView>(view);
    }

    @Override
    public void deach() {
        softReference.clear();
    }
}

适配器

MyAdapter适配器

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<OrderBean.DataBean> list;
    OrderPresenter presenter;


    public MyAdapter(Context context, List<OrderBean.DataBean> list,OrderPresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.qb_item,parent, false);;
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
        ((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
        ((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
        if (list.get(position).getStatus()==0){
            ((MyViewHolder) holder).mDaizhifuText.setText("待支付");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.RED);
            ((MyViewHolder) holder).mBtnText.setText("取消订单");
            ((MyViewHolder) holder).mBtnText.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) {
                            retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,2,list.get(position).getOrderid());
                            call.enqueue(new retrofit2.Callback<ResponseBody>() {
                                @Override
                                public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                    presenter.DateOrder();
                                    notifyDataSetChanged();
                            }

                                @Override
                                public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

                                }
                            });
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();

                }
            });

        }else if (list.get(position).getStatus()==1){
            ((MyViewHolder) holder).mDaizhifuText.setText("已支付");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
            ((MyViewHolder) holder).mBtnText.setText("查看订单");
        }else if (list.get(position).getStatus()==2){
            ((MyViewHolder) holder).mDaizhifuText.setText("已取消");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
            ((MyViewHolder) holder).mBtnText.setText("查看订单");
            ((MyViewHolder) holder).mBtnText.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) {
                            retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,0,list.get(position).getOrderid());
                            call.enqueue(new retrofit2.Callback<ResponseBody>() {
                                @Override
                                public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                    presenter.DateOrder();
                                    notifyDataSetChanged();
                                }

                                @Override
                                public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

                                }
                            });
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        if (list!=null){
            return list.size();
        }
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTitleText;
        TextView mDaizhifuText;
        TextView mPriceText;
        TextView mTameText;
        TextView mBtnText;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTitleText = (TextView) itemView.findViewById(R.id.text_title);
            mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
            mPriceText = (TextView) itemView.findViewById(R.id.text_price);
            mTameText = (TextView) itemView.findViewById(R.id.text_tame);
            mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
        }
    }
}

MyFrag适配器

public class MyFrag extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<OrderBean.DataBean> list;
    OrderPresenter presenter;
    public MyFrag(Context context, List<OrderBean.DataBean> list, OrderPresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.daizhifu,parent, false);;
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (list.get(position).getStatus()==0){
            ((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
            ((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
            ((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
            ((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
            ((MyViewHolder) holder).mDaizhifuText.setText("待支付");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.RED);
            ((MyViewHolder) holder).mBtnText.setText("取消订单");
            ((MyViewHolder) holder).mBtnText.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) {
                            retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,2,list.get(position).getOrderid());
                            call.enqueue(new retrofit2.Callback<ResponseBody>() {
                                @Override
                                public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                    presenter.DateOrder();
                                    notifyDataSetChanged();
                                }

                                @Override
                                public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

                                }
                            });
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (list!=null){
            return list.size();
        }
        return 0;
    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTitleText;
        TextView mDaizhifuText;
        TextView mPriceText;
        TextView mTameText;
        TextView mBtnText;
        LinearLayout ll;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTitleText = (TextView) itemView.findViewById(R.id.text_title);
            mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
            mPriceText = (TextView) itemView.findViewById(R.id.text_price);
            mTameText = (TextView) itemView.findViewById(R.id.text_tame);
            mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
            ll = itemView.findViewById(R.id.ll);
        }
    }
}

MyQx适配器

public class MyQx extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    Context context;
    List<OrderBean.DataBean> list;
    OrderPresenter presenter;
    public MyQx(Context context, List<OrderBean.DataBean> list,OrderPresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.qx_item,parent, false);;
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (list.get(position).getStatus()==2){
            ((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
            ((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
            ((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
            ((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
            ((MyViewHolder) holder).mDaizhifuText.setText("已取消");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
            ((MyViewHolder) holder).mBtnText.setText("查看订单");
            ((MyViewHolder) holder).mBtnText.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) {
                            retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,0,list.get(position).getOrderid());
                            call.enqueue(new retrofit2.Callback<ResponseBody>() {
                                @Override
                                public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                    presenter.DateOrder();
                                    notifyDataSetChanged();
                                }

                                @Override
                                public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

                                }
                            });
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        if (list != null){
            return list.size();
        }
        return 0;
    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTitleText;
        TextView mDaizhifuText;
        TextView mPriceText;
        TextView mTameText;
        TextView mBtnText;
        LinearLayout ll;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTitleText = (TextView) itemView.findViewById(R.id.text_title);
            mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
            mPriceText = (TextView) itemView.findViewById(R.id.text_price);
            mTameText = (TextView) itemView.findViewById(R.id.text_tame);
            mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
            ll = itemView.findViewById(R.id.ll);
        }
    }
}

MyZf适配器

public class MyZf extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    Context context;
    List<OrderBean.DataBean> list;
    private List<String> stringList;


    public MyZf(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.yizhifu,parent, false);;
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        stringList = new ArrayList<>();
        if (list.get(position).getStatus()==1){

//            stringList.add(list.get(position).getTitle());
//
//            System.out.println("****"+stringList.size());



            ((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
            ((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
            ((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
            ((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
            ((MyViewHolder) holder).mDaizhifuText.setText("已支付");
            ((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
            ((MyViewHolder) holder).mBtnText.setText("查看订单");
        }
    }

    @Override
    public int getItemCount() {
        if (list != null){
            return list.size();
        }
        return 0;
    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTitleText;
        TextView mDaizhifuText;
        TextView mPriceText;
        TextView mTameText;
        TextView mBtnText;
        LinearLayout ll;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTitleText = (TextView) itemView.findViewById(R.id.text_title);
            mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
            mPriceText = (TextView) itemView.findViewById(R.id.text_price);
            mTameText = (TextView) itemView.findViewById(R.id.text_tame);
            mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
            ll = itemView.findViewById(R.id.ll);
        }
    }
}

Fragment基类

public abstract class BaseFragment<T extends IPreesenter> extends Fragment {
    T presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        createpresenter();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected abstract void createpresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null){
            presenter.deach();
        }
    }
}

Fragment1类

public class Fragment_1 extends BaseFragment<OrderPresenter> implements IOrderView {
    private RecyclerView mRecy;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = View.inflate(getActivity(), R.layout.fragment_1, null);
        mRecy = (RecyclerView)view.findViewById(R.id.recy);
        presenter.DateOrder();
        return view;
    }

    @Override
    public void showOrder(List<OrderBean.DataBean> list) {
        LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
        mRecy.setLayoutManager(mgs);
        MyAdapter adapter = new MyAdapter(getActivity(),list,presenter);
        mRecy.setAdapter(adapter);
    }

    @Override
    protected void createpresenter() {
        presenter = new OrderPresenter(this);
    }
}

Fragment2类

public class Fragment_2 extends BaseFragment<OrderPresenter> implements IOrderView {
    RecyclerView recy;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = View.inflate(getActivity(), R.layout.fragment_2,null);
        recy = view.findViewById(R.id.recy);
        presenter.DateOrder();
        return view;
    }

    @Override
    protected void createpresenter() {
        presenter = new OrderPresenter(this);
    }

    @Override
    public void showOrder(List<OrderBean.DataBean> list) {
        LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
        recy.setLayoutManager(mgs);
        MyFrag adapter = new MyFrag(getActivity(),list,presenter);
        recy.setAdapter(adapter);
    }
}

Fragment3类

public class Fragment_3 extends BaseFragment<OrderPresenter> implements IOrderView {
    RecyclerView recyclerView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = View.inflate(getActivity(), R.layout.fragment_3,null);
        recyclerView = view.findViewById(R.id.re);
        presenter.DateOrder();
        return view;
    }

    @Override
    public void showOrder(List<OrderBean.DataBean> list) {
        LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mgs);
        MyQx adapter = new MyQx(getActivity(),list,presenter);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void createpresenter() {
        presenter = new OrderPresenter(this);
    }
}

Fragment4类

public class Fragment_4 extends BaseFragment<OrderPresenter> implements IOrderView {
    RecyclerView rv;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = View.inflate(getActivity(), R.layout.fragment_4,null);
        presenter.DateOrder();
        rv = view.findViewById(R.id.rv);
        return view;
    }

    @Override
    protected void createpresenter() {
        presenter = new OrderPresenter(this);
    }

    @Override
    public void showOrder(List<OrderBean.DataBean> list) {
        LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(mgs);
        MyZf adapter = new MyZf(getActivity(),list);
        rv.setAdapter(adapter);
    }
}

MainAtivity主方法类

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioGroup radioGroup;
    private RadioButton button_1;
    private RadioButton button_2;
    private RadioButton button_3;
    private RadioButton button_4;
    private Fragment_1 fragment_1;
    private Fragment_2 fragment_2;
    private Fragment_3 fragment_3;
    private Fragment_4 fragment_4;
    private List<Fragment> list;
    private FrameLayout frameLayout;
    private ImageView mBtnImage;
    private View item_popup;
    private PopupWindow popupWindow;
    private View view;
    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);
        initView();
    }

    private void initView() {
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        //找到四个按钮
        button_1 = (RadioButton) findViewById(R.id.button_1);
        button_2 = (RadioButton) findViewById(R.id.button_2);
        button_3 = (RadioButton) findViewById(R.id.button_3);
        button_4 = (RadioButton) findViewById(R.id.button_4);

        //创建Fragment对象及集合
        fragment_1 = new Fragment_1();
        fragment_2 = new Fragment_2();
        fragment_3 = new Fragment_3();
        fragment_4 = new Fragment_4();

        //将Fragment对象添加到list中
        list = new ArrayList<>();
        list.add(fragment_1);
        list.add(fragment_2);
        list.add(fragment_3);
        list.add(fragment_4);

        //设置RadioGroup开始时设置的按钮,设置第一个按钮为默认值
        radioGroup.check(R.id.button_1);


        //设置按钮点击监听
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);
        button_4.setOnClickListener(this);

        //初始时向容器中添加第一个Fragment对象
        addFragment(fragment_1);

        mBtnImage = (ImageView) findViewById(R.id.image_btn);
        mBtnImage.setOnClickListener(this);
        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);
    }

    @Override
    public void onClick(View v) {
        //我们根据参数的id区别不同按钮
        //不同按钮对应着不同的Fragment对象页面
        switch (v.getId()) {
            case R.id.button_1:
                addFragment(fragment_1);
                break;
            case R.id.button_2:
                addFragment(fragment_2);
                break;
            case R.id.button_3:
                addFragment(fragment_3);
                break;
            case R.id.button_4:
                addFragment(fragment_4);
                break;
            case R.id.image_btn:
                popupWindow.showAsDropDown(mBtnImage);
                break;
            case R.id.btn_daizhi:
                addFragment(fragment_2);
                popupWindow.dismiss();
                break;
            case R.id.btn_yizhi:
                addFragment(fragment_4);
                popupWindow.dismiss();
                break;
            case R.id.btn_yiqu:
                addFragment(fragment_3);
                popupWindow.dismiss();
                break;
            default:
                break;
        }

    }

    //向Activity中添加Fragment的方法
    public void addFragment(Fragment fragment) {

        //获得Fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        //使用管理器开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //使用事务替换Fragment容器中Fragment对象
        fragmentTransaction.replace(R.id.framelayout, fragment);
        //提交事务,否则事务不生效
        fragmentTransaction.commit();
    }
}

主页面布局

<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"
    tools:context="com.example.miaoxinrikao0116.MainActivity">
    <RelativeLayout
        android:id="@+id/rl"
        android:background="#fff"
        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:textSize="20sp"
            android:text="订单列表"/>
        <ImageView
            android:id="@+id/image_btn"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:clickable="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:background="@drawable/lv_icon"/>
    </RelativeLayout>


    <!--最下边为RadioGroup-->
    <RadioGroup
        android:layout_below="@+id/rl"
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--第一个RadioButton-->
        <RadioButton
            android:id="@+id/button_1"
            android:text="全部"
            android:button="@null"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第二个RadioButton-->
        <RadioButton
            android:id="@+id/button_2"
            android:text="待支付"
            android:button="@null"
            android:gravity="center"
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第三个RadioButton-->
        <RadioButton
            android:id="@+id/button_3"
            android:text="已取消"
            android:button="@null"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第四个RadioButton-->
        <RadioButton
            android:id="@+id/button_4"
            android:text="已支付"
            android:button="@null"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_below="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

待支付布局(daizhifu.xml)

   <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:id="@+id/ll">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/text_title"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:text="1111"/>
        <TextView
            android:id="@+id/text_daizhifu"
            android:layout_width="50dp"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:textColor="#ff0000"
            android:text="待支付"/>
    </LinearLayout>
    <TextView
        android:id="@+id/text_price"
        android:gravity="bottom"
        android:textColor="#ff0000"
        android:layout_marginLeft="20dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="1111"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/text_tame"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:text="1111"/>
        <TextView
            android:id="@+id/text_btn"
            android:layout_width="70dp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_height="30dp"
            android:text="取消订单"/>
    </LinearLayout>
    <TextView
        android:layout_marginTop="15dp"
        android:clickable="true"
        android:focusable="false"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#9999"/>
    </LinearLayout>
</LinearLayout>


item_popup.xmL布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="100dp"
        android:background="#ffffff"
        android:layout_height="wrap_content"

        android:orientation="vertical">
        <TextView
            android:id="@+id/btn_daizhi"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="0dp"
            android:textSize="16sp"
            android:padding="15dp"
            android:text="待支付"/>
        <TextView
            android:id="@+id/btn_yizhi"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="0dp"
            android:textSize="16sp"
            android:padding="15dp"
            android:text="已支付"/>
        <TextView
            android:id="@+id/btn_yiqu"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="0dp"
            android:textSize="16sp"
            android:padding="15dp"
            android:text="已取消"/>
    </LinearLayout>
</RelativeLayout>

qb_item布局

<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/text_title"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:text="1111"/>
        <TextView
            android:id="@+id/text_daizhifu"
            android:layout_width="50dp"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:textColor="#ff0000"
            android:text="待支付"/>
    </LinearLayout>
    <TextView
        android:id="@+id/text_price"
        android:gravity="bottom"
        android:textColor="#ff0000"
        android:layout_marginLeft="20dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="1111"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/text_tame"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="bottom"
            android:layout_height="match_parent"
            android:text="1111"/>
        <TextView
            android:id="@+id/text_btn"
            android:layout_width="70dp"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:layout_height="30dp"
            android:text="取消订单"/>
    </LinearLayout>
    <TextView
        android:layout_marginTop="15dp"
        android:clickable="true"
        android:focusable="false"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#9999"/>

</LinearLayout>

qx_item布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:id="@+id/ll">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text_title"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:text="1111"/>
            <TextView
                android:id="@+id/text_daizhifu"
                android:layout_width="50dp"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:textColor="#ff0000"
                android:text="待支付"/>
        </LinearLayout>
        <TextView
            android:id="@+id/text_price"
            android:gravity="bottom"
            android:textColor="#ff0000"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="1111"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text_tame"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:text="1111"/>
            <TextView
                android:id="@+id/text_btn"
                android:layout_width="70dp"
                android:gravity="center"
                android:layout_gravity="bottom"
                android:layout_height="30dp"
                android:text="取消订单"/>
        </LinearLayout>
        <TextView
            android:layout_marginTop="15dp"
            android:clickable="true"
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#9999"/>
    </LinearLayout>

</LinearLayout>

yizhifu.xml布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:visibility="visible"

    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text_title"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:text="1111"/>
            <TextView
                android:id="@+id/text_daizhifu"
                android:layout_width="50dp"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:textColor="#ff0000"
                android:text="待支付"/>
        </LinearLayout>
        <TextView
            android:id="@+id/text_price"
            android:gravity="bottom"
            android:textColor="#ff0000"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="1111"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text_tame"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="bottom"
                android:layout_height="match_parent"
                android:text="1111"/>
            <TextView
                android:id="@+id/text_btn"
                android:layout_width="70dp"
                android:gravity="center"
                android:layout_gravity="bottom"
                android:layout_height="30dp"
                android:text="取消订单"/>
        </LinearLayout>
        <TextView
            android:layout_marginTop="15dp"
            android:clickable="true"
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#9999"/>
    </LinearLayout>
</LinearLayout>

以下是一个简单的Redis购物车订单实现代码示例: ```python import redis import json class ShoppingCart: def __init__(self, user_id): self.redis = redis.Redis(host='localhost', port=6379, db=0) self.user_id = user_id def add_item(self, item_id, quantity): cart_key = f"cart:{self.user_id}" item_key = f"item:{item_id}" pipeline = self.redis.pipeline() pipeline.hincrby(cart_key, item_key, quantity) pipeline.expire(cart_key, 86400) # 设置过期时间为1天 pipeline.execute() def remove_item(self, item_id, quantity): cart_key = f"cart:{self.user_id}" item_key = f"item:{item_id}" pipeline = self.redis.pipeline() pipeline.hincrby(cart_key, item_key, -quantity) pipeline.hdel(cart_key, item_key) if pipeline.hget(cart_key, item_key) == 0 else None pipeline.execute() def get_items(self): cart_key = f"cart:{self.user_id}" cart = self.redis.hgetall(cart_key) items = [] for item_key, quantity in cart.items(): item_id = item_key.decode('utf-8').split(':')[1] item = json.loads(self.redis.get(item_key)) items.append({"id": item_id, "name": item['name'], "price": item['price'], "quantity": int(quantity)}) return items def checkout(self): cart_key = f"cart:{self.user_id}" items = self.get_items() if not items: return pipeline = self.redis.pipeline() order_id = self.redis.incr("order:id") order_key = f"order:{order_id}" total_price = 0 for item in items: total_price += item['price'] * item['quantity'] pipeline.hset(order_key, item['id'], item['quantity']) pipeline.hset(order_key, "total_price", total_price) pipeline.expire(order_key, 86400) pipeline.delete(cart_key) pipeline.execute() ``` 这个简单的购物车实现使用Redis的哈希表来存储购物车订单信息,使用Redis的管道来执行多个操作以提高效率。它包含四个主要函数:添加商品到购物车,从购物车中删除商品,获取购物车中的所有商品以及结账并创建订单购物车订单的键都是由用户ID和其他标识符组成的。在添加和删除商品时,我们使用Redis的`HINCRBY`命令来增加或减少购物车中商品的数量。在获取商品时,我们遍历购物车哈希表中的所有元素,并检索每个元素对应的商品信息。在结账时,我们创建一个新的订单哈希表,并将购物车中的所有商品信息添加到订单中。最后,我们删除购物车并将订单哈希表设置为24小时过期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值