在做一系列网络请求之前别忘了添加 compile 'com.squareup.okhttp3:okhttp:3.3.1' ---okhttp3依赖,再加上compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' -- 拦截器来封装我们需要的公共参数--source=android,好了说的够清楚了,下面看看基本的实现吧!
mvp实现M层
public class ShoppingCarModel implements IShoppingCarModel{
private android.os.Handler handler = new android.os.Handler();
private OkHttp3Utils okHttp3Utils;
@Override
public void Getcarinfo(String uid, final IShoppingCarPerstenter iShoppingCarPerstenter) {
String url = "https://www.zhaoapi.cn/product/getCarts?uid="+uid+"&source=android";
okHttp3Utils = new OkHttp3Utils();
okHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
iShoppingCarPerstenter.onFraide(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String result = response.body().string();
System.out.println("结果:"+result);
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
ShoppingCarBean shoppingCarBean = gson.fromJson(result, ShoppingCarBean.class);
iShoppingCarPerstenter.onSuccess(shoppingCarBean.getData());
}
});
}
});
}
}
public interface IShoppingCarModel {
void Getcarinfo(String uid, IShoppingCarPerstenter iShoppingCarPerstenter);
}
实现P层
public class ShoppingCarPerstenter implements IShoppingCarPerstenter{
private IShoppingCarActrivcyView iShoppingView;
private IShoppingCarModel iShoppingCarModel;
public ShoppingCarPerstenter(IShoppingCarActrivcyView iShoppingView) {
this.iShoppingView = iShoppingView;
iShoppingCarModel = new ShoppingCarModel();
}
@Override
public void onFraide(String msg) {
if(iShoppingView!=null){
iShoppingView.onFraide(msg);
}
}
@Override
public void onSuccess(List<ShoppingCarBean.DataBean> data) {
if(iShoppingView!=null){
iShoppingView.onSuccess(data);
}
}
@Override
public void Getcarinfo(String uid) {
iShoppingCarModel.Getcarinfo(uid,this);
}
@Override
public void onDestory() {
if(iShoppingView==null){
iShoppingView=null;
}
}
}
public interface IShoppingCarPerstenter {
void onFraide(String msg);
void onSuccess(List<ShoppingCarBean.DataBean> data);
void Getcarinfo(String uid);
void onDestory();
}
实现V层
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shoppingcar,container,false);
//找到资源id
elv = view.findViewById(R.id.elv);
//添加适配器
adapter = new MyAdapterShoppingCar(list,getActivity());
elv.setAdapter(adapter);
iShoppingCarPerstenter = new ShoppingCarPerstenter(this);
//添加访问的参数
iShoppingCarPerstenter.Getcarinfo("10698");
return view;
}
//默认展开
private void defaultExpand(){
for (int i = 0; i < adapter.getGroupCount();++i){
elv.expandGroup(i);
}
}
@Override
public void onFraide(String msg) {
Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(List<ShoppingCarBean.DataBean> data) {
this.list = data;
adapter.setList(list);
defaultExpand();
}
public interface IShoppingCarActrivcyView {
void onFraide(String msg);
void onSuccess(List<ShoppingCarBean.DataBean> data);
}
二级列表的适配器类
public class MyAdapterShoppingCar extends BaseExpandableListAdapter{
private List<ShoppingCarBean.DataBean> list;
private Context context;
public void setList(List<ShoppingCarBean.DataBean> data) {
this.list = data;
notifyDataSetChanged();
}
public MyAdapterShoppingCar(List<ShoppingCarBean.DataBean> list, Context context) {
this.context = context;
}
@Override
public int getGroupCount() {
return list != null ? list.size() : 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return list != null && list.get(groupPosition).getList() != null ? list.get(groupPosition).getList().size() : 0;
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.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 false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.group,parent,false);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.childen,parent,false);
}
//商品选择
CheckBox ck_child_choosed = convertView.findViewById(R.id.ck_child_choose);
//商品图片
ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic);
//商品主标题
TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name);
//商品副标题
TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr);
//商品价格
TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price);
//商品数量
TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num);
//商品减
TextView iv_sub = convertView.findViewById(R.id.iv_sub);
//商品加减中的数量变化
final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num);
//商品加
TextView iv_add = convertView.findViewById(R.id.iv_add);
//删除按钮
Button btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete);
//设置文本信息
tv_commodity_name.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
tv_commodity_attr.setText(list.get(groupPosition).getList().get(childPosition).getSubhead());
tv_commodity_price.setText("¥"+list.get(groupPosition).getList().get(childPosition).getPrice());
tv_commodity_num.setText("x"+list.get(groupPosition).getList().get(childPosition).getNum());
tv_commodity_show_num.setText(list.get(groupPosition).getList().get(childPosition).getNum()+"");
String images = list.get(groupPosition).getList().get(childPosition).getImages();
String[] urls = images.split("\\|");
//加载商品图片
Glide.with(context)
.load(urls[0])
.crossFade()
.into(iv_show_pic);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
布局
头部
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@color/colorAccent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_weight="1"
android:textColor="#fff"
android:gravity="center"
android:textSize="20sp"
android:text="返回"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#fff"
android:textSize="20sp"
android:layout_weight="1"
android:gravity="center"
android:text="购物车"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#fff"
android:textSize="20sp"
android:layout_weight="1"
android:gravity="center"
android:text="编辑"
/>
</LinearLayout>
底部
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="全选"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="合计:"
android:layout_marginLeft="10dp"
/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="结算"
android:textSize="20sp"
android:textColor="#fff"
android:layout_marginLeft="106dp"
android:background="#e90c0c"
/>
</LinearLayout>
子布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/ck_child_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleX="0.6"
android:scaleY="0.6" />
<ImageView
android:id="@+id/iv_show_pic"
android:layout_width="70dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="@mipmap/ic_launcher"
android:layout_toRightOf="@id/ck_child_choose" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/iv_show_pic"
android:orientation="vertical">
<TextView
android:id="@+id/tv_commodity_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="酒红色纯红色纯羊毛西服套装"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_commodity_attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="属性:粉蓝色"
android:textSize="12sp"
android:textColor="@color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_commodity_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥390"
android:textColor="@android:color/holo_red_dark"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_commodity_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="x1"
android:textColor="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/rl_edit"
android:layout_width="120dp"
android:background="@android:color/holo_orange_light"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
>
<TextView
android:id="@+id/iv_sub"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text=" - " />
<TextView
android:id="@+id/tv_commodity_show_num"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text="1"
/>
<TextView
android:id="@+id/iv_add"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="1dp"
android:background="@android:color/white"
android:layout_height="match_parent"
android:text=" + " />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_commodity_delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="x"
android:background="@android:color/holo_blue_light"
android:textSize="20sp"
android:textColor="@android:color/holo_green_dark"
android:layout_margin="5dp"
android:visibility="gone" />
</RelativeLayout>
父布局
<CheckBox
android:id="@+id/ck_group_choosed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商家1"
android:gravity="center_vertical"
android:textSize="25sp"
android:focusable="false"
android:padding="10dp"/>