import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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.TextView; import android.widget.Toast; import java.util.List; import Adapter.ShopAdapter; import Bean.ShopBean; import Presenter.ShopPresenter; import View.ShopView; import okhttp3.Call; public class MainActivity extends AppCompatActivity implements ShopView, View.OnClickListener { private RecyclerView mRecyclerView; private CheckBox mCheckBox; private TextView mTextView; private ShopPresenter mPre; private List<ShopBean.DataBean> mData; private ShopAdapter mAdapter; private double mSum; private int mNum; private Button mBut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 initView(); initData(); } private void initData() { mPre = new ShopPresenter(this); mPre.ShopNet(188 + ""); } /** * 寻找控件 */ private void initView() { mBut = (Button) findViewById(R.id.button_car); mRecyclerView = (RecyclerView) findViewById(R.id.rv_shopcar); mCheckBox = (CheckBox) findViewById(R.id.cb); mTextView = (TextView) findViewById(R.id.car_price); mCheckBox.setOnClickListener(this); } @Override public void ShopSeccuss(final ShopBean shop) { runOnUiThread(new Runnable() { @Override public void run() { mData = shop.getData(); LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this); mRecyclerView.setLayoutManager(llm); mAdapter = new ShopAdapter(MainActivity.this, mData); mRecyclerView.setAdapter(mAdapter); mAdapter.Price(new ShopAdapter.SetPrice() { @Override public void Price(int p, int i) { if (mData.get(p).getList().get(i).getSelected() == 1) { mSum = mSum + mData.get(p).getList().get(i).getNum() * mData.get(p).getList().get(i).getBargainPrice(); mNum++; } else { mSum = mSum - mData.get(p).getList().get(i).getNum() * mData.get(p).getList().get(i).getBargainPrice(); mNum--; } mTextView.setText(mSum + ""); mBut.setText("结算" + "(" + mNum + ")"); boolean bon = true; for (ShopBean.DataBean dataBean : mData) { for (ShopBean.DataBean.ListBean listBean : dataBean.getList()) { if (listBean.getSelected() == 0) { bon = false; } } } mCheckBox.setChecked(bon); } }); } }); } @Override public void ShopFail() { } @Override public void ShopNetWoke(Call call, Exception e) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "网络有误", Toast.LENGTH_SHORT).show(); return; } }); } /** * 选中点击时间 * @param view */ @Override public void onClick(View view) { //遍历判断 for (ShopBean.DataBean dataBean : mData) { for (ShopBean.DataBean.ListBean listBean : dataBean.getList()) { if (mCheckBox.isChecked()) { if (listBean.getSelected() == 0) { listBean.setSelected(1); mSum = mSum + listBean.getBargainPrice() * listBean.getNum(); mNum++; } } else { if (listBean.getSelected() == 1) { listBean.setSelected(0); mSum = mSum - listBean.getBargainPrice() * listBean.getNum(); mNum--; } } } } mBut.setText("结算" + "(" + mNum + ")"); mTextView.setText(mSum + ""); mAdapter.notifyDataSetChanged(); } }
上面是Activity里的代码逻辑
package Adapter; import android.content.Context; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import java.util.List; import Bean.ShopBean; import bwie.com.shopcar.R; /** * Created by 木子 on 2017/10/24. */ public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> { public Context context; public List<ShopBean.DataBean> list; private SetPrice setPrice; public void Price(SetPrice setPrice) { this.setPrice = setPrice; } public ShopAdapter(Context context, List<ShopBean.DataBean> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_shop, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.cb.setText(" " + list.get(position).getSellerName()); LinearLayoutManager llm = new LinearLayoutManager(context); final List<ShopBean.DataBean.ListBean> list1 = this.list.get(position).getList(); boolean bon = true; for (ShopBean.DataBean.ListBean listBean : list.get(position).getList()) { if (listBean.getSelected() == 0) { bon = false; } } holder.cb.setChecked(bon); holder.rv.setLayoutManager(llm); final Shop_ItemAdapter adapter = new Shop_ItemAdapter(list1, context); holder.rv.setAdapter(adapter); if (setPrice != null) { adapter.SetOnClickLiener(new Shop_ItemAdapter.SetOnClick() { @Override public void OnClick(int p) { boolean bon = true; for (ShopBean.DataBean.ListBean listBean : list.get(position).getList()) { if (listBean.getSelected() == 0) { bon = false; } } holder.cb.setChecked(bon); setPrice.Price(position, p); } }); } holder.cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (ShopBean.DataBean.ListBean listBean : list1) { if (holder.cb.isChecked()) { listBean.setSelected(1); } else { listBean.setSelected(0); } } adapter.notifyDataSetChanged(); } }); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private final CheckBox cb; private final RecyclerView rv; public ViewHolder(View itemView) { super(itemView); cb = itemView.findViewById(R.id.cb_shop); rv = itemView.findViewById(R.id.rv_shop); } } public interface SetPrice { void Price(int p, int i); } }
package Adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import Bean.ShopBean; import bwie.com.shopcar.R; /** * Created by 木子 on 2017/10/24. */ public class Shop_ItemAdapter extends RecyclerView.Adapter<Shop_ItemAdapter.ViewHolder> { private List<ShopBean.DataBean.ListBean> list; private Context context; private ViewHolder holder; public Shop_ItemAdapter(List<ShopBean.DataBean.ListBean> list, Context context) { this.list = list; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_car, null); holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.tv_price.setText("¥" + list.get(position).getBargainPrice()); holder.tv.setText(list.get(position).getTitle()); String[] split = list.get(position).getImages().split("\\|"); Glide.with(context).load(split[0]).into(holder.iv); if (list.get(position).getSelected() == 1) { holder.cb.setChecked(true); } else { holder.cb.setChecked(false); } if (setOnClick != null) { holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { list.get(position).setSelected(1); } else { list.get(position).setSelected(0); } setOnClick.OnClick(position); } }); } } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView iv; private CheckBox cb; private TextView tv; private TextView tv_price; public ViewHolder(View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv_car); cb = itemView.findViewById(R.id.cb_car); tv = itemView.findViewById(R.id.tv_car); tv_price = itemView.findViewById(R.id.tv_price_car); } } public SetOnClick setOnClick; public void SetOnClickLiener(SetOnClick setOnClick) { this.setOnClick = setOnClick; } public interface SetOnClick { void OnClick(int p); } }