使用MVP模式 实现购物车

该博客详细介绍了如何运用MVP模式来构建购物车功能。内容包括所需的依赖如HttpUtils用于GET请求,OnNetListener接口进行回调,以及Api接口、MainModel、MainPresenter、MainActivity、MyAdapter等组件的设计和交互。同时提到了eventBus中的MessageEvent和priceAndCountEvent事件,以及GoosBean数据模型。布局文件activity_main.xml和itm_child_market.xml、item_parent_market.xml也有所涉及。
摘要由CSDN通过智能技术生成

所需要的依赖

1
2
3
4
5
6
7
8
compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'org.greenrobot:eventbus:3.1.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

net下HttpUtils  GET请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class HttpUtils {
     private static volatile HttpUtils httpUtils;
     private final OkHttpClient client;
 
     private HttpUtils(){
         HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
         loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
         client = new OkHttpClient.Builder()
                 .addInterceptor(loggingInterceptor)
                 .build();
     }
     public static HttpUtils getHttpUtils(){
         if(httpUtils==null){
             synchronized (HttpUtils.class){
                 if (httpUtils==null){
                     httpUtils = new HttpUtils();
                 }
             }
         }
         return httpUtils;
     }
     /**
      * GET请求
      *
      * @param url
      * @param callback
      */
     public void doGet(String url, Callback callback){
         Request request = new Request.Builder().url(url).build();
         client.newCall(request).enqueue(callback);
     }
}

  OnNetListener接口回调

1
2
3
4
public interface OnNetListener< T > {
     public void onSuccess(T t);
     public void onFailure(Exception e);
}

  Api

1
2
3
public interface Api {
     public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
}

  model层  MainModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class MainModel implements IMainModel{
     private Handler handler = new Handler(Looper.getMainLooper());
 
     public void getGoods(final OnNetListener< GoosBean > onNetListener){
         HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
 
             }
 
             @Override
             public void onResponse(Call call, Response response) throws IOException {
                 String string = response.body().string();
                 final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {
                         onNetListener.onSuccess(goosBean);
                     }
                 });
             }
         });
     }
}

  IMainModel

1
2
3
public interface IMainModel {
     public void getGoods(OnNetListener< GoosBean > onNetListener);
}

  presenter层  MainPresenter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MainPresenter {
     private final IMainModel iMainModel;
     private final IMainActivity iMainActivity;
     public MainPresenter(IMainActivity iMainActivity) {
         this.iMainActivity = iMainActivity;
         iMainModel = new MainModel() ;
     }
 
     public void getGoods(){
 
         iMainModel.getGoods(new OnNetListener< GoosBean >() {
             @Override
             public void onSuccess(GoosBean goosBean) {
                 //List< GoosBean.DataBean > groupList, List< List <GoosBean.DataBean.DatasBean>> childList
                 List< GoosBean.DataBean > dataBean = goosBean.getData();
                 List< List <GoosBean.DataBean.DatasBean>> childList = new ArrayList< List <GoosBean.DataBean.DatasBean>>();
                 for (int i = 0; i <  dataBean.size (); i++) {
                     List<GoosBean.DataBean.DatasBean> datas = dataBean.get(i).getDatas();
                     childList.add(datas);
                 }
                 iMainActivity.showList(dataBean, childList);
             }
 
             @Override
             public void onFailure(Exception e) {
 
             }
         });
     }
 
}

  view层  MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class MainActivity extends AppCompatActivity implements IMainActivity{
 
     private ExpandableListView mElv;
     private CheckBox mCheckbox2;
     /**
      * 0
      */
     private TextView mTvPrice;
     /**
      * 结算(0)
      */
     private TextView mTvNum;
     private MyAdapter adapter;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         initView();
         EventBus.getDefault().register(this);
         new MainPresenter(this).getGoods();
         mCheckbox2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 adapter.changeAllListCbState(mCheckbox2.isChecked());
             }
         });
     }
 
     @Override
     protected void onDestroy() {
         super.onDestroy();
         EventBus.getDefault().unregister(this);
     }
 
     private void initView() {
         mElv = (ExpandableListView) findViewById(R.id.elv);
         mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
         mTvPrice = (TextView) findViewById(R.id.tv_price);
         mTvNum = (TextView) findViewById(R.id.tv_num);
     }
 
     @Override
     public void showList(List< GoosBean.DataBean > groupList, List< List <GoosBean.DataBean.DatasBean>> childList) {
         adapter = new MyAdapter(this, groupList, childList);
         mElv.setAdapter(adapter);
         mElv.setGroupIndicator(null);
         //默认让其全部展开
         for (int i = 0; i < groupList.size(); i++) {
             mElv.expandGroup(i);
         }
     }
     @Subscribe
     public void onMessageEvent(MessageEvent event){
         mCheckbox2.setChecked(event.isChecked());
 
     }
     @Subscribe
     public void onMessageEvent(PriceAndCountEvent event){
 
         mTvNum.setText("结算(" + event.getCount() + ")");
         mTvPrice.setText(event.getPrice()+"");
     }
 
}

  IMainActivity

1
2
3
public interface IMainActivity {
     public void showList(List< GoosBean.DataBean > groupList, List< List <GoosBean.DataBean.DatasBean>> childList);
}

  adapter下 MyAdapter

基本功能 2016/10/31 添加抽奖功能,修复已知Bug(点击删除购物,出现闪退bug) 2016/10/28 添加微信分享功能 2016/10/28 添加忘记密码功能、完善用户评价系统、以及完善订单页面按钮功能及显示 2016/10/27 添加自动更新功能,修复已知的登陆闪退Bug 2016/10/26 添加订单查询(全部订单、待付款、待发货、待收货、待评价、退款和售后等查询)以及显示功能,收藏宝贝查询、及显示功能 2016/10/25 添加加入购物车功能、移除购物车中的商品和下单之后更新购物车等功能 2016/10/24 添加评论功能的显示、登陆用户的收藏和取消收藏功能、下单页面的显示、以及下单功能的实现 2016/10/23 添加商品的类别显示、分类查询展示、固定数据的搜索(暂时数据固定) 2016/10/22 添加用户注销功能、头像的上传以及更新用户个人资料功能 2016/10/20 添加注册、登陆、加载默认头像等功能、解决ViewPager Fragment 中Fragment被预加载问 2016/10/19 进行主页、微淘、问大家、购物车、我的淘宝等页面的设计、添加轮播图、资讯滚动条功能 2016/10/18 构建基本MVP框架 开发过程中遇到的问题(可能导致程序无法运行的bug)及解决方案 当用户未登录时,点击购物车,登陆之后,程序闪退 出现问题 :NullPointerException 解决方案: 使用Fragment的延时加载(懒加载)实现对数据的加载 拍照时无法进行图片的裁剪(不断加载) 解决步骤如下: Activity跳转时图库时的Intent如下 Intent takePhotoIntent = new Intent( “android.media.action.IMAGE_CAPTURE”); takePhotoIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUri); 在onActivityResult()方法中调用P层进行处理,相关代码如下 String uri = Environment.getExternalStorageDirectory() “/icoImage.jpg”; if(!allSelectedPicture.contains(uri)){ allSelectedPicture.add(uri); } ViewPager和Fragment结合使用,Fragment出现被预计载的情况 解决方案 项目中使用的主要技术及框架 框架 ButterKnife 注解绑定获取控件功能 Picasso 网络和本地图片的加载功能 okhttp 网络连接功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值