mvp简单购物车

item01

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="30dp"
            android:textColor="#000000"
            android:text="商品分类的标题"
            android:layout_marginLeft="20dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

    </LinearLayout>

</LinearLayout>

item02

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            />

        <ImageView
            android:id="@+id/imag_id"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:src="@mipmap/ic_launcher_round"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">


            <TextView
                android:id="@+id/name_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋"/>
            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="20dp"
                android:textSize="20sp"
                android:text="20"/>

        </LinearLayout>




</LinearLayout>

myadapter

package com.example.moth02.adpter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;


import com.example.moth02.R;
import com.example.moth02.bean.UserBean;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyView> {
    Context context;
    List<UserBean.DataEntity> list;
    private CheckBox checkbox;
    public MyAdapter(Context context, List<UserBean.DataEntity> list) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<UserBean.DataEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyAdapter.MyView onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.item01,null);
        MyView myView = new MyView(view);
        return myView;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyView myView,final int i) {


        myView.text1.setText(list.get(i).getName());

        final MyAdapters myAdapters = new MyAdapters(context,list,i);
        myView.rv1.setLayoutManager(new LinearLayoutManager(context));
        myView.rv1.setAdapter(myAdapters);
        myAdapters.setList(list);

        //选中
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    list.get(i).setCreated_at(1+"");
                    notifyDataSetChanged();
                }else{
                    list.get(i).setCreated_at(0+"");
                    notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyView extends RecyclerView.ViewHolder{


        private final TextView text1;
        private final RecyclerView rv1;


        public MyView(@NonNull View itemView) {
            super(itemView);

            checkbox = itemView.findViewById(R.id.checkbox);
            text1 = itemView.findViewById(R.id.text1);
            rv1 = itemView.findViewById(R.id.rv1);


        }
    }


}

myadapters

package com.example.moth02.adpter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.moth02.R;
import com.example.moth02.bean.UserBean;

import java.util.List;

public class MyAdapters extends RecyclerView.Adapter<MyAdapters.MyViews>{

    Context context;
    List<UserBean.DataEntity> list;
    int a;


    public MyAdapters(Context context, List<UserBean.DataEntity> list, int a) {
        this.context = context;
        this.list = list;
        this.a = a;
    }

    public void setList(List<UserBean.DataEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyAdapters.MyViews onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.item02,null);
        MyViews myViews = new MyViews(view);
        return myViews;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapters.MyViews myViews, int i) {
        myViews.name.setText(list.get(a).getSpus().get(i).getName());
        myViews.price.setText(list.get(a).getSpus().get(i).getPraise_num()+"");
        Glide.with(context).load(list.get(a).getSpus().get(i).getPic_url()).into(myViews.image);

        String at = list.get(a).getCreated_at();
        if(at.equals("1")){
            myViews.checkboxs.setChecked(true);
        }else if(at.equals("0")){
            myViews.checkboxs.setChecked(false);
        }

    }

    @Override
    public int getItemCount() {
        return list.get(a).getSpus().size();
    }




    public class MyViews extends RecyclerView.ViewHolder{


        private final CheckBox checkboxs;
        private final ImageView image;
        private final TextView name;
        private final TextView price;

        public MyViews(@NonNull View itemView) {
            super(itemView);

            checkboxs = itemView.findViewById(R.id.checkbox_id);
            image = itemView.findViewById(R.id.imag_id);
            name = itemView.findViewById(R.id.name_id);
            price = itemView.findViewById(R.id.price);

        }
    }
}

契约类

package com.example.moth02.constrat;

import com.example.moth02.http.OkHttpCallBack;

import java.util.HashMap;

public interface Constrets {
    //v
    interface MainView{
        void Success(String result);
        void Error(String msg);
    }

    //m
    interface MainModel{
        void getModels(HashMap<String,String>params, OkHttpCallBack okHttpCallBack);
    }

    //p
    public abstract class ShopPresenter{
        public abstract void getShopP(HashMap<String,String> params);
    }
}

http接口

package com.example.moth02.http;

public interface OkHttpCallBack {
    void Success(String result);
    void Failure(String msg);
}

okhttp

package com.example.moth02.http;

import android.os.Handler;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtil {

    private static OkHttpUtil Instance = null;
    static Handler handler = new Handler();

    private static OkHttpUtil getInstance(){
        if(Instance == null){
            synchronized (OkHttpUtil.class){
                if(Instance == null){
                    Instance = new OkHttpUtil();
                }
            }
        }
        return Instance;
    }

    public static void doGet(String url,final OkHttpCallBack okHttpCallBack){
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code()==200){
                    final String string = response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            okHttpCallBack.Success(string);
                        }
                    });
                }
            }
        });
    }

}

model

package com.example.moth02.model;

import com.example.moth02.api.API;
import com.example.moth02.constrat.Constrets;
import com.example.moth02.http.OkHttpCallBack;
import com.example.moth02.http.OkHttpUtil;

import java.util.HashMap;

public class ShopModel implements Constrets.MainModel {
    @Override
    public void getModels(HashMap<String, String> params, final OkHttpCallBack okHttpCallBack) {
        OkHttpUtil.doGet(API.url, new OkHttpCallBack() {
            @Override
            public void Success(String result) {
                okHttpCallBack.Success(result);
            }

            @Override
            public void Failure(String msg) {
                okHttpCallBack.Failure(msg);
            }
        });
    }
}

presenter

package com.example.moth02.presenter;

import com.example.moth02.constrat.Constrets;
import com.example.moth02.http.OkHttpCallBack;
import com.example.moth02.model.ShopModel;

import java.util.HashMap;

public class ShopPresenter extends Constrets.ShopPresenter {

    private final Constrets.MainView mainView;
    private final ShopModel shopModel;

    public ShopPresenter(Constrets.MainView mainView) {
        this.mainView = mainView;
        shopModel = new ShopModel();
    }

    @Override
    public void getShopP(HashMap<String, String> params) {
        shopModel.getModels(params, new OkHttpCallBack() {
            @Override
            public void Success(String result) {
                mainView.Success(result);
            }

            @Override
            public void Failure(String msg) {
                mainView.Error(msg);
            }
        });
    }
}

mainActivity

package com.example.moth02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.moth02.adpter.MyAdapter;
import com.example.moth02.bean.UserBean;
import com.example.moth02.constrat.Constrets;
import com.example.moth02.presenter.ShopPresenter;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity implements Constrets.MainView {

    private RecyclerView rv;
    private CheckBox check;
    private Button jie;
    private ShopPresenter shopPresenter;
    List<UserBean.DataEntity> list;
    List<UserBean.DataEntity.SpusEntity> lists;
    private MyAdapter myAdapter;
    private UserBean userBean;
    private TextView sums;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = findViewById(R.id.rv);
        check = findViewById(R.id.check);
        sums = findViewById(R.id.sums);
        jie = findViewById(R.id.jie);

        shopPresenter = new ShopPresenter(this);
        shopPresenter.getShopP(new HashMap<String, String>());

        rv.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MyAdapter(this,list);

    }



    @Override
    public void Success(String result) {
        Gson gson = new Gson();
        userBean = gson.fromJson(result, UserBean.class);
        myAdapter.setList(userBean.getData());
        rv.setAdapter(myAdapter);

    }

    @Override
    public void Error(String msg) {

    }
}

layput_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>

        <TextView
            android:id="@+id/sums"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="总价:"
            />

        <Button
            android:id="@+id/jie"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="去结算"/>

    </LinearLayout>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_42634876/article/details/88318756
今日推荐