先倒入所需要依赖
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
testCompile 'junit:junit:4.12'
compile 'org.greenrobot:eventbus:3.0.0'
第一步写接口
public interface GoodsAPI {
@GET("ad/getAd")
Observable<TuiBean> Tui();
@GET("product/getProductDetail")
Observable<GoodsBean> get(@Query("pid") int pid);
@GET("product/addCart?uid=2237")
Call<ResponseBody> add(@Query("pid") int AddPid);
@GET("product/getCarts?uid=2237")
Observable<JiaBean> Jia();
@GET("product/deleteCart?uid=2237")
Call<ResponseBody> getShan(@Query("pid") int ShanPid);
}
主页布局是一个RecyclerVie w
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
根据接口创建Bean类
此步骤省略
创建展示数据的view
public interface ITuiView {
void showData(List<TuiBean.TuijianBean.ListBean> list);
}
创建model
public interface ITuiModel {
void showTui(Tui tui);
interface Tui{
void complate(List<TuiBean.TuijianBean.ListBean> list);
}
}
public class TuiModel implements ITuiModel {
@Override
public void showTui(final Tui tui) {
RetrofitApp.getInstance().Tui()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<TuiBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.i("======", "onError: "+e.toString());
}
@Override
public void onNext(TuiBean tuiBean) {
List<TuiBean.TuijianBean.ListBean> list= tuiBean.getTuijian().getList();
tui.complate(list);
}
});
}
}
封装RetrofitApp
public class RetrofitApp {
private static GoodsAPI service = null ;
public static GoodsAPI getInstance(){
if(service == null){
synchronized (RetrofitApp.class){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
service = retrofit.create(GoodsAPI.class);
}
}
return service;
}
}
创建presenter
public interface IPresenter<T>{
void attach(T view);
void deach();
}
public class TuiPresenter implements IPresenter<ITuiView>{
ITuiView view;
ITuiModel model;
SoftReference<ITuiView> reference;
public TuiPresenter(ITuiView view) {
attach(view);
model = new TuiModel();
}
public void TuiData(){
model.showTui(new ITuiModel.Tui() {
@Override
public void complate(List<TuiBean.TuijianBean.ListBean> list) {
reference.get().showData(list);
}
});
}
@Override
public void attach(ITuiView view) {
reference = new SoftReference<ITuiView>(view);
}
@Override
public void deach() {
reference.clear();
}
}
基类
public abstract class BeasActivity<T extends IPresenter> extends Activity {
T presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createpresenter();
}
protected abstract void createpresenter();
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter!=null){
presenter.deach();
}
}
}
初始化frseco
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
创建adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<TuiBean.TuijianBean.ListBean> list;
//构造方法
public MyAdapter(Context context, List<TuiBean.TuijianBean.ListBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.recy_item,null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
String images = list.get(position).getImages();
String[] split = images.split("\\|");
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse(split[0]))
.build();
((MyViewHolder)holder).sim.setController(controller);
((MyViewHolder) holder).tv.setText(list.get(position).getTitle());
((MyViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pid = list.get(position).getPid();
Intent intent = new Intent(context, DetaActivity.class);
intent.putExtra("pid",pid+"");
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
if (list!=null){
return list.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
SimpleDraweeView sim;
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
sim = itemView.findViewById(R.id.sim);
tv = itemView.findViewById(R.id.tv);
}
}
}
主页Activity
public class MainActivity extends BeasActivity<TuiPresenter> implements ITuiView {
TuiPresenter presenter;
private RecyclerView mRv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
presenter.TuiData();
}
private void initView() {
mRv = (RecyclerView) findViewById(R.id.rv);
}
@Override
protected void createpresenter() {
presenter = new TuiPresenter(this);
}
@Override
public void showData(List<TuiBean.TuijianBean.ListBean> list) {
Log.i("=========", "showData: "+list.toString());
GridLayoutManager mgs = new GridLayoutManager(this,2);
mRv.setLayoutManager(mgs);
MyAdapter adapter = new MyAdapter(this,list);
mRv.setAdapter(adapter);
}
}
接下来。就是点击详情
首先。还是解析一个bean类。此步骤省略
布局
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/oo_sim"
android:layout_width="match_parent"
android:layout_height="600dp" />
<TextView
android:id="@+id/oo_tv"
android:layout_below="@+id/oo_sim"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="加入购物车"
android:background="#ff0000"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
创建View层
public interface IGoodsView {
void showData(GoodsBean bean);
int pid();
}
创建model层
public interface IGoodsModel {
void showGoods(int pid,Goods goods);
interface Goods{
void complate(GoodsBean bean);
}
}
public class GoodsModel implements IGoodsModel {
@Override
public void showGoods(int pid, final Goods goods) {
RetrofitUtils.getInstance().get(pid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<GoodsBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.i("=======", "onError: "+e.toString());
}
@Override
public void onNext(GoodsBean bean) {
goods.complate(bean);
}
});
}
}
接口
public class RetrofitUtils {
private static GoodsAPI service = null ;
public static GoodsAPI getInstance(){
if(service == null){
synchronized (RetrofitUtils.class){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(OkHttpUtils.getInstance())
.build();
service = retrofit.create(GoodsAPI.class);
}
}
return service;
}
}
创建presenter
public class GoodsPresenter implements IPresenter<IGoodsView>{
IGoodsView view;
IGoodsModel model;
SoftReference<IGoodsView> reference;
public GoodsPresenter(IGoodsView view) {
attach(view);
model = new GoodsModel();
}
public void GoodsData(){
int pid = reference.get().pid();
model.showGoods(pid, new IGoodsModel.Goods() {
@Override
public void complate(GoodsBean bean) {
reference.get().showData(bean);
}
});
}
@Override
public void attach(IGoodsView view) {
reference = new SoftReference<IGoodsView>(view);
}
@Override
public void deach() {
reference.clear();
}
}
最后Adpter
public class DetaActivity extends BeasActivity<GoodsPresenter> implements IGoodsView, View.OnClickListener {
//全局变量
GoodsPresenter presenter;
int i;
private SimpleDraweeView mSimOo;
private TextView mTvOo;
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deta);
initView();
String Sid = getIntent().getStringExtra("pid");
i = Integer.parseInt(Sid);
presenter.GoodsData();
}
//找控件
private void initView() {
mSimOo = (SimpleDraweeView) findViewById(R.id.oo_sim);
mTvOo = (TextView) findViewById(R.id.oo_tv);
mBtn = (Button) findViewById(R.id.btn);
mBtn.setOnClickListener(this);
}
@Override
protected void createpresenter() {
presenter = new GoodsPresenter(this);
}
//p层方法
@Override
public void showData(GoodsBean bean) {
String images = bean.getData().getImages();
String[] split = images.split("\\|");
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse(split[0]))
.build();
mSimOo.setController(controller);
mTvOo.setText(bean.getData().getTitle());
EventBus.getDefault().post(new MessageEvent(bean.getData().getPid()));
}
@Override
public int pid() {
return i;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Call<ResponseBody> call = RetrofitAPI.getInstance().add(i);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(DetaActivity.this,"添加",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DetaActivity.this,JiaActivity.class);
intent.putExtra("pid",i+"");
startActivity(intent);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(DetaActivity.this,"失败",Toast.LENGTH_SHORT).show();
}
});
break;
default:
break;
}
}
/*@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Intent intent = new Intent(DetaActivity.this,JiaActivity.class);
startActivity(intent);
break;
default:
break;
}
}*/
}
购物车的bean类。还是省略
布局
activity_jia
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
>
<TextView
android:id="@+id/jia_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="编辑"/>
<TextView
android:id="@+id/jia_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:visibility="gone"
android:text="完成"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_View"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:gravity="center_vertical"
android:padding="10dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:background="@drawable/shopcart_unselected"
android:button="@null"
android:id="@+id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<LinearLayout
android:padding="10dp"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:textColor="#e53e42"
android:id="@+id/total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="总价 : ¥0元"
/>
<TextView
android:id="@+id/total_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="共0件商品"
/>
</LinearLayout>
<TextView
android:id="@+id/shanchu"
android:gravity="center"
android:textSize="25sp"
android:text="删除"
android:textColor="#fff"
android:visibility="gone"
android:background="@drawable/qujiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/jiesuan"
android:gravity="center"
android:textSize="25sp"
android:text="去结算"
android:textColor="#fff"
android:background="@drawable/qujiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
自定义布局
custom_jiajian
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:background="#fff"
android:textSize="20sp"
android:id="@+id/reverse"
android:text="一"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<EditText
android:textStyle="bold"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/count"
/>
<Button
android:id="@+id/add"
android:background="#fff"
android:textSize="25sp"
android:text="+"
android:layout_width="50dp"
android:layout_height="wrap_content" />
</LinearLayout>
商店布局
rely_cart_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/shop_checkbox"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_marginLeft="20dp"
android:text="良品铺子"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textStyle="bold"
android:id="@+id/shop_name"
/>
</LinearLayout>
<LinearLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/item_face"
android:src="@mipmap/ic_launcher"
fresco:failureImageScaleType="centerInside"
android:layout_width="120dp"
android:layout_height="120dp" />
<LinearLayout
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_name"
android:textSize="20sp"
android:text="三只松鼠"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
/>
<TextView
android:textColor="#f00"
android:id="@+id/item_price"
android:textSize="23sp"
android:text="299"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.example.mx.miaoxinzhoukao3.utils.CustomJiaJian
android:id="@+id/custom_jiajian"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<ImageView
android:id="@+id/item_delete"
android:layout_marginRight="10dp"
android:src="@mipmap/ic_launcher"
android:layout_width="30dp"
android:layout_height="30dp" />
</LinearLayout>
</LinearLayout>
创建view
public interface IJiaView {
void showData(JiaBean bean);
}
创建model
public interface IJiaModel {
void showJia(Jia jia);
interface Jia{
void complate(JiaBean bean);
}
}
public class JiaModel implements IJiaModel {
@Override
public void showJia(final Jia jia) {
RetrofitUtils.getInstance().Jia()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JiaBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(JiaBean bean) {
jia.complate(bean);
}
});
}
}
接口
public class RetrofitUtils {
private static GoodsAPI service = null ;
public static GoodsAPI getInstance(){
if(service == null){
synchronized (RetrofitUtils.class){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(OkHttpUtils.getInstance())
.build();
service = retrofit.create(GoodsAPI.class);
}
}
return service;
}
}
创建presenter
public class JiaPresenter implements IPresenter<IJiaView> {
IJiaView view;
IJiaModel model;
SoftReference<IJiaView> reference;
public JiaPresenter(IJiaView view) {
attach(view);
model = new JiaModel();
}
public void JiaData(){
model.showJia(new IJiaModel.Jia() {
@Override
public void complate(JiaBean bean) {
reference.get().showData(bean);
}
});
}
@Override
public void attach(IJiaView view) {
reference = new SoftReference<IJiaView>(view);
}
@Override
public void deach() {
reference.clear();
}
}
创建adapter
public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.MyViewHolder>{
JiaActivity jiaActivity;
Context context;
List<Integer> listint=new ArrayList<>();
//创建大的集合
private List<JiaBean.DataBean.ListBean> list;
//存放商家的id和商家的名称的map集合
private Map<String,String> map = new HashMap<>();
public RecyAdapter(Context context, JiaActivity jiaActivity) {
this.context = context;
this.jiaActivity=jiaActivity;
}
/**
* 添加数据并更新显示
* */
public void add(JiaBean cartBean){
//传进来的是bean对象
if(list == null){
list = new ArrayList<>();
}
//第一层遍历商家和商品
for (JiaBean.DataBean shop : cartBean.getData()){
//把商品的id和商品的名称添加到map集合里 ,,为了之后方便调用
map.put(shop.getSellerid(),shop.getSellerName());
//第二层遍历里面的商品
for (int i=0;i<shop.getList().size();i++){
//添加到list集合里
list.add(shop.getList().get(i));
}
}
//调用方法 设置显示或隐藏 商铺名
setFirst(list);
notifyDataSetChanged();
}
/**
* 设置数据源,控制是否显示商家
* */
private void setFirst(List<JiaBean.DataBean.ListBean> list) {
if(list.size()>0){
//如果是第一条数据就设置isFirst为1
list.get(0).setIsFirst(1);
//从第二条开始遍历
for (int i=1;i<list.size();i++){
//如果和前一个商品是同一家商店的
if (list.get(i).getSellerid() == list.get(i-1).getSellerid()){
//设置成2不显示商铺
list.get(i).setIsFirst(2);
}else{//设置成1显示商铺
list.get(i).setIsFirst(1);
//如果当前条目选中,把当前的商铺也选中
if (list.get(i).isItem_check()==true){
list.get(i).setShop_check(list.get(i).isItem_check());
}
}
}
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.recy_cart_item,null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
//判断是否选中
if(list.get(position).isItem_check()==true){
listint.add(position);
Log.i("listssssss", "onBindViewHolder: "+listint.toString());
jiaActivity.getdeletetv().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Call<ResponseBody> call = RetrofitUtils.getInstance().getShan(list.get(position).getPid());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(context,"删除",Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(context,"失败",Toast.LENGTH_SHORT).show();
}
});
list.remove(position);
sum(list);
notifyDataSetChanged();
}
});
}
/**
* 设置商铺的 shop_checkbox和商铺的名字 显示或隐藏
* */
if(list.get(position).getIsFirst()==1){
//显示商家
holder.shop_checkbox.setVisibility(View.VISIBLE);
holder.shop_name.setVisibility(View.VISIBLE);
//设置shop_checkbox的选中状态
holder.shop_checkbox.setChecked(list.get(position).isShop_check());
holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));
}else{//2
//隐藏商家
holder.shop_name.setVisibility(View.GONE);
holder.shop_checkbox.setVisibility(View.GONE);
}
//拆分images字段
String images = list.get(position).getImages();
String[] split = images.split("\\|");
//设置商品的图片
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(Uri.parse(split[0]))
.build();
holder.item_face.setController(controller);
//控制商品的item_checkbox,,根据字段改变
holder.item_checkbox.setChecked(list.get(position).isItem_check());
holder.item_name.setText(list.get(position).getTitle());
holder.item_price.setText(list.get(position).getPrice()+"");
//调用customjiajian里面的方法设置 加减号中间的数字
holder.customJiaJian.setEditText(list.get(position).getNum());
//商铺的shop_checkbox点击事件 ,控制商品的item_checkbox
holder.shop_checkbox.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//先改变数据源中的shop_check
list.get(position).setShop_check(holder.shop_checkbox.isChecked());
for (int i=0;i<list.size();i++){
//如果是同一家商铺的 都给成相同状态
if(list.get(position).getSellerid()==list.get(i).getSellerid()){
//当前条目的选中状态 设置成 当前商铺的选中状态
list.get(i).setItem_check(holder.shop_checkbox.isChecked());
}
}
//刷新适配器
notifyDataSetChanged();
//调用求和的方法
sum(list);
}
});
//商品的item_checkbox点击事件,控制商铺的shop_checkbox
holder.item_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.item_checkbox.isChecked()){
list.get(position).setItem_check(true);
}
//先改变数据源中的item_checkbox
list.get(position).setItem_check(holder.item_checkbox.isChecked());
//反向控制商铺的shop_checkbox
for (int i=0;i<list.size();i++){
for (int j=0;j<list.size();j++){
//如果两个商品是同一家店铺的 并且 这两个商品的item_checkbox选中状态不一样
if(list.get(i).getSellerid()==list.get(j).getSellerid() && !list.get(j).isItem_check()){
//就把商铺的shop_checkbox改成false
list.get(i).setShop_check(false);
break;
}else{
//同一家商铺的商品 选中状态都一样,就把商铺shop_checkbox状态改成true
list.get(i).setShop_check(true);
}
}
}
//更新适配器
notifyDataSetChanged();
//调用求和的方法
sum(list);
}
});
//删除条目的点击事件
holder.item_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);//移除集合中的当前数据
//删除完当前的条目 重新判断商铺的显示隐藏
setFirst(list);
//调用重新求和
sum(list);
notifyDataSetChanged();
}
});
//加减号的监听,
holder.customJiaJian.setCustomListener(new CustomJiaJian.CustomListener() {
@Override
public void jiajian(int count) {
//改变数据源中的数量
list.get(position).setNum(count);
notifyDataSetChanged();
sum(list);
}
@Override
//输入值 求总价
public void shuRuZhi(int count) {
list.get(position).setNum(count);
notifyDataSetChanged();
sum(list);
}
});
}
/**
* 计算总价的方法
* */
private void sum(List<JiaBean.DataBean.ListBean> list){
int totalNum = 0;//初始的总价为0
float totalMoney = 0.0f;
boolean allCheck = true;
for (int i=0;i<list.size();i++){
//把 已经选中的 条目 计算价格
if (list.get(i).isItem_check()){
totalNum += list.get(i).getNum();
totalMoney += list.get(i).getNum() * list.get(i).getPrice();
}else{
//如果有个未选中,就标记为false
allCheck = false;
}
}
//接口回调出去 把总价 总数量 和allcheck 传给view层
updateListener.setTotal(totalMoney+"",totalNum+"",allCheck);
}
//view层调用这个方法, 点击quanxuan按钮的操作
public void quanXuan(boolean checked) {
for (int i=0;i<list.size();i++){
list.get(i).setShop_check(checked);
list.get(i).setItem_check(checked);
}
notifyDataSetChanged();
sum(list);
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final CheckBox shop_checkbox;
private final TextView shop_name;
private final CheckBox item_checkbox;
private final TextView item_name;
private final TextView item_price;
private final CustomJiaJian customJiaJian;
private final ImageView item_delete;
private final SimpleDraweeView item_face;
public MyViewHolder(View itemView) {
super(itemView);
shop_checkbox = (CheckBox) itemView.findViewById(R.id.shop_checkbox);
shop_name = (TextView) itemView.findViewById(R.id.shop_name);
item_checkbox = (CheckBox) itemView.findViewById(R.id.item_checkbox);
item_name = (TextView) itemView.findViewById(R.id.item_name);
item_price = (TextView) itemView.findViewById(R.id.item_price);
customJiaJian = (CustomJiaJian) itemView.findViewById(R.id.custom_jiajian);
item_delete = (ImageView) itemView.findViewById(R.id.item_delete);
item_face = (SimpleDraweeView) itemView.findViewById(R.id.item_face);
}
}
UpdateListener updateListener;
public void setUpdateListener(UpdateListener updateListener){
this.updateListener = updateListener;
}
//接口
public interface UpdateListener{
public void setTotal(String total,String num,boolean allCheck);
}
}
自定义控件
public class CustomJiaJian extends LinearLayout {
private Button reverse;
private Button add;
private EditText countEdit;
private int mCount =1;
public CustomJiaJian(Context context) {
super(context);
}
public CustomJiaJian(final Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context, R.layout.custom_jiajian,this);
reverse = (Button) view.findViewById(R.id.reverse);
add = (Button) view.findViewById(R.id.add);
countEdit = (EditText) view.findViewById(R.id.count);
reverse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String content = countEdit.getText().toString().trim();
int count = Integer.valueOf(content);
if(count>1){
mCount = count-1;
countEdit.setText(mCount+"");
//回调给adapter里面
if(customListener!=null){
customListener.jiajian(mCount);
}
}
if (count==1){
Toast.makeText(context,"最小只能为一",Toast.LENGTH_SHORT).show();
}
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String content = countEdit.getText().toString().trim();
int count = Integer.valueOf(content)+1;
mCount = count;
countEdit.setText(mCount+"");
//接口回调给adapter
if(customListener!=null){
customListener.jiajian(mCount);
}
}
});
}
public CustomJiaJian(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
CustomListener customListener;
public void setCustomListener(CustomListener customListener){
this.customListener = customListener;
}
//加减的接口
public interface CustomListener{
public void jiajian(int count);
public void shuRuZhi(int count);
}
//这个方法是供recyadapter设置 数量时候调用的
public void setEditText(int num) {
if(countEdit !=null) {
countEdit.setText(num + "");
}
}
}
jiaAc tivity
public class JiaActivity extends BeasActivity<JiaPresenter> implements IJiaView, View.OnClickListener {
JiaPresenter presenter;
private RecyclerView mViewRecycler;
private CheckBox mQuanxuan;
private TextView mPriceTotal;
private TextView mNumTotal;
private RecyAdapter recyAdapter;
private TextView mTvJia;
private TextView mTv2Jia;
private TextView mShanchu;
private TextView mJiesuan;
int pid;
List<JiaBean.DataBean.ListBean> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jia);
initView();
String Sid = getIntent().getStringExtra("pid");
pid = Integer.parseInt(Sid);
presenter.JiaData();
}
public TextView getdeletetv(){
return mShanchu;
}
private void initView() {
mViewRecycler = (RecyclerView) findViewById(R.id.recycler_View);
mQuanxuan = (CheckBox) findViewById(R.id.quanxuan);
mPriceTotal = (TextView) findViewById(R.id.total_price);
mNumTotal = (TextView) findViewById(R.id.total_num);
mQuanxuan.setTag(1);//1为不选中
LinearLayoutManager manager = new LinearLayoutManager(JiaActivity.this, LinearLayoutManager.VERTICAL, false);
//new出适配器
recyAdapter = new RecyAdapter(this,this);
mViewRecycler.setLayoutManager(manager);
mViewRecycler.setAdapter(recyAdapter);
recyAdapter.setUpdateListener(new RecyAdapter.UpdateListener() {
@Override
public void setTotal(String total, String num, boolean allCheck) {
//设置ui的改变
mNumTotal.setText("共" + num + "件商品");//总数量
mPriceTotal.setText("总价 :¥" + total + "元");//总价
if (allCheck) {
mQuanxuan.setTag(2);
mQuanxuan.setBackgroundResource(R.drawable.shopcart_selected);
} else {
mQuanxuan.setTag(1);
mQuanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
}
mQuanxuan.setChecked(allCheck);
}
});
//这里只做ui更改, 点击全选按钮,,调到adapter里面操作
mQuanxuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用adapter里面的方法 ,,把当前quanxuan状态传递过去
int tag = (int) mQuanxuan.getTag();
if (tag == 1) {
mQuanxuan.setTag(2);
mQuanxuan.setBackgroundResource(R.drawable.shopcart_selected);
} else {
mQuanxuan.setTag(1);
mQuanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
}
recyAdapter.quanXuan(mQuanxuan.isChecked());
}
});
mTvJia = (TextView) findViewById(R.id.jia_tv);
mTv2Jia = (TextView) findViewById(R.id.jia_tv2);
mShanchu = (TextView) findViewById(R.id.shanchu);
mJiesuan = (TextView) findViewById(R.id.jiesuan);
mTvJia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTv2Jia.setVisibility(View.VISIBLE);
mTvJia.setVisibility(View.GONE);
mShanchu.setVisibility(View.VISIBLE);
mJiesuan.setVisibility(View.GONE);
}
});
mTv2Jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvJia.setVisibility(View.VISIBLE);
mTv2Jia.setVisibility(View.GONE);
mShanchu.setVisibility(View.GONE);
mJiesuan.setVisibility(View.VISIBLE);
}
});
mShanchu.setOnClickListener(this);
}
@Override
protected void createpresenter() {
presenter = new JiaPresenter(this);
}
@Override
public void showData(JiaBean bean) {
recyAdapter.add(bean);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shanchu:
Call<ResponseBody> call = RetrofitUtils.getInstance().getShan(pid);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(JiaActivity.this,"删除",Toast.LENGTH_SHORT).show();
recyAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(JiaActivity.this,"失败",Toast.LENGTH_SHORT).show();
}
});
break;
default:
break;
}
}
}
数据展示+购物车
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 023|决策时间:用户思考了很久,又把商品放回去了,怎么办? 什么是“决策时间”?消费者的注意力时长越来越短,人们...