关于左边点击右边
private Itemclick itemclick;
public void setItemclick(Itemclick itemclick) {
this.itemclick = itemclick;
}
public interface Itemclick{
void click(int cid);
}
mh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemclick.click(list.get(i).getCid());
}
});
leftrv =view.findViewById(R.id.leftrv);
rightrv =view.findViewById(R.id.rightrv);
leftrv.setLayoutManager(new LinearLayoutManager(getActivity()));
rightrv.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new LeftAdapter(getActivity());
adapter1 = new RightAdapter(getActivity());
presenter = new ShowPresenter(this);
presenter.show1(UserApi.RIGHT_API,null);
presenter.show(UserApi.LEFTFEN_API,null);
leftrv.setAdapter(adapter);
rightrv.setAdapter(adapter1);
adapter.setItemclick(new LeftAdapter.Itemclick() {
@Override
public void click(int cid) {
HashMap<String,String> params=new HashMap<>();
params.put(“cid”,cid+"");
presenter.show1(UserApi.RIGHT_API,params);
}
});
}
关于购物车
void notityNum();
void nitify(boolean isChecked,int pos);
//void niticart();
*
- 计算总价
- */
- // adapter.setCartCallback(this);(这句话???)
public void getTotalprice(){
double totalprice=0;
for (ShowcartsBean.Data datum : data) {
for (ShowcartsBean.Data.LA la : datum.list) {
if(la.isProductChecked) {
totalprice += la.price * la.productNum;
}
}
}
fprice.setText(“¥:”+totalprice);
}
@Override
public void niticart() {
getTotalprice();
}
@Override
public void onSuccessCarts(String result) {
final ShowcartsBean showcartsBean = new Gson().fromJson(result, ShowcartsBean.class);
data = showcartsBean.data;
for (ShowcartsBean.Data datum : data) {
for (ShowcartsBean.Data.LA la : datum.list) {
la.productNum=1;
}
}
adapter.setList(data);
adapter.setCardUIcallback(this);
}
public class FragmentFour extends Fragment implements AContract.AView,CartUICallback {
private CheckBox cb;
private RecyclerView rvv;
private TextView price;
private Button btn_order;
private SerachPresenter presenter;
private AddsAdapter adapter;
private List<ShowcartsBean.Data> data;
private TextView fprice;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragmentfour, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
cb =view.findViewById(R.id.cb);
fprice =view.findViewById(R.id.price);
rvv =view.findViewById(R.id.rvv);
price =view.findViewById(R.id.price);
btn_order = view.findViewById(R.id.btn_order);
rvv.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new AddsAdapter(getActivity(),this);
presenter = new SerachPresenter(this);
HashMap<String,String> params=new HashMap<>();
params.put("uid","24421");
presenter.addcartss(UserApi.SHOWCATRS_API,params);
rvv.setAdapter(adapter);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
for (ShowcartsBean.Data datum : data) {
datum.isChecked=true;
for (ShowcartsBean.Data.LA la : datum.list) {
la.isProductChecked=true;
}
}
}else{
for (ShowcartsBean.Data datum : data) {
datum.isChecked=false;
for (ShowcartsBean.Data.LA la : datum.list) {
la.isProductChecked=false;
}
}
}
adapter.notifyDataSetChanged();
getTotalprice();
}
});
}
父子类
package com.example.addcarts.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
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.addcarts.R;
import com.example.addcarts.callback.CardCallback;
import com.example.addcarts.callback.CartUICallback;
import com.example.addcarts.entity.ShowcartsBean;
import java.util.ArrayList;
import java.util.List;
public class AddsAdapter extends RecyclerView.Adapter<AddsAdapter.VV> implements CardCallback {
private Context context;
private List<ShowcartsBean.Data> list;
private CartUICallback cartUICallback;
public AddsAdapter(Context context, CartUICallback cartUICallback) {
this.context = context;
this.list=new ArrayList<>();
this.cartUICallback = cartUICallback;
}
public void setList(List<ShowcartsBean.Data> list) {
if(list!=null) {
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public AddsAdapter.VV onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(context).inflate(R.layout.fragitem, viewGroup, false);
return new VV(view);
}
@Override
public void onBindViewHolder(@NonNull final AddsAdapter.VV vv, final int i) {
for (ShowcartsBean.Data.LA la : list.get(i).list) {
la.pos=i;
}
vv.name.setText(list.get(i).sellerName);
vv.fcheckbox.setChecked(list.get(i).isChecked);
vv.rvv.setLayoutManager(new LinearLayoutManager(context));
vv.rvv.setAdapter(new AddshowAdapter(context,list.get(i).list,this));
new AddshowAdapter(context,list.get(i).list,this)).setCartCallback(this);
vv.fcheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(i).isChecked=vv.fcheckbox.isChecked();
for (ShowcartsBean.Data.LA la : list.get(i).list) {
la.isProductChecked=list.get(i).isChecked;
}
notifyDataSetChanged();
if(cartUICallback!=null){
cartUICallback.niticart();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void notityNum() {
if(cartUICallback!=null){
cartUICallback.niticart();
}
}
@Override
public void nitify(boolean isChecked, int pos) {
list.get(pos).isChecked=isChecked;
if(cartUICallback!=null){
cartUICallback.niticart();
}
notifyDataSetChanged();
}
public class VV extends RecyclerView.ViewHolder {
private CheckBox fcheckbox;
private TextView name;
private RecyclerView rvv;
public VV(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
rvv=itemView.findViewById(R.id.frv);
fcheckbox=itemView.findViewById(R.id.fcheckbox);
}
}
}
子类适配器
package com.example.addcarts.adapter;
import android.content.Context;
import android.graphics.PorterDuff;
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.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.addcarts.R;
import com.example.addcarts.callback.CardCallback;
import com.example.addcarts.entity.ShowcartsBean;
import com.example.addcarts.width.AddminView;
import java.util.ArrayList;
import java.util.List;
public class AddshowAdapter extends RecyclerView.Adapter<AddshowAdapter.VG> {
private Context context;
private List<ShowcartsBean.Data.LA> list;
private CardCallback cardCallback;
public AddshowAdapter(Context context, List<ShowcartsBean.Data.LA> list, CardCallback cardCallback) {
this.context = context;
this.list = list;
this.cardCallback = cardCallback;
}
@NonNull
@Override
public AddshowAdapter.VG onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(context).inflate(R.layout.fragmentfouritem, viewGroup, false);
return new VG(view);
}
@Override
public void onBindViewHolder(@NonNull final AddshowAdapter.VG vg, final int i) {
vg.checkBox.setChecked(list.get(i).isProductChecked);
vg.name.setText(list.get(i).title);
vg.price.setText(list.get(i).price+"");
final String[] split = list.get(i).images.split("!");
Glide.with(context).load(split[0]).into(vg.icon);
vg.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(i).isProductChecked=vg.checkBox.isChecked();
for (ShowcartsBean.Data.LA la : list) {
if(!la.isProductChecked){
cardCallback.nitify(false,la.pos);
return;
}
cardCallback.nitify(true,la.pos);
}
}
});
vg.addminView.setAddminu(new AddminView.Addminu() {
@Override
public void addm(int num) {
list.get(i).productNum=num;
cardCallback.notityNum();
}
});
vg.addminView.setNum(list.get(i).productNum);
}
@Override
public int getItemCount() {
return list.size();
}
public class VG extends RecyclerView.ViewHolder {
private CheckBox checkBox;
private ImageView icon;
private TextView name;
private TextView price;
private AddminView addminView;
public VG(@NonNull View itemView) {
super(itemView);
checkBox=itemView.findViewById(R.id.checkbox);
icon=itemView.findViewById(R.id.icon);
name=itemView.findViewById(R.id.fv_name);
price=itemView.findViewById(R.id.fv_price);
addminView=itemView.findViewById(R.id.addmin);
}
}
}
自定义组合加减
final View view = LayoutInflater.from(context).inflate(R.layout.addview, this, true);
addTV = view.findViewById(R.id.add);
minusTv = view.findViewById(R.id.minus);
numTv = view.findViewById(R.id.et_num);
minusTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num = Integer.parseInt(numTv.getText().toString());
num–;
if(num==0){
num=1;
Toast.makeText(getContext(),“不能再减了”,Toast.LENGTH_SHORT).show();
}
numTv.setText(num+"");
addminu.addm(num);
}
});
addTV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num = Integer.parseInt(numTv.getText().toString());
num++;
numTv.setText(num+"");
addminu.addm(num);
}
});
}
public void setNum(int num) {
numTv.setText(num+"");
}
private Addminu addminu;
public void setAddminu(Addminu addminu) {
this.addminu = addminu;
}
public interface Addminu{
void addm(int num);
}