public class MainActivity extends AppCompatActivity implements IView {
private RecyclerView recy_view;
private Presenter presenter;
private CheckBox check;
private ShopAdapter shopAdapter;
private Button jiesuan;
double zongjia;
private TextView price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recy_view = findViewById(R.id.recy_view);
price = findViewById(R.id.price);
check = findViewById(R.id.check);
jiesuan = findViewById(R.id.jiesuan);
presenter = new Presenter(this);
presenter.onPresenter();
}
@Override
public void onSuccess(final OneBean data) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recy_view.setLayoutManager(linearLayoutManager);
final List<TwoBean> list = data.getData();
//全選反選
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i <list.size(); i++) {
list.get(i).setChecked(isChecked);
for (int j = 0; j <list.get(i).getList().size(); j++) {
list.get(i).getList().get(j).setChecked(isChecked);
}
}
shopAdapter.notifyDataSetChanged();
}
});
//總價
jiesuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zongjia = 0;
for (int u = 0; u <list.size(); u++) {
for (int z = 0; z <list.get(u).getList().size(); z++) {
if (list.get(u).getList().get(z).isChecked()){
String bargainPrice = list.get(u).getList().get(z).getBargainPrice();
double vv = Double.parseDouble(bargainPrice);
zongjia+=vv;
}
}
}
price.setText(zongjia+"");
}
});
shopAdapter = new ShopAdapter(this,list);
recy_view.setAdapter(shopAdapter);
}
@Override
public void onFilar(String msg) {
}
}
、、、、、加減器
public class Jiajian extends LinearLayout {
private TextView jian;
private TextView jia;
private TextView num;
public Jiajian(Context context) {
this(context,null);
}
public Jiajian(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public Jiajian(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
initJiajian();
}
private void init(Context context) {
View view = inflate(context, R.layout.jiajianqi, this);
jian = findViewById(R.id.jian);
jia = findViewById(R.id.jia);
num = findViewById(R.id.num);
}
private void initJiajian() {
jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sum();
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jianfa();
}
});
}
private void sum() {
String s = num.getText().toString();
int i;
if(!TextUtils.isEmpty(s)&&!s.contains(".")){
i = Integer.parseInt(s);
i++;
num.setText(i+"");
}
}
private void jianfa() {
String s = num.getText().toString();
int i;
if (!TextUtils.isEmpty(s)&&!s.contains(".")){
i = Integer.parseInt(s);
i--;
if (i<0){
i=0;
Toast.makeText(getContext(), "不能小於0", Toast.LENGTH_SHORT).show();
}
num.setText(i+"");
}
}
public void setNums(int nums){
if (nums>0){
num.setText(nums+"");
}else {
Toast.makeText(getContext(), "不能小於0", Toast.LENGTH_SHORT).show();
}
}
}
、、、、、單選adapter
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
Context context;
List list;
public ShopAdapter(Context context, List<TwoBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ShopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.shop_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ShopAdapter.ViewHolder viewHolder, int i) {
TwoBean twoBean = list.get(i);
viewHolder.bind(twoBean);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView shop_name;
private final RecyclerView shop_view;
private final CheckBox check_shop;
private GoodsAdapter goodsAdapter;
public ViewHolder(@NonNull View itemView) {
super(itemView);
check_shop = itemView.findViewById(R.id.check_shop);
shop_name = itemView.findViewById(R.id.shop_name);
shop_view = itemView.findViewById(R.id.shop_view);
}
public void bind(TwoBean twoBean) {
String sellerName = twoBean.getSellerName();
shop_name.setText(sellerName);
check_shop.setChecked(twoBean.isChecked());
final List<ThreeBean> list = twoBean.getList();
check_shop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i <list.size(); i++) {
list.get(i).setChecked(isChecked);
}
goodsAdapter.notifyDataSetChanged();
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
shop_view.setLayoutManager(linearLayoutManager);
goodsAdapter = new GoodsAdapter(context,list);
shop_view.setAdapter(goodsAdapter);
}
}
}
、、、、、、、、子adapter
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
Context context;
List list;
public GoodsAdapter(Context context, List<ThreeBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public GoodsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.goods_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull GoodsAdapter.ViewHolder viewHolder, int i) {
ThreeBean threeBean = list.get(i);
viewHolder.bind(threeBean);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView goods_img;
private final TextView goods_name;
private final TextView goods_prcie;
private final CheckBox check_goods;
public ViewHolder(@NonNull View itemView) {
super(itemView);
goods_img = itemView.findViewById(R.id.goods_img);
goods_name = itemView.findViewById(R.id.goods_name);
goods_prcie = itemView.findViewById(R.id.goods_prcie);
check_goods = itemView.findViewById(R.id.check_goods);
}
public void bind(ThreeBean threeBean) {
check_goods.setChecked(threeBean.isChecked());
goods_name.setText(threeBean.getSubhead());
goods_prcie.setText(threeBean.getBargainPrice());
goods_img.setImageURI(threeBean.getDetailUrl());
}
}
}