购物车+确定订单+订单列表
【【【购物车的主布局】】】】
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:visibility="gone"
android:layout_centerVertical="true"
android:id="@+id/detail_image_back"
android:padding="5dp"
android:src="@drawable/leftjiantou"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="购物车" />
</RelativeLayout>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:layout_below="@+id/text_title"
android:layout_above="@+id/linear_bottom"
android:id="@+id/smart_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--二级列表登录的按钮-->
<com.dash.a1511n.view.custom.MyExpanableView
android:id="@+id/my_expanable_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</com.dash.a1511n.view.custom.MyExpanableView>
<ImageView
android:id="@+id/empty_cart_image"
android:visibility="gone"
android:src="@drawable/empty_cart"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/linear_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/cart_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="登录" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录后用于同步电脑和手机的商品" />
</LinearLayout>
<!--为你推荐-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F2F0F4"
android:gravity="center"
android:padding="10dp"
android:text="为你推荐" />
<android.support.v7.widget.RecyclerView
android:id="@+id/tui_jian_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<RelativeLayout
android:layout_above="@+id/linear_bottom"
android:layout_below="@+id/text_title"
android:id="@+id/relative_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_bottom"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:layout_marginLeft="10dp"
android:id="@+id/cart_check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="5dp"
android:text="全选"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:id="@+id/text_total"
android:text="合计:¥0.00"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="@+id/text_buy"
android:gravity="center"
android:background="#ff0000"
android:textColor="#ffffff"
android:text="去结算(0)"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
【【购物车的acvitity 的代码】】】
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dash.a1511n.R;
import com.dash.a1511n.model.bean.CartBean;
import com.dash.a1511n.model.bean.CountPriceBean;
import com.dash.a1511n.model.bean.FenLeiBean;
import com.dash.a1511n.model.bean.HomeBean;
import com.dash.a1511n.presenter.FragmentCartPresenter;
import com.dash.a1511n.presenter.FragmentHomeP;
import com.dash.a1511n.util.ApiUtil;
import com.dash.a1511n.util.ChenJinUtil;
import com.dash.a1511n.util.CommonUtils;
import com.dash.a1511n.view.Iview.FragmentCartInter;
import com.dash.a1511n.view.Iview.InterFragmentHome;
import com.dash.a1511n.view.Iview.OnItemListner;
import com.dash.a1511n.view.activity.DetailActivity;
import com.dash.a1511n.view.activity.LoginActivity;
import com.dash.a1511n.view.activity.MakeSureOrderActivity;
import com.dash.a1511n.view.adapter.MyExpanableAdapter;
import com.dash.a1511n.view.adapter.TuiJianAdapter;
import com.dash.a1511n.view.custom.MyExpanableView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Dash on 2018/1/23.
*/
public class FragmentShoppingCart extends Fragment implements InterFragmentHome,FragmentCartInter, View.OnClickListener {
private LinearLayout linear_login;
private Button cart_login;
private RecyclerView tui_jian_recycler;
private MyExpanableView my_expanable_view;
private FragmentHomeP fragmentHomeP;
private RelativeLayout relative_progress;
private FragmentCartPresenter fragmentCartPresenter;
private CheckBox cart_check_all;
private TextView text_total;
private TextView text_buy;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
CountPriceBean countPriceBean = (CountPriceBean) msg.obj;
//设置显示
text_total.setText("合计:¥"+countPriceBean.getPriceString());
text_buy.setText("去结算("+countPriceBean.getCount()+")");
}
}
};
private MyExpanableAdapter myExpanableAdapter;
private ImageView empty_cart_image;
private CartBean cartBean;
//创建一个集合,,,装的是去结算的时候 选中的所有商品的数据
private ArrayList<CartBean.DataBean.ListBean> list_selected = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frament_cart_layout,container,false);
linear_login = view.findViewById(R.id.linear_login);
cart_login = view.findViewById(R.id.cart_login);
tui_jian_recycler = view.findViewById(R.id.tui_jian_recycler);
my_expanable_view = view.findViewById(R.id.my_expanable_view);
relative_progress = view.findViewById(R.id.relative_progress);
cart_check_all = view.findViewById(R.id.cart_check_all);
text_total = view.findViewById(R.id.text_total);
text_buy = view.findViewById(R.id.text_buy);
empty_cart_image = view.findViewById(R.id.empty_cart_image);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//去掉默认的指示器
my_expanable_view.setGroupIndicator(null);
//失去焦点....界面不是从recyclerView开始显示
tui_jian_recycler.setFocusable(false);
//2.为你推荐,,,只需要获取一次
fragmentHomeP = new FragmentHomeP(this);
//调用p层里面的方法....https://www.zhaoapi.cn/ad/getAd
fragmentHomeP.getNetData(ApiUtil.HOME_URL);
fragmentCartPresenter = new FragmentCartPresenter(this);
//全选 设置点击事件
cart_check_all.setOnClickListener(this);
text_buy.setOnClickListener(this);
}
@Override
public void onResume() {
//Log.e("----","获取到焦点");
super.onResume();
initData();
//为你推荐的数据请求一次....其实为你推荐的数据也是随着登录和不登录改变
}
private void initData() {
ChenJinUtil.setStatusBarColor(getActivity(), Color.parseColor("#484648"));
//判断是否登录...没有,则显示登录按钮....已经登录显示购物车数据
if (CommonUtils.getBoolean("isLogin")) {
//请求购物车的数据...显示购物车
my_expanable_view.setVisibility(View.VISIBLE);
empty_cart_image.setVisibility(View.VISIBLE);
linear_login.setVisibility(View.GONE);
//请求购物车的数据
getCartData();
}else {
//登录按钮的显示
linear_login.setVisibility(View.VISIBLE);
my_expanable_view.setVisibility(View.GONE);
empty_cart_image.setVisibility(View.GONE);
//登录的点击事件
cart_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),LoginActivity.class);
startActivity(intent);
}
});
}
}
/**
* 获取购物车数据
*/
private void getCartData() {
//显示进度条
relative_progress.setVisibility(View.VISIBLE);
//查询购物车的数据
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
//Log.e("----","隐藏");
}else {
//Log.e("----","显示");
initData();
}
}
@Override
public void onSuccess(final HomeBean homeBean) {
//瀑布流
tui_jian_recycler.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
//为你推荐设置适配器
TuiJianAdapter tuiJianAdapter = new TuiJianAdapter(getActivity(), homeBean.getTuijian());
tui_jian_recycler.setAdapter(tuiJianAdapter);
//为你推荐的点击事件
tuiJianAdapter.setOnItemListner(new OnItemListner() {
@Override
public void onItemClick(int position) {
//跳转的是自己的详情页面
Intent intent = new Intent(getActivity(), DetailActivity.class);
//传递pid
intent.putExtra("pid",homeBean.getTuijian().getList().get(position).getPid());
startActivity(intent);
}
@Override
public void onItemLongClick(int position) {
}
});
}
@Override
public void onFenLeiDataSuccess(FenLeiBean fenLeiBean) {
}
@Override
public void getCartDataNull() {
relative_progress.setVisibility(View.GONE);
empty_cart_image.setVisibility(View.VISIBLE);
my_expanable_view.setVisibility(View.GONE);
Toast.makeText(getActivity(),"购物车为空,先去逛逛吧",Toast.LENGTH_SHORT).show();
}
@Override
public void getCartDataSuccess(final CartBean cartBean) {
this.cartBean = cartBean;
//进度条隐藏
relative_progress.setVisibility(View.GONE);
empty_cart_image.setVisibility(View.GONE);
my_expanable_view.setVisibility(View.VISIBLE);
//显示购物车的数据....二级列表设置适配器
//1.根据某一组里面所有子孩子是否选中,决定当前组是否选中
for (int i =0;i<cartBean.getData().size();i++) {
//遍历每一组的数据,,,设置是否选中...有所有的子孩子决定
CartBean.DataBean dataBean = cartBean.getData().get(i);
dataBean.setGroupChecked(isAllChildInGroupChecked(dataBean.getList()));
}
//2.根据所有的组是否选中,,,决定全选是否选中
cart_check_all.setChecked(isAllGroupChecked(cartBean));
//3.
myExpanableAdapter = new MyExpanableAdapter(getActivity(), cartBean,handler,relative_progress,fragmentCartPresenter);
my_expanable_view.setAdapter(myExpanableAdapter);
//展开所有的每一组
for (int i = 0;i<cartBean.getData().size();i++) {
my_expanable_view.expandGroup(i);
}
//4.计算商品的总价和数量
myExpanableAdapter.sendPriceAndCount();
//子条目的点击事件
my_expanable_view.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
//Toast.makeText(getActivity(),"点击了",Toast.LENGTH_SHORT).show();
//跳转的是自己的详情页面
Intent intent = new Intent(getActivity(), DetailActivity.class);
//传递pid
intent.putExtra("pid",cartBean.getData().get(groupPosition).getList().get(childPosition).getPid());
startActivity(intent);
return false;
}
});
}
/**
* 是否所有的组选中
* @param cartBean
* @return
*/
private boolean isAllGroupChecked(CartBean cartBean) {
for (int i=0;i<cartBean.getData().size();i++) {
//组没有选中
if (! cartBean.getData().get(i).isGroupChecked()) {
return false;
}
}
return true;
}
/**
* 判断组中所有的子孩子是否全部选中
* @param list
* @return
*/
private boolean isAllChildInGroupChecked(List<CartBean.DataBean.ListBean> list) {
for (int i =0;i<list.size();i++) {
if (list.get(i).getSelected() == 0) {
return false;
}
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cart_check_all://全选
//点击全选的时候,,,,根据全选的状态 改变购物车所有商品的选中状态
myExpanableAdapter.setAllChildChecked(cart_check_all.isChecked());
break;
case R.id.text_buy://去结算
//当显示的是去结算(0)....打击没有任何反应
if ("去结算(0)".equals(text_buy.getText().toString())) {
return;
}
//选中了商品之后,,,跳转到确认订单页面
Intent intent = new Intent(getActivity(), MakeSureOrderActivity.class);
//跳转传值...选中的商品的数据...传递一个集合过去...选中的商品的集合
//先清空一下集合
list_selected.clear();
for (int i = 0;i<cartBean.getData().size();i++) {
List<CartBean.DataBean.ListBean> list = cartBean.getData().get(i).getList();
for (int j = 0;j<list.size();j++) {
//判断是否选中,,,如果选中存放到集合中去
if (list.get(j).getSelected() == 1) {
list_selected.add(list.get(j));
}
}
}
//通过intent把集合传到下一个页面
intent.putExtra("list_selected",list_selected);
startActivity(intent);
break;
}
}
}
【购物车的条目的子布局】
【列表的一级列表】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/group_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/group_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
【列表的第二级列表】
<CheckBox
android:focusable="false"
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/child_check_box"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/child_check_box"
android:id="@+id/child_image"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:maxLines="2"
android:minLines="2"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/child_text_delete"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/child_image"
android:id="@+id/child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/child_price"
android:layout_marginBottom="5dp"
android:layout_alignBottom="@+id/child_image"
android:layout_toRightOf="@+id/child_image"
android:textColor="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/child_text_delete"
android:layout_alignBottom="@+id/child_image"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:background="@drawable/cart_bian_kuang"
android:id="@+id/child_text_jian"
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:background="@drawable/cart_bian_kuang"
android:id="@+id/child_text_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:padding="5dp"
android:background="@drawable/cart_bian_kuang"
android:id="@+id/child_text_add"
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:gravity="center"
android:background="#ff0000"
android:textColor="#ffffff"
android:id="@+id/child_text_delete"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/child_image"
android:layout_alignBottom="@id/child_image"
android:text="删除"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RelativeLayout>
【购物车的适配器 MyExpanableAdapter】
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.dash.a1511n.R;
import com.dash.a1511n.model.bean.CartBean;
import com.dash.a1511n.model.bean.CountPriceBean;
import com.dash.a1511n.presenter.FragmentCartPresenter;
import com.dash.a1511n.util.ApiUtil;
import com.dash.a1511n.util.CommonUtils;
import com.dash.a1511n.util.OkHttp3Util_03;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by Dash on 2018/1/31.
*/
public class MyExpanableAdapter extends BaseExpandableListAdapter{
private FragmentCartPresenter fragmentCartPresenter;
private RelativeLayout relative_progress;
private Handler handler;
private CartBean cartBean;
private Context context;
private DecimalFormat decimalFormat = new DecimalFormat("0.00");
private int childIndex;
private int allIndex;
public MyExpanableAdapter(Context context, CartBean cartBean, Handler handler, RelativeLayout relative_progress, FragmentCartPresenter fragmentCartPresenter) {
this.context = context;
this.cartBean = cartBean;
this.handler = handler;
this.relative_progress = relative_progress;
this.fragmentCartPresenter = fragmentCartPresenter;
}
@Override
public int getGroupCount() {
return cartBean.getData().size();
}
//一组里面有多少个子条目...cartBean.getData().get(groupPosition).getList(),,某一个组里面子条目的数据
@Override
public int getChildrenCount(int groupPosition) {
return cartBean.getData().get(groupPosition).getList().size();
}
//cartBean.getData().get(groupPosition)某个组的数据
@Override
public Object getGroup(int groupPosition) {
return cartBean.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return cartBean.getData().get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
final GroupHolder holder;
if (view == null) {
view = View.inflate(context, R.layout.cart_group_item_layout,null);
holder = new GroupHolder();
holder.group_check_box = view.findViewById(R.id.group_check_box);
holder.group_shop_name = view.findViewById(R.id.group_shop_name);
view.setTag(holder);
}else {
holder = (GroupHolder) view.getTag();
}
//赋值..checkBox设置是否选中...商家设置名字
final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);
holder.group_shop_name.setText(dataBean.getSellerName());
holder.group_check_box.setChecked(dataBean.isGroupChecked());
//设置点击事件
holder.group_check_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示progress
relative_progress.setVisibility(View.VISIBLE);
childIndex = 0;
//根据商家是否选中的状态,,,去更新所有子孩子的状态
updateChildInGroup(holder.group_check_box.isChecked(),dataBean);
}
});
return view;
}
/**
* 根据组的状态...根据所有里面的子孩子
* @param checked 表示组是否选中
* @param dataBean
*/
private void updateChildInGroup(final boolean checked, final CartBean.DataBean dataBean) {
CartBean.DataBean.ListBean listBean = dataBean.getList().get(childIndex);
//更新购物车
//更新购物车....跟新当前商品是否能选中的状态...selected值
Map<String, String> params = new HashMap<>();
//?uid=71&sellerid=1&pid=1&selected=0&num=10
params.put("uid", CommonUtils.getString("uid"));
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(checked ? 1:0));
params.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
childIndex ++;
if (childIndex < dataBean.getList().size()) {
//可以继续更新
updateChildInGroup(checked,dataBean);
}else {
//已经全部更新完成....重新查询购物车数据,显示
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
}
});
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ChildHolder holder;
if (view == null) {
view = View.inflate(context,R.layout.cart_child_item_layout,null);
holder = new ChildHolder();
holder.child_check_box = view.findViewById(R.id.child_check_box);
holder.child_image = view.findViewById(R.id.child_image);
holder.child_title = view.findViewById(R.id.child_title);
holder.child_text_add = view.findViewById(R.id.child_text_add);
holder.child_text_num = view.findViewById(R.id.child_text_num);
holder.child_text_jian = view.findViewById(R.id.child_text_jian);
holder.child_price = view.findViewById(R.id.child_price);
holder.child_text_delete = view.findViewById(R.id.child_text_delete);
view.setTag(holder);
}else {
holder = (ChildHolder) view.getTag();
}
//赋值
final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);
设置子条目是否选中...selected : 1,,0表示不选中,1表示选中
holder.child_check_box.setChecked(listBean.getSelected() == 1 ? true :false);
Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.child_image);
holder.child_title.setText(listBean.getTitle());
//一个double类型的值保留两位小数
holder.child_price.setText("¥"+decimalFormat.format(listBean.getBargainPrice()));
//注意:----text设置文本的时候一定要是string类型
holder.child_text_num.setText(String.valueOf(listBean.getNum()));
//checkBox点击事件
holder.child_check_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示progressBar
relative_progress.setVisibility(View.VISIBLE);
//更新购物车....跟新当前商品是否能选中的状态...selected值
Map<String, String> params = new HashMap<>();
//?uid=71&sellerid=1&pid=1&selected=0&num=10
params.put("uid", CommonUtils.getString("uid"));
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
//此时需要selected是否选中...传什么值过去?如果当前是1 传0,,如果是0,,传1
params.put("selected", String.valueOf(listBean.getSelected()==0 ? 1:0));
params.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
//更新成功之后....重新查询一下购物车的数据,然后进行展示
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
});
}
});
//加
holder.child_text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示progressBar
relative_progress.setVisibility(View.VISIBLE);
//更新购物车....跟新当前商品是否能选中的状态...selected值
Map<String, String> params = new HashMap<>();
//?uid=71&sellerid=1&pid=1&selected=0&num=10
params.put("uid", CommonUtils.getString("uid"));
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(listBean.getSelected()));
//num+1
params.put("num", String.valueOf(listBean.getNum() +1));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
//更新成功之后....重新查询一下购物车的数据,然后进行展示
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
});
}
});
//减
holder.child_text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
if (num == 1) {
return;
}
//显示progressBar
relative_progress.setVisibility(View.VISIBLE);
//更新购物车....跟新当前商品是否能选中的状态...selected值
Map<String, String> params = new HashMap<>();
//?uid=71&sellerid=1&pid=1&selected=0&num=10
params.put("uid", CommonUtils.getString("uid"));
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(listBean.getSelected()));
params.put("num", String.valueOf(num -1));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
//更新成功之后....重新查询一下购物车的数据,然后进行展示
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
});
}
});
//删除
holder.child_text_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示progress
relative_progress.setVisibility(View.VISIBLE);
//请求删除购物车的接口...删除成功之后 再次请求查询购物车
Map<String, String> params = new HashMap<>();
//?uid=72&pid=1
params.put("uid",CommonUtils.getString("uid"));
params.put("pid", String.valueOf(listBean.getPid()));
OkHttp3Util_03.doPost(ApiUtil.DELETE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
//查询购物车的数据
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
});
}
});
return view;
}
//如果返回false 子条目不可以点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 计算价格和数量....发送给fragment进行显示
*/
public void sendPriceAndCount() {
double price = 0;
int count = 0;
for (int i= 0;i<cartBean.getData().size();i++) {
//遍历每一组里面的子孩子
List<CartBean.DataBean.ListBean> list = cartBean.getData().get(i).getList();
for (int j = 0; j< list.size(); j++) {
//判断每一个子孩子是否选中,,,如果选中计算价格和数量
if (list.get(j).getSelected() == 1) {
price = price + list.get(j).getBargainPrice() * list.get(j).getNum();
count += list.get(j).getNum();
}
}
}
String priceString = decimalFormat.format(price);
CountPriceBean countPriceBean = new CountPriceBean(priceString, count);
//去显示价格和数量
Message msg = Message.obtain();
msg.what = 1;
msg.obj = countPriceBean;
handler.sendMessage(msg);
}
/**
* 点击全选的时候,,,,根据全选的状态 改变购物车所有商品的选中状态
* @param checked 全选的状态
*/
public void setAllChildChecked(boolean checked) {
//改变购物车所有商品的选中状态...要有这些所有的商品...创建一个结合装所有的商品
List<CartBean.DataBean.ListBean> allList = new ArrayList<>();
for (int i = 0;i<cartBean.getData().size();i++) {
List<CartBean.DataBean.ListBean> list = cartBean.getData().get(i).getList();
for (int j = 0; j< list.size(); j++) {
allList.add(list.get(j));
}
}
//progress
relative_progress.setVisibility(View.VISIBLE);
allIndex = 0;//从第一条开始 索引是0
//操作这个allList集合
updateAllChild(allList,checked);
}
/**
* 跟新所有的子条目
* @param allList
* @param checked
*/
private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {
//第一条开始
CartBean.DataBean.ListBean listBean = allList.get(allIndex);
//更新购物车....跟新当前商品是否能选中的状态...selected值
Map<String, String> params = new HashMap<>();
//?uid=71&sellerid=1&pid=1&selected=0&num=10
params.put("uid", CommonUtils.getString("uid"));
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(checked ? 1:0));
params.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
//成功之后索引+1
allIndex ++;
if (allIndex <allList.size()) {
//继续更新下一条
updateAllChild(allList,checked);
}else {
//代表全部更新完毕....请求查询购物车的数据
fragmentCartPresenter.getCartData(ApiUtil.SELECT_CART,CommonUtils.getString("uid"));
}
}
}
});
}
private class GroupHolder{
CheckBox group_check_box;
TextView group_shop_name;
}
private class ChildHolder{
CheckBox child_check_box;
ImageView child_image;
TextView child_title;
TextView child_price;
TextView child_text_jian;
TextView child_text_num;
TextView child_text_add;
TextView child_text_delete;
}
}
【确认订单 的布局 activity_make_sure_order】
<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.dash.a1511n.view.activity.MakeSureOrderActivity">
<!--标题-->
<RelativeLayout
android:id="@+id/detai_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/detail_image_back"
android:padding="5dp"
android:src="@drawable/leftjiantou"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_centerInParent="true"
android:text="确认订单"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<ScrollView
android:layout_below="@+id/detai_relative"
android:layout_above="@+id/linear_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--地址-->
<RelativeLayout
android:id="@+id/relative_addr_01"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_alignLeft="@+id/text_addr"
android:id="@+id/text_name"
android:text="收货人:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_phone"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_marginTop="10dp"
android:id="@+id/ding_wei_image"
android:layout_below="@+id/text_name"
android:src="@drawable/ding_wei"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_below="@+id/text_name"
android:layout_toRightOf="@+id/ding_wei_image"
android:id="@+id/text_addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<View
android:background="#f4f4f4"
android:layout_width="match_parent"
android:layout_height="10dp"/>
<!--列表展示商品 可以使用recyclerView 还可以使用自定义的listView-->
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:id="@+id/product_list_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
<!--底部 提交订单-->
<LinearLayout
android:id="@+id/linear_bottom"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/text_shi_fu_ku"
android:padding="10dp"
android:gravity="center_vertical|right"
android:textColor="#ff0000"
android:text="实付款:¥0.00"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text_submit_order"
android:background="#ff0000"
android:text="提交订单"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
【确认订单的acvitity】
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dash.a1511n.R;
import com.dash.a1511n.model.bean.CartBean;
import com.dash.a1511n.model.bean.CreateOrderBean;
import com.dash.a1511n.model.bean.DefaultAddrBean;
import com.dash.a1511n.model.bean.GetAllAddrBean;
import com.dash.a1511n.presenter.CreateOrderPresenter;
import com.dash.a1511n.presenter.GetDefaultAddrPresenter;
import com.dash.a1511n.util.ApiUtil;
import com.dash.a1511n.util.CommonUtils;
import com.dash.a1511n.util.OkHttp3Util_03;
import com.dash.a1511n.view.Iview.CreateOrderInter;
import com.dash.a1511n.view.Iview.DefaultAddrInter;
import com.dash.a1511n.view.adapter.SureOrderAdapter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MakeSureOrderActivity extends AppCompatActivity implements View.OnClickListener,CreateOrderInter,DefaultAddrInter {
private ArrayList<CartBean.DataBean.ListBean> list_selected;
private ImageView detail_image_back;
private RecyclerView product_list_recycler;
private TextView text_shi_fu_kuan;
private TextView text_submit_order;
private DecimalFormat decimalFormat = new DecimalFormat("0.00");
private double price;
private CreateOrderPresenter createOrderPresenter;
private int index;
private GetDefaultAddrPresenter getDefaultAddrPresenter;
private TextView text_name;
private TextView text_phone;
private TextView text_addr;
private RelativeLayout relative_addr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_sure_order);
//找到控件
detail_image_back = findViewById(R.id.detail_image_back);
product_list_recycler = findViewById(R.id.product_list_recycler);
text_shi_fu_kuan = findViewById(R.id.text_shi_fu_ku);
text_submit_order = findViewById(R.id.text_submit_order);
text_name = findViewById(R.id.text_name);
text_phone = findViewById(R.id.text_phone);
text_addr = findViewById(R.id.text_addr);
relative_addr = findViewById(R.id.relative_addr_01);
//获取选中的购物车数据的集合
list_selected = (ArrayList<CartBean.DataBean.ListBean>) getIntent().getSerializableExtra("list_selected");
//获取默认的地址...有默认地址则显示,,,没有则弹出一个dialog
getDefaultAddrPresenter = new GetDefaultAddrPresenter(this);
getDefaultAddrPresenter.getDefaultAddr(ApiUtil.GET_DEFAULT_ADDR_URL,CommonUtils.getString("uid"));
}
private void initData() {
createOrderPresenter = new CreateOrderPresenter(this);
//设置点击事件
detail_image_back.setOnClickListener(this);
text_submit_order.setOnClickListener(this);
relative_addr.setOnClickListener(this);
//布局管理器
product_list_recycler.setLayoutManager(new LinearLayoutManager(MakeSureOrderActivity.this));
//添加分割线
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(MakeSureOrderActivity.this, LinearLayout.VERTICAL);
dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.item_decoration_shape));
product_list_recycler.addItemDecoration(dividerItemDecoration);
//设置适配器
SureOrderAdapter sureOrderAdapter = new SureOrderAdapter(MakeSureOrderActivity.this, list_selected);
product_list_recycler.setAdapter(sureOrderAdapter);
price = 0;
//显示实付款...计算价格
for (int i = 0;i<list_selected.size(); i++) {
price += list_selected.get(i).getBargainPrice() * list_selected.get(i).getNum();
}
//格式化两位
String priceString = decimalFormat.format(price);
text_shi_fu_kuan.setText("实付款:¥"+priceString);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.detail_image_back://返回
finish();
break;
case R.id.text_submit_order://提交订单...生成订单
//实际上点击提交订单的操作是:1.生成这个订单2.调用支付宝/微信/网银进行付款(付款的操作在后边学)
//https://www.zhaoapi.cn/product/createOrder?uid=71&price=99.99
createOrderPresenter.createOrder(ApiUtil.CREATE_ORDER_URL, CommonUtils.getString("uid"),price);
break;
case R.id.relative_addr_01://跳转到选择收货地址页面...回传数据
Intent intent = new Intent(MakeSureOrderActivity.this,ChooseAddrActivity.class);
startActivityForResult(intent,2001);
break;
}
}
@Override
public void onCreateOrderSuccess(CreateOrderBean createOrderBean) {
if ("0".equals(createOrderBean.getCode())) {//创建订单成功...成功之后才能去付款
//无论付款成功/失败/取消 该订单都已经创建了,,,需要跳转到订单列表,,,并且购物车里面相关商品需要删除
//1.订单创建成功之后 删除购物车列表中对应的商品...使用递归删除选中的商品
index = 0;
deleteProductInCart(list_selected);
}else {
Toast.makeText(MakeSureOrderActivity.this,createOrderBean.getMsg(),Toast.LENGTH_SHORT).show();
}
}
/**
* 删除购物车
* @param list_selected
*/
private void deleteProductInCart(final ArrayList<CartBean.DataBean.ListBean> list_selected) {
CartBean.DataBean.ListBean listBean = list_selected.get(index);
//请求删除购物车的接口...删除成功之后 再次请求查询购物车
Map<String, String> params = new HashMap<>();
//?uid=72&pid=1
params.put("uid",CommonUtils.getString("uid"));
params.put("pid", String.valueOf(listBean.getPid()));
OkHttp3Util_03.doPost(ApiUtil.DELETE_CART_URL, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {//删除一条成功,,,index++
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
index ++;//判断是否继续删除.,..如果index<list.size() 继续,,,不是代表全部删完了
if (index < list_selected.size()) {
//继续
deleteProductInCart(list_selected);
}else {
//删除完成...//1.调支付的操作...//2.跳转到订单列表页面
Toast.makeText(MakeSureOrderActivity.this,"应该调用支付的操作,然后再跳转订单列表",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MakeSureOrderActivity.this,OrderListActivity.class);
startActivity(intent);
finish();
}
}
});
}
}
});
}
/**
* 回调的是有默认地址
* @param defaultAddrBean
*/
@Override
public void onGetDefaultAddrSuccess(DefaultAddrBean defaultAddrBean) {
//显示地址...设置适配器
text_name.setText("收货人: "+defaultAddrBean.getData().getName());
text_phone.setText(defaultAddrBean.getData().getMobile()+"");
text_addr.setText("收货地址: "+defaultAddrBean.getData().getAddr());
//有地址的时候进行设置适配器
initData();
}
/**
* 回调的是空地址
*/
@Override
public void onGetDefaultAddrEmpty() {
//弹出对话框...取消,,,finish/确定...添加新地址...没添加点击了返回,当前确认订单页面finish,,,如果添加了显示地址
AlertDialog.Builder builder = new AlertDialog.Builder(MakeSureOrderActivity.this);
builder.setTitle("提示")
.setMessage("您还没有默认的收货地址,请设置收货地址")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//结束确认订单显示
MakeSureOrderActivity.this.finish();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//确定...跳转添加新地址...如果没有保存地址,确认订单finish,,,
//如果保存了地址...数据传回来进行显示(带有回传值的跳转)
Intent intent = new Intent(MakeSureOrderActivity.this,AddNewAddrActivity.class);
startActivityForResult(intent,1001);
}
})
.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001 && resultCode == 1002) {
//如果没有数据...没有保存,,,finish当前订单页面
if (data == null) {
finish();
return;
}
//如果有数据...显示地址...设置适配器
text_name.setText("收货人: "+data.getStringExtra("name"));
text_phone.setText(data.getStringExtra("phone"));
text_addr.setText("收货地址: "+data.getStringExtra("addr"));
initData();
}
if (requestCode == 2001 && resultCode == 2002) {//选择新地址过来的
if (data == null) {
return;
}
GetAllAddrBean.DataBean dataBean = (GetAllAddrBean.DataBean) data.getSerializableExtra("addrBean");
text_name.setText("收货人: "+dataBean.getName());
text_phone.setText(String.valueOf(dataBean.getMobile()));
text_addr.setText("收货地址: "+dataBean.getAddr());
}
}
}
【确认订单的列表 适配器】
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.dash.a1511n.R;
import com.dash.a1511n.model.bean.CartBean;
import com.dash.a1511n.view.activity.MakeSureOrderActivity;
import com.dash.a1511n.view.hodler.SureOrderHolder;
import java.util.ArrayList;
/**
* Created by Dash on 2018/2/24.
*/
public class SureOrderAdapter extends RecyclerView.Adapter<SureOrderHolder>{
private ArrayList<CartBean.DataBean.ListBean> list_selected;
private Context context;
public SureOrderAdapter(Context context, ArrayList<CartBean.DataBean.ListBean> list_selected) {
this.context = context;
this.list_selected = list_selected;
}
@Override
public SureOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.sure_order_item_layout,null);
SureOrderHolder sureOrderHolder = new SureOrderHolder(view);
return sureOrderHolder;
}
@Override
public void onBindViewHolder(SureOrderHolder holder, int position) {
CartBean.DataBean.ListBean listBean = list_selected.get(position);
//赋值
Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.sure_item_image);
holder.sure_item_title.setText(listBean.getTitle());
holder.sure_item_price.setText("¥"+listBean.getBargainPrice());
holder.sure_item_num.setText("x"+listBean.getNum());
}
@Override
public int getItemCount() {
return list_selected.size();
}
}
【确认订单的列表 子布局】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/recycler_out_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:background="#ffffff"
android:id="@+id/recycler_innner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
【订单列表的布局】
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dash.a1511n.view.activity.OrderListActivity">
<RelativeLayout
android:id="@+id/detai_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/detail_image_back"
android:padding="5dp"
android:src="@drawable/leftjiantou"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_centerInParent="true"
android:text="我的订单"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/san_dian_pop"
android:padding="5dp"
android:layout_alignParentRight="true"
android:src="@drawable/sandian"
android:layout_width="40dp"
android:layout_height="40dp" />
</RelativeLayout>
<RadioGroup
android:id="@+id/radio_group"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<RadioButton
android:id="@+id/radio_01"
android:checked="true"
android:button="@null"
android:gravity="center"
android:textColor="@color/radio_text_color"
android:text="全部"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_02"
android:button="@null"
android:gravity="center"
android:textColor="@color/radio_text_color"
android:text="待支付"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_03"
android:button="@null"
android:gravity="center"
android:textColor="@color/radio_text_color"
android:text="已支付"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_04"
android:button="@null"
android:gravity="center"
android:textColor="@color/radio_text_color"
android:text="已取消"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</RadioGroup>
<FrameLayout
android:id="@+id/frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
【popupview的布局】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/pop_dai_pay"
android:text="待支付"
android:layout_width="80dp"
android:layout_height="50dp" />
<TextView
android:gravity="center"
android:id="@+id/pop_already_pay"
android:text="已支付"
android:layout_width="80dp"
android:layout_height="50dp" />
<TextView
android:gravity="center"
android:id="@+id/pop_cancel"
android:text="已取消"
android:layout_width="80dp"
android:layout_height="50dp" />
</LinearLayout>
【订单列表的acvitity的数据】
public class OrderListActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView detail_image_back;
private ImageView detail_share;
private RadioGroup radio_group;
private ImageView san_dian_pop;
private PopupWindow popupWindow;
private TextView pop_dai_pay;
private TextView pop_already_pay;
private TextView pop_cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_list);
//找到控件
detail_image_back = findViewById(R.id.detail_image_back);
radio_group = findViewById(R.id.radio_group);
san_dian_pop = findViewById(R.id.san_dian_pop);
detail_image_back.setOnClickListener(this);
san_dian_pop.setOnClickListener(this);
initPopUpWindown();
int flag = getIntent().getIntExtra("flag", -1);
//这个订单列表页面可以从提交订单跳过来,,,,还可以从我的那个fragment去跳转
//默认显示的是全部页面
if (flag == -1) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentAllOrder()).commit();
}else {
//如果从fragemnt跳转过来 需要展示自己的页面
if (flag == 1) {//待支付
radio_group.check(R.id.radio_02);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentDaiPayOrder()).commit();
}else if (flag == 2) {//已支付
radio_group.check(R.id.radio_03);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentAlreadyPayOrder()).commit();
}else if (flag == 3) {//已取消
radio_group.check(R.id.radio_04);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentCacelOrder()).commit();
}
}
radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
switch (id) {
case R.id.radio_01://全部
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentAllOrder()).commit();
break;
case R.id.radio_02://待支付
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentDaiPayOrder()).commit();
break;
case R.id.radio_03://已支付
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentAlreadyPayOrder()).commit();
break;
case R.id.radio_04://已取消
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentCacelOrder()).commit();
break;
}
}
});
}
private void initPopUpWindown() {
View view= View.inflate(OrderListActivity.this,R.layout.order_pop_layout,null);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//找到控件
pop_dai_pay = view.findViewById(R.id.pop_dai_pay);
pop_already_pay = view.findViewById(R.id.pop_already_pay);
pop_cancel = view.findViewById(R.id.pop_cancel);
pop_dai_pay.setOnClickListener(this);
pop_already_pay.setOnClickListener(this);
pop_cancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.detail_image_back:
finish();
break;
case R.id.san_dian_pop://弹出pop
//判断一下当前radioGroup选中了哪一个RadioButton...设置展示的背景颜色
int checkedRadioButtonId = radio_group.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
case R.id.radio_02://待支付
pop_dai_pay.setBackgroundColor(Color.BLUE);
pop_already_pay.setBackgroundColor(Color.WHITE);
pop_cancel.setBackgroundColor(Color.WHITE);
break;
case R.id.radio_03://已支付
pop_dai_pay.setBackgroundColor(Color.WHITE);
pop_already_pay.setBackgroundColor(Color.BLUE);
pop_cancel.setBackgroundColor(Color.WHITE);
break;
case R.id.radio_04://已取消
pop_dai_pay.setBackgroundColor(Color.WHITE);
pop_already_pay.setBackgroundColor(Color.WHITE);
pop_cancel.setBackgroundColor(Color.BLUE);
break;
}
popupWindow.showAsDropDown(san_dian_pop);
break;
case R.id.pop_dai_pay://待支付
radio_group.check(R.id.radio_02);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentDaiPayOrder()).commit();
popupWindow.dismiss();
break;
case R.id.pop_already_pay://已支付
radio_group.check(R.id.radio_03);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentAlreadyPayOrder()).commit();
popupWindow.dismiss();
break;
case R.id.pop_cancel://已取消
radio_group.check(R.id.radio_04);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentCacelOrder()).commit();
popupWindow.dismiss();
break;
}
}
}
【订单列表的适配器】
public class OrderListAdapter extends RecyclerView.Adapter<OrderListHolder>{
private List<OrderListBean.DataBean> listAll;
private Context context;
public OrderListAdapter(Context context, List<OrderListBean.DataBean> listAll) {
this.context = context;
this.listAll = listAll;
}
@Override
public OrderListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context,R.layout.order_item_layout,null);
OrderListHolder orderListHolder = new OrderListHolder(view);
return orderListHolder;
}
@Override
public void onBindViewHolder(final OrderListHolder holder, int position) {
final OrderListBean.DataBean dataBean = listAll.get(position);
holder.text_title.setText(dataBean.getTitle());
holder.text_price.setText("价格:"+dataBean.getPrice());
//0 待支付1 已支付2 已取消
int status = dataBean.getStatus();
if (status == 0) {
holder.text_flag.setText("待支付");
holder.order_button.setText("取消订单");
}else if (status == 1) {
holder.text_flag.setText("已支付");
holder.order_button.setText("查看订单");
}else if (status == 2) {
holder.text_flag.setText("已取消");
holder.order_button.setText("查看订单");
}
holder.text_time.setText("创建时间:"+dataBean.getCreatetime());
//点击事件
holder.order_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示的是 取消订单...取消成功后 显示查看订单,,,flag显示已取消
if ("取消订单".equals(holder.order_button.getText().toString())) {
//弹出对话框
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示")
.setMessage("确定取消订单吗?")
.setNegativeButton("否",null)
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//请求取消订单的接口,,,成功之后 文字显示改变
Map<String, String> params = new HashMap<>();
params.put("uid", CommonUtils.getString("uid"));
params.put("status", String.valueOf(2));
params.put("orderId", String.valueOf(dataBean.getOrderid()));
OkHttp3Util_03.doPost(ApiUtil.UPDATE_ORDER_URL, 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();
final UpdateOrderBean updateOrderBean = new Gson().fromJson(json,UpdateOrderBean.class);
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
if ("0".equals(updateOrderBean.getCode())) {
holder.text_flag.setText("已取消");
holder.order_button.setText("查看订单");
}
}
});
}
}
});
}
})
.show();
}else {
//如果显示查看订单...去查看订单的信息...吐司
Toast.makeText(context,"即将跳转查看订单",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return listAll.size();
}
}
【订单列表的子布局】
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_flag"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ff0000"
android:layout_below="@+id/text_title"
android:layout_marginTop="10dp"
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="@+id/text_price"
android:layout_marginTop="10dp"
android:id="@+id/text_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:id="@+id/order_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
【待支付的fragment】
public class FragmentDaiPayOrder extends Fragment implements FragmentOrderListInter {
private RecyclerView recyclerView;
private SmartRefreshLayout smartRefreshLayout;
private OrderListPresenter orderListPresenter;
//分页
private int page = 1;
//大集合
private List<OrderListBean.DataBean> listAll = new ArrayList<>();
private OrderListAdapter orderListAdapter;
private RelativeLayout relative_empty_order;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_all_layout,container,false);
recyclerView = view.findViewById(R.id.recycler_order);
smartRefreshLayout = view.findViewById(R.id.smart_refresh);
relative_empty_order = view.findViewById(R.id.relative_empty_order);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//获取订单列表的数据
orderListPresenter = new OrderListPresenter(this);
orderListPresenter.getOrderData(ApiUtil.ORDER_LIST_URL, CommonUtils.getString("uid"),page);
smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
smartRefreshLayout.finishRefresh(2000);
}
});
smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
page ++;
orderListPresenter.getOrderData(ApiUtil.ORDER_LIST_URL, CommonUtils.getString("uid"),page);
}
});
}
@Override
public void onOrderDataSuccess(OrderListBean orderListBean) {
if ("0".equals(orderListBean.getCode())) {
//添加到大集合...只添加待支付的数据
for (int i=0;i<orderListBean.getData().size();i++) {
if (orderListBean.getData().get(i).getStatus() == 0) {
listAll.add(orderListBean.getData().get(i));
}
}
if (listAll.size() == 0) {
relative_empty_order.setVisibility(View.VISIBLE);
smartRefreshLayout.setVisibility(View.GONE);
}else {
relative_empty_order.setVisibility(View.GONE);
smartRefreshLayout.setVisibility(View.VISIBLE);
}
//设置适配器
setAdapter();
}
}
/**
* 设置适配器
*/
private void setAdapter() {
if (orderListAdapter == null) {
orderListAdapter = new OrderListAdapter(getActivity(), listAll);
recyclerView.setAdapter(orderListAdapter);
}else {
orderListAdapter.notifyDataSetChanged();
}
smartRefreshLayout.finishLoadmore();
}
}
【已支付】
public class FragmentAlreadyPayOrder extends Fragment implements FragmentOrderListInter {
private RecyclerView recyclerView;
private SmartRefreshLayout smartRefreshLayout;
private OrderListPresenter orderListPresenter;
//分页
private int page = 1;
//大集合
private List<OrderListBean.DataBean> listAll = new ArrayList<>();
private OrderListAdapter orderListAdapter;
private RelativeLayout relative_empty_order;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_all_layout,container,false);
recyclerView = view.findViewById(R.id.recycler_order);
smartRefreshLayout = view.findViewById(R.id.smart_refresh);
relative_empty_order = view.findViewById(R.id.relative_empty_order);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//获取订单列表的数据
orderListPresenter = new OrderListPresenter(this);
orderListPresenter.getOrderData(ApiUtil.ORDER_LIST_URL, CommonUtils.getString("uid"),page);
}
@Override
public void onOrderDataSuccess(OrderListBean orderListBean) {
if ("0".equals(orderListBean.getCode())) {
//添加到大集合
for (int i=0;i<orderListBean.getData().size();i++) {
if (orderListBean.getData().get(i).getStatus() == 1) {
listAll.add(orderListBean.getData().get(i));
}
}
if (listAll.size() == 0) {
relative_empty_order.setVisibility(View.VISIBLE);
smartRefreshLayout.setVisibility(View.GONE);
}else {
relative_empty_order.setVisibility(View.GONE);
smartRefreshLayout.setVisibility(View.VISIBLE);
}
//设置适配器
setAdapter();
}
}
/**
* 设置适配器
*/
private void setAdapter() {
if (orderListAdapter == null) {
orderListAdapter = new OrderListAdapter(getActivity(), listAll);
recyclerView.setAdapter(orderListAdapter);
}else {
orderListAdapter.notifyDataSetChanged();
}
}
}
【已取消】
public class FragmentCacelOrder extends Fragment implements FragmentOrderListInter {
private RecyclerView recyclerView;
private SmartRefreshLayout smartRefreshLayout;
private OrderListPresenter orderListPresenter;
//分页
private int page = 1;
//大集合
private List<OrderListBean.DataBean> listAll = new ArrayList<>();
private OrderListAdapter orderListAdapter;
private RelativeLayout relative_empty_order;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_all_layout,container,false);
recyclerView = view.findViewById(R.id.recycler_order);
smartRefreshLayout = view.findViewById(R.id.smart_refresh);
relative_empty_order = view.findViewById(R.id.relative_empty_order);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//获取订单列表的数据
orderListPresenter = new OrderListPresenter(this);
orderListPresenter.getOrderData(ApiUtil.ORDER_LIST_URL, CommonUtils.getString("uid"),page);
}
@Override
public void onOrderDataSuccess(OrderListBean orderListBean) {
if ("0".equals(orderListBean.getCode())) {
//添加到大集合
for (int i=0;i<orderListBean.getData().size();i++) {
if (orderListBean.getData().get(i).getStatus() == 2) {
listAll.add(orderListBean.getData().get(i));
}
}
if (listAll.size() == 0) {
relative_empty_order.setVisibility(View.VISIBLE);
smartRefreshLayout.setVisibility(View.GONE);
}else {
relative_empty_order.setVisibility(View.GONE);
smartRefreshLayout.setVisibility(View.VISIBLE);
}
//设置适配器
setAdapter();
}
}
/**
* 设置适配器
*/
private void setAdapter() {
if (orderListAdapter == null) {
orderListAdapter = new OrderListAdapter(getActivity(), listAll);
recyclerView.setAdapter(orderListAdapter);
}else {
orderListAdapter.notifyDataSetChanged();
}
}
}
购物车+确定订单+订单列表相关推荐
- dj电商-数据表的设计-购物车表与订单表设计
购物车,商品数量 用户的购买数量不应该超过库存的数量 使用redis实现购物车的功能 原因 如果通过mysql查询数据库,浪费性能 通过redis来查,速度更快 订单 点击去结算,进入订单 >订 ...
- 订单管理之获取订单表表列表数据
获取订单表表列表数据 [订单数据是不能删除的,只能修改订单信息的状态] 接口分析 请求方式: GET /meiduo_admin/orders/?keyword=<搜索内容>&pa ...
- java美团购物车,仿美团详情页与购物车源码-订单页
项目整体图 首先是order.html 订单 (function () { var docEl = document.documentElement; function setRemUnit(){ / ...
- 订单生成列表html,订单列表_1.html
订单列表 $axure.utils.getTransparentGifPath = function() { return 'resources/images/transparent.gif'; ...
- 淘宝/天猫官方商品/订单订单API接口
淘宝/天猫官方商品/订单订单API接口 测试地址:点击注册 商品接口列表 产品管理包 Taobao.product.get 获取一个产品的信息 官方文档 Taobao.products.search ...
- 写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表
写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表 写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商 ...
- 黑马淘淘商城第十二天 购物车实现、订单确认页面展示
1. 课程计划 第十二天: 1.购物车实现 2.订单确认页面展示 2. 购物车的实现 2.1. 功能分析 1.购物车是一个独立的表现层工程. 2.添加购物车不要求登录.可以指定购买商品的数量. 3.展 ...
- day09-确认订单订单列表发布
确认订单 添加收货地址 利用 vant提供的 button组件来实现 页面布局 <view><view class="chooseAddressBox">& ...
- Bootstrap(实现搜索书籍页面购物车页面以及订单)
运用Bootstrap实现以下案例: 案例1:实现搜索书籍页面 <!DOCTYPE html> <html><head><meta charset=" ...
最新文章
- 分布式深度学习最佳入门(踩坑)指南
- 微信拦截网站怎么办 微信屏蔽网址如何正常打开
- 详解一个ThreadLocal 的谜题
- linux vscode配置spring boot开发环境
- 《电子基础与维修工具核心教程》——2.4 电阻的串并联
- 这就是多媒体开发 视频竟然比连续图片更省带宽
- 内向的人很难成为群体程序员吗?
- xampp配置虚拟主机
- hashCode()和equals()的若干问题
- PR视频剪辑软件教程
- 中望cad linux版本,中望CAD2021下载 中望CAD ZWCAD 2021 For Linux v5.0.1446 官方正式版(支持国产UOS系统) 下载-脚本之家...
- 保姆级详细介绍Navicat安装会出现的四种问题,详细给出解决办法
- 适合社会化制造的设计
- 线程的三种创建方式以及区别
- 微信公众平台 使用JS-SDK实现拍照上传功能
- 简练网软考知识点整理-项目风险应对策略
- java实现斜水印铺满整张图
- Laravel填充数据Seeder出现Target class [***] does not exist.
- 10.13NOIP模拟赛
- 中文常用停用词表(哈工大停用词表、百度停用词表