仿京東 首頁i+購物車+詳情頁i
阿新 • • 發佈:2019-02-09
//配置支援Rxjava2 implementation 'com.squareup.retrofit2:adapter-rxjava2:+' implementation 'io.reactivex.rxjava2:rxjava:+' implementation 'io.reactivex.rxjava2:rxandroid:+' //banner implementation 'com.youth.banner:banner:1.4.10' implementation 'com.jcodecraeer:xrecyclerview:1.3.2' implementation 'com.facebook.fresco:fresco:0.9.0+' implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0' implementation 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
attrs.xml
<resources> <declare-styleable name="MyApp"> <attr name="title_text" format="string"></attr> <attr name="title_color" format="color"></attr> <attr name="title_text_right" format="string"></attr> <attr name="title_color_right" format="color"></attr> </declare-styleable> </resources>
dimens.xml
<resources>
<dimen name="left_space_pading">12dp</dimen>
</resources>
//抽基
package com.example.thinkpad.wsjykaomoni.base; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; public abstract class BaseActivity <P extends BasePresenter > extends AppCompatActivity{ protected P presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(provideID()); //獲取介面 initview(); presenter= providePresenter(); //請求資料 initData(); //點選事件 initLiatener(); } protected abstract int provideID(); protected abstract P providePresenter(); protected abstract void initLiatener(); protected abstract void initData(); protected abstract void initview(); @Override protected void onDestroy() { presenter.onDestroy(); super.onDestroy(); } }
//
package com.example.thinkpad.wsjykaomoni.base;
import io.reactivex.disposables.CompositeDisposable;
public abstract class BasePresenter <V extends Iview>{
protected CompositeDisposable compositeDisposable= new CompositeDisposable();
protected V view;
public BasePresenter(V view){
this.view=view;
initModel();
}
protected abstract void initModel();
void onDestroy(){
view=null;
compositeDisposable.clear();
}
}
//
public interface Iview {
}
package com.example.thinkpad.wsjykaomoni.constantApi;
public class ConstantApi {
public static final String BaASE_URL="https://www.zhaoapi.cn/";
//首頁介面
public static final String HOME="home/getHome";
//詳情頁面
public static final String XIANGQIN="product/getProductDetail";
//新增購物車
public static final String ADDCAR="product/addCart";
//查詢購物車
public static final String SELECTCAR="product/getCarts";
}
//
package com.example.thinkpad.wsjykaomoni.util;
import com.example.thinkpad.wsjykaomoni.constantApi.ConstantApi;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitManager {
private static String BASE_URL= ConstantApi.BaASE_URL;
private final Retrofit mretrofit;
private static class SingleHodel{
private static final RetrofitManager INSTANT= new RetrofitManager(BASE_URL);
}
public static RetrofitManager getDefault(){
return SingleHodel.INSTANT;
}
public RetrofitManager(String baseUrl) {
mretrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(buildOkhttpCline())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
private OkHttpClient buildOkhttpCline() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000,TimeUnit.MILLISECONDS)
.readTimeout(5000,TimeUnit.MILLISECONDS)
.addInterceptor(httpLoggingInterceptor)
.build();
}
public <T> T create(final Class<T> Clazz) {
return mretrofit.create(Clazz);
}
}
//m
package com.example.thinkpad.wsjykaomoni.model;
import com.example.thinkpad.wsjykaomoni.bean.AddcarBean;
import com.example.thinkpad.wsjykaomoni.bean.HomeBean;
import com.example.thinkpad.wsjykaomoni.bean.SelectBean;
import com.example.thinkpad.wsjykaomoni.bean.XiangBean;
import com.example.thinkpad.wsjykaomoni.constantApi.ConstantApi;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface IModel {
//首頁
@GET(ConstantApi.HOME)
Observable<HomeBean> homezhan();
//詳情頁
@GET(ConstantApi.XIANGQIN)
Observable<XiangBean> xiang(@Query("pid")int pid);
//加入購物車
@GET(ConstantApi.ADDCAR)
Observable<AddcarBean> addcar(@Query("uid") int uid,@Query("pid")int pid);
//查詢購物車
@GET(ConstantApi.SELECTCAR)
Observable<SelectBean> select(@Query("uid") int uid);
}
//m介面
package com.example.thinkpad.wsjykaomoni.model;
import com.example.thinkpad.wsjykaomoni.bean.AddcarBean;
import com.example.thinkpad.wsjykaomoni.bean.HomeBean;
import com.example.thinkpad.wsjykaomoni.bean.SelectBean;
import com.example.thinkpad.wsjykaomoni.bean.XiangBean;
import com.example.thinkpad.wsjykaomoni.util.RetrofitManager;
import io.reactivex.Observable;
public class Model {
//首頁
public Observable<HomeBean> homezhuan(){
return RetrofitManager.getDefault().create(IModel.class).homezhan();
}
//詳情
public Observable<XiangBean> xiang(final int pid){
return RetrofitManager.getDefault().create(IModel.class).xiang(pid);
}
//加入購物車
public Observable<AddcarBean> addcar (final int uid,final int pid){
return RetrofitManager.getDefault().create(IModel.class).addcar(uid,pid);
}
//查詢購物車
public Observable<SelectBean> select (final int uid){
return RetrofitManager.getDefault().create(IModel.class).select(uid);
}
}
//購物車自定義
package com.example.thinkpad.wsjykaomoni.zidingyi;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.thinkpad.wsjykaomoni.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MyAddSubView extends LinearLayout{
//獲取控制元件id
@BindView(R.id.sub_tv)
TextView subTv;
@BindView(R.id.product_number_tv)
TextView productNumberTv;
@BindView(R.id.add_tv)
TextView addTv;
private int number = 1;
public MyAddSubView(Context context) {
this(context,null);
}
public MyAddSubView(Context context,@Nullable AttributeSet attrs) {
super(context, attrs);
View view = inflate(context, R.layout.add_remove_view_layput, this);
//必須初始化
ButterKnife.bind(view);
}
@OnClick({R.id.sub_tv,R.id.add_tv})
public void onViewClicked(View view){
switch (view.getId()){
//點選減號
//在購買數量大於1的時候,每點選一次就減一,並付給數量值
case R.id.sub_tv:
if(number>1){
--number;
//給商品數量賦值
productNumberTv.setText(number+"");
if(onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
}else{
Toast.makeText(getContext(),"不能在少了",Toast.LENGTH_LONG).show();
}
break;
//點選加號
case R.id.add_tv:
++number;
//給商品數量賦值
productNumberTv.setText(number+"");
if(onNumberChangeListener!=null){
//給介面傳值
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
//通過介面告訴外面一共有幾個商品
public interface OnNumberChangeListener{
void onNumberChange(int num);
}
}
//banner類
package com.example.thinkpad.wsjykaomoni.zidingyi;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.youth.banner.loader.ImageLoader;
public class MyLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Uri parse = Uri.parse((String) path);
imageView.setImageURI(parse);
}
@Override
public ImageView createImageView(Context context) {
SimpleDraweeView simpleDraweeView = new SimpleDraweeView(context);
return simpleDraweeView;
}
}
//購物車v
package com.example.thinkpad.wsjykaomoni.view;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.example.thinkpad.wsjykaomoni.R;
import com.example.thinkpad.wsjykaomoni.adpter.MyAdapter;
import com.example.thinkpad.wsjykaomoni.base.BaseActivity;
import com.example.thinkpad.wsjykaomoni.bean.SelectBean;
import com.example.thinkpad.wsjykaomoni.opresenter.Selectpresenter;
import java.util.List;
public class CarActivity extends BaseActivity<Selectpresenter> implements IselectVIew{
private ExpandableListView car_el;
private Button btn_cart_pay;
private TextView tv_cart_total_price;
private CheckBox cb_care_all_select;
private MyAdapter myAdapter;
@Override
protected int provideID() {
return R.layout.activity_car;
}
@Override
protected Selectpresenter providePresenter() {
return presenter=new Selectpresenter(this);
}
@Override
protected void initLiatener() {
}
@Override
protected void initData() {
presenter.select(14815);
}
@Override
protected void initview() {
car_el = findViewById(R.id.car_el);
cb_care_all_select = findViewById(R.id.cb_care_all_select);
tv_cart_total_price = findViewById(R.id.tv_cart_total_price);
btn_cart_pay = findViewById(R.id.btn_cart_pay);
}
@Override
public void oselectsuccer(SelectBean selectBean) {
List<SelectBean.DataBean> data = selectBean.getData();
// Log.e("tag","我是p+");
myAdapter = new MyAdapter(data);
myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {
@Override
public void onSellerCheckedChange(int groupPostition) {
//商家被點選
boolean currentSellerAllProductSelected = myAdapter.isCurrentSellerAllProductSelected(groupPostition);
myAdapter.changeCurrentSellerAllProductsStatus(groupPostition,!currentSellerAllProductSelected);
myAdapter.notifyDataSetChanged();
//重新整理底部資料
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
//點選商品得checkbox
myAdapter.changeCurrentProductStatus(groupPosition,childPosition);
myAdapter.notifyDataSetChanged();
//重新整理底部資料
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
//當加減被點選
myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
myAdapter.notifyDataSetChanged();
//重新整理底部資料
refreshSelectedAndTotalPriceAndTotalNumber();
//聯網更新網路上得商品數量
}
});
car_el.setAdapter(myAdapter);
//展開二級列表
for (int i=0;i<data.size();i++){
car_el.expandGroup(i);
}
//重新整理checkbox狀態和總價和總數量
refreshSelectedAndTotalPriceAndTotalNumber();
}
//重新整理checkbox狀態和總價和總數量
private void refreshSelectedAndTotalPriceAndTotalNumber() {
//去判斷是否所有得商品都被選中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
//設定給全選checkBox
cb_care_all_select.setChecked(allProductsSelected);//cbCartAllSelect
//計算總價
float totalPrice = myAdapter.calculateTotalPrice();
tv_cart_total_price.setText("總價 " + totalPrice);
//計算總數量
int totalNumber = myAdapter.calvulateTotalNumber();
btn_cart_pay.setText("去結算(" + totalNumber + ")");
}
@Override
public void onselectFile(String errso) {
}
//點選實現全選
public void onViewClicked(View view) {
//底部全選按鈕
//時候所有得商品都被選中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
myAdapter.changeAllProductsStatus(!allProductsSelected);
myAdapter.notifyDataSetChanged();
//重新整理底部資料
refreshSelectedAndTotalPriceAndTotalNumber();
}
}
//秒殺Adpter
package com.example.thinkpad.wsjykaomoni.adpter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.thinkpad.wsjykaomoni.R;
import com.example.thinkpad.wsjykaomoni.bean.HomeBean;
import com.example.thinkpad.wsjykaomoni.view.XiangActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import java.net.URI;
import java.util.List;
public class MiaoAdpter extends RecyclerView.Adapter<MiaoAdpter.MyViewHodel> {
private Context mcontext;
private List<HomeBean.DataBean.MiaoshaBean.ListBean> arr;
public MiaoAdpter(Context context,List<HomeBean.DataBean.MiaoshaBean.ListBean> list){
this.arr=list;
this.mcontext=context;
}
@NonNull
@Override
public MyViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(mcontext).inflate(R.layout.miao_item_tian, parent, false);
MyViewHodel myViewHodel = new MyViewHodel(inflate);
return myViewHodel ;
}
@Override
public void onBindViewHolder(@NonNull MyViewHodel holder, final int position) {
String detailUrl = arr.get(position).getImages();
String[] split = detailUrl.split("\\|");
String s = split[0];
Uri parse = Uri.parse(s);
holder.miao_sl.setImageURI(parse);
holder.miao_text.setText("¥"+arr.get(position).getPrice());
holder.miao_sl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mcontext, XiangActivity.class);
intent.putExtra("pid",arr.get(position).getPid());
mcontext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return arr==null?0:arr.size();
}
public class MyViewHodel extends RecyclerView.ViewHolder{
private final SimpleDraweeView miao_sl;
private final TextView miao_text;
public MyViewHodel(View itemView) {
super(itemView);
miao_sl = itemView.findViewById(R.id.miao_sl);
miao_text = itemView.findViewById(R.id.miao_text);
}
}
}