1. // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir } ext{ retrofitVersion = '2.4.0' rxandroid = '2.1.0' butterknife = '8.8.1' okhttp = '3.11.0' }
2. compile 'com.facebook.fresco:fresco:0.11.0' compile 'com.hjm:BottomTabBar:1.1.1' implementation 'com.sunfusheng:marqueeview:1.3.2' compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.jcodecraeer:xrecyclerview:1.5.9' compile 'com.youth.banner:banner:1.4.9' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.gyf.immersionbar:immersionbar:2.3.1' compile "com.squareup.retrofit2:retrofit:$rootProject.ext.retrofitVersion" compile "com.squareup.retrofit2:converter-gson:$rootProject.ext.retrofitVersion" compile "com.squareup.retrofit2:adapter-rxjava2:$rootProject.ext.retrofitVersion" compile 'io.reactivex.rxjava2:rxandroid:2.1.0' compile 'com.squareup.okhttp3:okhttp:3.11.0' compile 'com.squareup.okhttp3:logging-interceptor:3.11.0' testCompile 'junit:junit:4.12' compile project(':base') } configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion '25.0.1' } } } }
3.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
4.activity_main.xml
<com.hjm.bottomtabbar.BottomTabBar android:id="@+id/btm_tb" android:layout_width="match_parent" android:layout_height="match_parent" />
5.frag1.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:layout_marginBottom="-10dp"> <TextView android:id="@+id/shou" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="点击捕获异常" android:textSize="22dp" /> </LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recy_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"/>
6.frag1_item.xml
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/one_img" android:layout_width="100dp" android:layout_height="80dp"/> <TextView android:id="@+id/one_text" android:layout_width="match_parent" android:layout_marginLeft="10dp" android:layout_height="wrap_content" android:layout_alignBottom="@id/one_img" android:text="xxxxxxx" />
7.frag2.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:background="#CCCCCC" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="8" android:gravity="center" android:text="购物车" android:textSize="25dp"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="编辑" android:textSize="18dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#909090"/> <android.support.v7.widget.RecyclerView android:id="@+id/carGV" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" /> <RelativeLayout android:id="@+id/cart_bottom_layout" android:padding="5dp" android:background="#CCCCCC" android:textColor="#C0C0C0" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <CheckBox android:layout_centerVertical="true" android:id="@+id/allCheckbox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/totalpriceTv" android:layout_centerVertical="true" android:layout_toRightOf="@+id/allCheckbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总价:¥0.0"/> <Button android:id="@+id/buy" android:layout_alignParentRight="true" android:text="去结算" android:textSize="20dp" android:textColor="#fff" android:background="#F23030" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC"/>
8.frag2_er.xml
<LinearLayout android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/productCheckbox" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:src="@mipmap/ic_launcher" android:id="@+id/product_icon" android:layout_width="80dp" android:layout_height="80dp"/> </LinearLayout> <LinearLayout android:layout_marginLeft="10dp" android:layout_width="match_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:text="商品标题" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/price" android:layout_alignParentLeft="true" android:text="优惠价:¥99.99" android:textColor="@android:color/holo_red_light" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.example.kson.zk3_lianxi01.MyJIaJianView android:id="@+id/jiajianqi" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#999999"/>
9.frag2_jia_jian_view.xml
<TextView android:id="@+id/jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="25sp" android:padding="5dp"/> <View android:layout_width="1px" android:layout_height="match_parent" android:background="#999999"/> <EditText android:id="@+id/num" android:layout_weight="1" android:text="1" android:gravity="center" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <View android:layout_width="1px" android:layout_height="match_parent" android:background="#999999"/> <TextView android:id="@+id/jia" android:textSize="25sp" android:padding="5dp" android:text="+" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
10.frag2_yi.xml
<LinearLayout android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/sellerCheckbox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/sellerNameTv" android:text="商家" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#999999"/> <android.support.v7.widget.RecyclerView android:id="@+id/productXRV" android:layout_width="match_parent" android:layout_height="wrap_content"/> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#999999"/>
11.jia_jian_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffff"/> <size android:width="100dp" android:height="30dp"/> <stroke android:width="1px" android:color="#999999"/> </shape>
12.MainActivity
import android.graphics.Color; import android.os.Build; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import com.example.kson.zk3_lianxi01.fragment.Frag1; import com.example.kson.zk3_lianxi01.fragment.Frag2; import com.example.kson.zk3_lianxi01.fragment.Frag3; import com.example.kson.zk3_lianxi01.fragment.Frag4; import com.example.kson.zk3_lianxi01.fragment.Frag5; import com.gyf.barlibrary.ImmersionBar; import com.hjm.bottomtabbar.BottomTabBar; public class MainActivity extends AppCompatActivity { private BottomTabBar btm_tb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //沉浸式状态栏 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //隐藏ActionBar ActionBar actionBar = getSupportActionBar(); actionBar.hide(); } //初始化控件 btm_tb = (BottomTabBar) findViewById(R.id.btm_tb); //设置并使用BottomTabBar ImmersionBar.with(this).init(); btm_tb.init(getSupportFragmentManager()) .setImgSize(50,50) .setFontSize(15) .setChangeColor(Color.RED,Color.GRAY) .addTabItem("新品",R.drawable.syn, Frag1.class) .addTabItem("精选",R.drawable.fxn, Frag2.class) .addTabItem("分类",R.drawable.spn, Frag3.class) .addTabItem("我的衣橱",R.drawable.fln, Frag4.class) .addTabItem("个人中心",R.drawable.myn, Frag5.class); } }
13.MyJIaJianView
import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import io.reactivex.annotations.Nullable; public class MyJIaJianView extends LinearLayout { private TextView jiaTv,jiantv; private EditText numEt; private int num = 1; public MyJIaJianView(Context context) { this(context,null); } public MyJIaJianView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyJIaJianView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } //初始化自定义的布局 private void init(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.frag2_jia_jian_view,this,true); jiantv = view.findViewById(R.id.jian); jiaTv = view.findViewById(R.id.jia); numEt = view.findViewById(R.id.num); numEt.setText(num+""); jiaTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { num++; numEt.setText(num+""); if (jiaJianListener!=null){ jiaJianListener.getNum(num); } } }); jiantv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { num--; if (num<=0){ Toast.makeText(getContext(), "数量不能小于1", Toast.LENGTH_SHORT).show(); num = 1; } numEt.setText(num+""); if (jiaJianListener!=null){ jiaJianListener.getNum(num); } } }); } public void setNumEt(int n) { numEt.setText(n+""); num = Integer.parseInt(numEt.getText().toString()); } private JiaJianListener jiaJianListener; public void setJiaJianListener(JiaJianListener jiaJianListener) { this.jiaJianListener = jiaJianListener; } public interface JiaJianListener{ void getNum(int num); } } 14.frag1Bean ,frag2Bean
15.Contants
public class Contants { public static final String BASE_RELEASE_URL = "http://www.zhaoapi.cn/"; }
16.App
import android.app.Application; import com.example.kson.zk3_lianxi01.yichangbuhuo.CrashHandler; import com.facebook.drawee.backends.pipeline.Fresco; public class App extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(getApplicationContext()); } }
17.Frag1Api
import com.example.kson.zk3_lianxi01.bean.frag1Bean; import java.util.HashMap; import io.reactivex.Observable; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.POST; /** * Created by admin on 2018/9/15. */ public interface Frag1Api { @POST("product/searchProducts") @FormUrlEncoded Observable<frag1Bean> Show(@FieldMap HashMap<String,String> prams); }
18.Frag2Api
import com.example.kson.zk3_lianxi01.bean.frag2Bean; import java.util.HashMap; import io.reactivex.Observable; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.POST; /** * Created by admin on 2018/9/15. */ public interface Frag2Api { @POST("product/getCarts") @FormUrlEncoded Observable<frag2Bean> shopping(@FieldMap HashMap<String,String> prams); }
19.frag1Contract
import com.example.kson.zk3_lianxi01.bean.frag1Bean; import java.util.HashMap; import io.reactivex.Observable; import lianxibase1.bwie.com.base.base.mvp.BasePresenter; import lianxibase1.bwie.com.base.base.mvp.IBaseModel; import lianxibase1.bwie.com.base.base.mvp.IBaseView; public interface frag1Contract { abstract class frag1Present extends BasePresenter<frag1Model,frag1View> { public abstract void Show(HashMap<String,String> prams); @Override public frag1Model getmModel() { return new com.example.kson.zk3_lianxi01.model.frag1Model(); } } interface frag1View extends IBaseView { void fail(String msg); void success(frag1Bean frag1Bean); } interface frag1Model extends IBaseModel { Observable<frag1Bean> Show(HashMap<String, String> prams); } }
20.frag2Contract
import com.example.kson.zk3_lianxi01.bean.frag2Bean; import java.util.HashMap; import io.reactivex.Observable; import lianxibase1.bwie.com.base.base.mvp.BasePresenter; import lianxibase1.bwie.com.base.base.mvp.IBaseModel; import lianxibase1.bwie.com.base.base.mvp.IBaseView; public interface frag2Contract { //Presenter abstract class frag2Presenter extends BasePresenter<frag2Model,frag2View> { public abstract void shopping(HashMap<String,String> parms); @Override public frag2Contract.frag2Model getmModel() { return new com.example.kson.zk3_lianxi01.model.frag2Model(); } } //model层接口 interface frag2Model extends IBaseModel { Observable<frag2Bean> shopping(HashMap<String,String> parms); } //view层接口 interface frag2View extends IBaseView { void success(frag2Bean frag2Bean); void fail(String msg); } }
21.frag1Model
import com.example.kson.zk3_lianxi01.app.Frag2Api; import com.example.kson.zk3_lianxi01.bean.frag2Bean; import com.example.kson.zk3_lianxi01.common.Contants; import com.example.kson.zk3_lianxi01.view.frag2Contract; import java.util.HashMap; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import lianxibase1.bwie.com.base.net.RetrofitUtils; public class frag2Model implements frag2Contract.frag2Model{ @Override public Observable<frag2Bean> shopping(HashMap<String,String> parms) { return RetrofitUtils.getInstance().createApi(Contants.BASE_RELEASE_URL, Frag2Api.class) .shopping(parms).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); } }
22.frag2Model
import com.example.kson.zk3_lianxi01.app.Frag2Api; import com.example.kson.zk3_lianxi01.bean.frag2Bean; import com.example.kson.zk3_lianxi01.common.Contants; import com.example.kson.zk3_lianxi01.view.frag2Contract; import java.util.HashMap; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import lianxibase1.bwie.com.base.net.RetrofitUtils; public class frag2Model implements frag2Contract.frag2Model{ @Override public Observable<frag2Bean> shopping(HashMap<String,String> parms) { return RetrofitUtils.getInstance().createApi(Contants.BASE_RELEASE_URL, Frag2Api.class) .shopping(parms).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); } }
23.frag1Presenter
import com.example.kson.zk3_lianxi01.bean.frag1Bean; import com.example.kson.zk3_lianxi01.view.frag1Contract; import java.util.HashMap; import io.reactivex.functions.Consumer; public class frag1Presenter extends frag1Contract.frag1Present { @Override public void Show(HashMap<String, String> prams) { mModel.Show(prams).subscribe(new Consumer<frag1Bean>() { @Override public void accept(frag1Bean frag1Bean) throws Exception { mView.success(frag1Bean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { mView.fail("请求失败"); } }); } }
24.frag2Presenter
import com.example.kson.zk3_lianxi01.bean.frag2Bean; import com.example.kson.zk3_lianxi01.view.frag2Contract; import java.util.HashMap; import io.reactivex.functions.Consumer; public class frag2Presenter extends frag2Contract.frag2Presenter{ @Override public void shopping(HashMap<String,String> parms) { mModel.shopping(parms).subscribe(new Consumer<frag2Bean>() { @Override public void accept(frag2Bean frag2Bean) throws Exception { mView.success(frag2Bean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { mView.fail("网络有问题 ,请稍候再试"); } }); } }
25.frag1adapter
import android.content.Context; import android.net.Uri; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.kson.zk3_lianxi01.R; import com.example.kson.zk3_lianxi01.bean.frag1Bean; import com.example.kson.zk3_lianxi01.delete.ItemTouchHelperAdapter; import com.facebook.drawee.view.SimpleDraweeView; import java.util.Collections; import java.util.List; /** * Created by admin on 2018/9/15. */ public class frag1adapter extends RecyclerView.Adapter<frag1adapter.frag1HolderView> implements ItemTouchHelperAdapter { private Context context; private frag1Bean frag1Bean; private final List<frag1Bean.DataBean> data; public frag1adapter(Context context, frag1Bean frag1Bean) { this.context = context; this.frag1Bean = frag1Bean; data = frag1Bean.getData(); } @Override public frag1adapter.frag1HolderView onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.frag1_item, parent, false); return new frag1HolderView(view); } @Override public void onBindViewHolder(frag1adapter.frag1HolderView holder, int position) { holder.one_text.setText(data.get(position).getTitle()); Uri uri = Uri.parse(data.get(position).getImages().split("\\|")[0]); holder.one_img.setImageURI(uri); } @Override public int getItemCount() { return data.size(); } @Override public void onItemMove(int fromPosition, int toPosition) { //交换位置 Collections.swap(data,fromPosition,toPosition); notifyItemMoved(fromPosition,toPosition); } @Override public void onItemDissmiss(int position) { //移除数据 data.remove(position); notifyItemRemoved(position); } public class frag1HolderView extends RecyclerView.ViewHolder { private SimpleDraweeView one_img; private TextView one_text; public frag1HolderView(View itemView) { super(itemView); one_img = itemView.findViewById(R.id.one_img); one_text = itemView.findViewById(R.id.one_text); } } }
26.frag2yiadapter
import android.content.Context; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; import com.example.kson.zk3_lianxi01.R; import com.example.kson.zk3_lianxi01.bean.frag2Bean; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.List; public class frag2yiadapter extends RecyclerView.Adapter<frag2yiadapter.frag2yiViewHolder> { private Context context; private List<frag2Bean.DataBean> date; public frag2yiadapter(Context context,List<frag2Bean.DataBean> date){ this.context = context; this.date = date; } @Override public frag2yiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.frag2_yi,parent,false); if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); } return new frag2yiViewHolder(view); } @Override public void onBindViewHolder(final frag2yiViewHolder holder, int position) { final frag2Bean.DataBean bean = date.get(position); holder.sellerCheckbox.setChecked(bean.isSelected()); for (int i = 0; i < bean.getList().size(); i++) { if (!bean.getList().get(i).isSelected()){ holder.sellerCheckbox.setChecked(false); }else { holder.sellerCheckbox.setChecked(true); } } holder.productXRV.setLayoutManager(new LinearLayoutManager(context)); holder.sellerNameTv.setText(date.get(position).getSellerName()); frag2eradapter frag2eradapter = new frag2eradapter(context,bean.getList()); holder.productXRV.setAdapter(frag2eradapter); holder.sellerCheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (holder.sellerCheckbox.isChecked()){ bean.setSelected(true); for (int i = 0; i < bean.getList().size(); i++) { bean.getList().get(i).setSelected(true); } }else { bean.setSelected(false); for (int i = 0; i < bean.getList().size(); i++) { bean.getList().get(i).setSelected(false); } } notifyDataSetChanged(); } }); } @Override public int getItemCount() { return date.size(); } public class frag2yiViewHolder extends RecyclerView.ViewHolder{ private CheckBox sellerCheckbox; private TextView sellerNameTv; private RecyclerView productXRV; public frag2yiViewHolder(View itemView) { super(itemView); sellerCheckbox = itemView.findViewById(R.id.sellerCheckbox); sellerNameTv = itemView.findViewById(R.id.sellerNameTv); productXRV = itemView.findViewById(R.id.productXRV); } } public List<frag2Bean.DataBean> getDate() { return date; } @Subscribe(threadMode = ThreadMode.MAIN) public void notify(String msg) { notifyDataSetChanged(); } }
27.frag2eradapter
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; 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.kson.zk3_lianxi01.MyJIaJianView; import com.example.kson.zk3_lianxi01.R; import com.example.kson.zk3_lianxi01.bean.frag2Bean; import org.greenrobot.eventbus.EventBus; import java.util.List; /** * Created by admin on 2018/9/15. */ public class frag2eradapter extends RecyclerView.Adapter<frag2eradapter.frag2erViewHolder>{ private Context context; private List<frag2Bean.DataBean.ListBean> listBeans; public frag2eradapter(Context context,List<frag2Bean.DataBean.ListBean> listBeans){ this.context = context; this.listBeans = listBeans; } @Override public frag2erViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.frag2_er, parent, false); return new frag2erViewHolder(view); } @Override public void onBindViewHolder(final frag2erViewHolder holder, int position) { final frag2Bean.DataBean.ListBean bean = listBeans.get(position); holder.productCheckbox.setChecked(listBeans.get(position).isSelected()); holder.title.setText(listBeans.get(position).getTitle()); holder.price.setText("优惠价:¥"+listBeans.get(position).getBargainPrice()); String[] imgs = bean.getImages().split("\\|"); Glide.with(context).load(imgs[0]).into(holder.product_icon); holder.jiajianqi.setNumEt(bean.getNum()); holder.jiajianqi.setJiaJianListener(new MyJIaJianView.JiaJianListener() { @Override public void getNum(int num) { bean.setNum(num); EventBus.getDefault().post(""); } }); holder.jiajianqi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(holder.productCheckbox.isChecked()){ bean.setSelected(true); }else{ bean.setSelected(false); } EventBus.getDefault().post(""); } }); } @Override public int getItemCount() { return listBeans.size(); } public class frag2erViewHolder extends RecyclerView.ViewHolder{ private CheckBox productCheckbox; private ImageView product_icon; private TextView title; private TextView price; private MyJIaJianView jiajianqi; public frag2erViewHolder(View itemView) { super(itemView); productCheckbox = itemView.findViewById(R.id.productCheckbox); product_icon = itemView.findViewById(R.id.product_icon); title = itemView.findViewById(R.id.title); price = itemView.findViewById(R.id.price); jiajianqi = itemView.findViewById(R.id.jiajianqi); } } }
28.Frag1
import android.os.Handler; import android.os.Message; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.widget.TextView; import com.example.kson.zk3_lianxi01.R; import com.example.kson.zk3_lianxi01.adapter.frag1adapter; import com.example.kson.zk3_lianxi01.bean.frag1Bean; import com.example.kson.zk3_lianxi01.delete.SimpleItemTouchHelperCallback; import com.example.kson.zk3_lianxi01.presenter.frag1Presenter; import com.example.kson.zk3_lianxi01.view.frag1Contract; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.util.HashMap; import butterknife.BindView; import butterknife.OnClick; import butterknife.Unbinder; import lianxibase1.bwie.com.base.base.mvp.BaseMvpFragment; import lianxibase1.bwie.com.base.base.mvp.BasePresenter; public class Frag1 extends BaseMvpFragment<frag1Contract.frag1Model,frag1Contract.frag1Present> implements frag1Contract.frag1View{ @BindView(R.id.recy_view) XRecyclerView recy_view; @BindView(R.id.shou) TextView shou; Unbinder unbinder; private int page = 1; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0){ recy_view.refreshComplete(); } } }; private frag1adapter frag1adapter; private int i = 1; @OnClick(R.id.shou) public void shou(){ i = 10/0; } @Override protected int bindLayoutId() { return R.layout.frag1; } @Override protected void initData() { super.initData(); recy_view.setLayoutManager(new GridLayoutManager(getActivity(),1)); recy_view.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page = 1; frag1adapter.notifyDataSetChanged(); new Thread(){ @Override public void run() { super.run(); handler.sendEmptyMessageDelayed(0,2000); } }.start(); } @Override public void onLoadMore() { page += 1; frag1adapter.notifyDataSetChanged(); handler.postDelayed(new Runnable() { @Override public void run() { recy_view.loadMoreComplete(); } },2000); } }); HashMap<String,String> prams = new HashMap<>(); prams.put("keywords","笔记本"); prams.put("page",page+""); presenter.Show(prams); } @Override public void success(frag1Bean frag1Bean) { Log.i("ccc",frag1Bean.toString()); frag1adapter = new frag1adapter(getActivity(), frag1Bean); recy_view.setAdapter(frag1adapter); //先实例化Callback ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(frag1adapter); //用Callback构造ItemtouchHelper ItemTouchHelper touchHelper = new ItemTouchHelper(callback); //调用ItemTouchHelper的attachToRecyclerView方法建立联系 touchHelper.attachToRecyclerView(recy_view); } @Override public BasePresenter initPresenter() { return new frag1Presenter(); } @Override public void showLoading() { } @Override public void hideLoading() { } @Override public void fail(String msg) { } }
29.Frag2
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.RelativeLayout; import android.widget.TextView; import com.example.kson.zk3_lianxi01.R; import com.example.kson.zk3_lianxi01.adapter.frag2yiadapter; import com.example.kson.zk3_lianxi01.bean.frag2Bean; import com.example.kson.zk3_lianxi01.presenter.frag2Presenter; import com.example.kson.zk3_lianxi01.view.frag2Contract; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.HashMap; import java.util.List; import butterknife.BindView; import lianxibase1.bwie.com.base.base.mvp.BaseMvpFragment; import lianxibase1.bwie.com.base.base.mvp.BasePresenter; public class Frag2 extends BaseMvpFragment<frag2Contract.frag2Model,frag2Contract.frag2Presenter> implements frag2Contract.frag2View { @BindView(R.id.carGV) RecyclerView carGV; @BindView(R.id.cart_bottom_layout) RelativeLayout cart_bottom_layout; @BindView(R.id.allCheckbox) CheckBox allCheckbox; @BindView(R.id.totalpriceTv) TextView totalpriceTv; @BindView(R.id.buy) Button buy; private List<frag2Bean.DataBean> dataBeans; private frag2yiadapter frag2yiadapter; @Override protected void initData() { super.initData(); carGV.setLayoutManager(new LinearLayoutManager(getActivity())); HashMap<String,String> prams = new HashMap<>(); prams.put("uid","71"); presenter.shopping(prams); allCheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (allCheckbox.isChecked()){ for (int i = 0; i < dataBeans.size(); i++) { dataBeans.get(i).setSelected(true); for (int i1 = 0; i1 < dataBeans.get(i).getList().size(); i1++) { dataBeans.get(i).getList().get(i1).setSelected(true); } } }else { for (int i = 0; i < dataBeans.size(); i++) { dataBeans.get(i).setSelected(false); for (int i1 = 0; i1 < dataBeans.get(i).getList().size(); i1++) { dataBeans.get(i).getList().get(i1).setSelected(false); } } } frag2yiadapter.notifyDataSetChanged(); numm(); } }); } @Override public BasePresenter initPresenter() { return new frag2Presenter(); } @Override public void showLoading() { } @Override public void hideLoading() { } @Override public void success(frag2Bean frag2Bean) { dataBeans = frag2Bean.getData(); frag2yiadapter = new frag2yiadapter(getActivity(),dataBeans); carGV.setAdapter(frag2yiadapter); } @Override public void fail(String msg) { } @Override protected int bindLayoutId() { EventBus.getDefault().register(this); return R.layout.frag2; } @Subscribe(threadMode = ThreadMode.MAIN) public void notify(String msg) { StringBuilder stringBuilder = new StringBuilder(); if (frag2yiadapter != null){ for (int i = 0; i < frag2yiadapter.getDate().size(); i++) { stringBuilder.append(frag2yiadapter.getDate().get(i).isSelected()); for (int i1 = 0; i1 < frag2yiadapter.getDate().get(i).getList().size(); i1++) { stringBuilder.append(frag2yiadapter.getDate().get(i).getList().get(i1).isSelected()); } } } if (stringBuilder.toString().contains("false")) { allCheckbox.setChecked(false); } else { allCheckbox.setChecked(true); } frag2yiadapter.notifyDataSetChanged(); numm(); } private void numm() { double mymax = 0; for (int i = 0; i < frag2yiadapter.getDate().size(); i++) { for (int i1 = 0; i1 < frag2yiadapter.getDate().get(i).getList().size(); i1++) { if (frag2yiadapter.getDate().get(i).getList().get(i1).isSelected()) { frag2Bean.DataBean.ListBean listBean = frag2yiadapter.getDate().get(i).getList().get(i1); mymax += Double.parseDouble(String.valueOf(listBean.getBargainPrice()))*listBean.getNum(); } } totalpriceTv.setText("总价:¥"+mymax); } } @Override public void onDestroyView() { super.onDestroyView(); EventBus.getDefault().unregister(this); } }
30.ItemTouchHelperAdapter
public interface ItemTouchHelperAdapter { //数据交换 void onItemMove(int fromPosition, int toPosition); //数据删除 void onItemDissmiss(int position); }
31.SimpleItemTouchHelperCallback
import android.graphics.Canvas; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import com.example.kson.zk3_lianxi01.adapter.frag1adapter; public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback{ private frag1adapter frag1adapter; public SimpleItemTouchHelperCallback(frag1adapter frag1adapter) { this.frag1adapter = frag1adapter; } /** * 该方法用于返回可以滑动的方向,比如说允许从右到左侧滑,允许上下拖动等。我们一般使用makeMovementFlags(int,int)或makeFlag(int, int)来构造我们的返回值。 例如:要使RecyclerView的Item可以上下拖动,同时允许从右到左侧滑,但不许允许从左到右的侧滑,我们可以这样写: * @param recyclerView * @param viewHolder * @return */ @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { //允许上下的拖动 int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; //只允许从右向左侧滑 int swipeFlags = ItemTouchHelper.LEFT; return makeMovementFlags(dragFlags,swipeFlags); } /** * 当用户拖动一个Item进行上下移动从旧的位置到新的位置的时候会调用该方法,在该方法内,我们可以调用Adapter的notifyItemMoved方法来交换两个ViewHolder的位置,最后返回true,表示被拖动的ViewHolder已经移动到了目的位置。所以,如果要实现拖动交换位置,可以重写该方法(前提是支持上下拖动): * @param recyclerView * @param viewHolder * @param target * @return */ @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { //onItemMove是接口方法 frag1adapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition()); return true; } /** * 当用户左右滑动Item达到删除条件时,会调用该方法,一般手指触摸滑动的距离达到RecyclerView宽度的一半时,再松开手指,此时该Item会继续向原先滑动方向滑过去并且调用onSwiped方法进行删除,否则会反向滑回原来的位置。在该方法内部我们可以这样写: * 如果在onSwiped方法内我们没有进行任何操作,即不删除已经滑过去的Item,那么就会留下空白的地方,因为实际上该ItemView还占据着该位置,只是移出了我们的可视范围内罢了。 * @param viewHolder * @param direction */ @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { //onItemDissmiss是接口方法 frag1adapter.onItemDissmiss(viewHolder.getAdapterPosition()); } /** * 该方法返回true时,表示支持长按拖动,即长按ItemView后才可以拖动,我们遇到的场景一般也是这样的。默认是返回true。 * @return */ @Override public boolean isLongPressDragEnabled() { return true; } /** * 该方法返回true时,表示如果用户触摸并左右滑动了View,那么可以执行滑动删除操作,即可以调用到onSwiped()方法。默认是返回true。 * @return */ @Override public boolean isItemViewSwipeEnabled() { return true; } /** * 我们可以在这个方法内实现我们自定义的交互规则或者自定义的动画效果。 * @param c * @param recyclerView * @param viewHolder * @param dX * @param dY * @param actionState * @param isCurrentlyActive */ @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } }
32.CrashHandler
import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Field; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Environment; import android.os.Looper; import android.util.Log; import android.widget.Toast; /** * Created by admin on 2018/9/16. */ public class CrashHandler implements Thread.UncaughtExceptionHandler{ public static final String TAG = "CrashHandler"; //系统默认的UncaughtException处理类 private Thread.UncaughtExceptionHandler mDefaultHandler; //CrashHandler实例 private static CrashHandler INSTANCE = new CrashHandler(); //程序的Context对象 private Context mContext; //用来存储设备信息和异常信息 private Map<String, String> infos = new HashMap<String, String>(); //用于格式化日期,作为日志文件名的一部分 private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); //** 保证只有一个CrashHandler实例 */ private CrashHandler() { } /** 获取CrashHandler实例 ,单例模式 */ public static CrashHandler getInstance() { return INSTANCE; } public void init(Context context) { mContext = context; //获取系统默认的UncaughtException处理器 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); //设置该CrashHandler为程序的默认处理器 Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { if (!handleException(ex) && mDefaultHandler != null) { //如果用户没有处理则让系统默认的异常处理器来处理 mDefaultHandler.uncaughtException(thread, ex); } else { try { Thread.sleep(3000); } catch (InterruptedException e) { Log.e(TAG, "error : ", e); } //退出程序 android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } private boolean handleException(Throwable ex) { if (ex == null) { return false; } //使用Toast来显示异常信息 new Thread() { @Override public void run() { Looper.prepare(); Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_LONG).show(); Looper.loop(); } }.start(); //收集设备参数信息 collectDeviceInfo(mContext); //保存日志文件 saveCrashInfo2File(ex); return true; } public void collectDeviceInfo(Context ctx) { try { PackageManager pm = ctx.getPackageManager(); PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES); if (pi != null) { String versionName = pi.versionName == null ? "null" : pi.versionName; String versionCode = pi.versionCode + ""; infos.put("versionName", versionName); infos.put("versionCode", versionCode); } } catch (NameNotFoundException e) { Log.e(TAG, "an error occured when collect package info", e); } Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); infos.put(field.getName(), field.get(null).toString()); Log.d(TAG, field.getName() + " : " + field.get(null)); } catch (Exception e) { Log.e(TAG, "an error occured when collect crash info", e); } } } private String saveCrashInfo2File(Throwable ex) { StringBuffer sb = new StringBuffer(); for (Map.Entry<String, String> entry : infos.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); sb.append(key + "=" + value + "\n"); } Writer writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); ex.printStackTrace(printWriter); Throwable cause = ex.getCause(); while (cause != null) { cause.printStackTrace(printWriter); cause = cause.getCause(); } printWriter.close(); String result = writer.toString(); sb.append(result); try { long timestamp = System.currentTimeMillis(); String time = formatter.format(new Date()); String fileName = "crash-" + time + "-" + timestamp + ".log"; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { String path = "/sdcard/crash/"; File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } FileOutputStream fos = new FileOutputStream(path + fileName); fos.write(sb.toString().getBytes()); fos.close(); } return fileName; } catch (Exception e) { Log.e(TAG, "an error occured while writing file...", e); } return null; } } 附github