对于购物车自己做的一些简单的逻辑处理,能力有限还望多多指教
使用RecyclerView展示的数据
首先在主界面的布局是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shoppingcar.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_shop"></android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/bottom_shop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="@+id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:text="全选"
android:textSize="22sp" />
<LinearLayout
android:id="@+id/jiesuan_editor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="合计:"
android:textColor="#ff0000"
android:textSize="25sp" />
<TextView
android:id="@+id/zongjie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="0"
android:textColor="#ff0000"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
然后就是RecyclerView的item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/shop_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="20dp"
/>
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/imageviewgood"
android:layout_width="130dp"
android:layout_height="100dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
>
<TextView
android:id="@+id/shop_name"
android:textSize="18sp"
android:text="商品名字"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<TextView
android:id="@+id/shop_content"
android:text="商品信息"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:id="@+id/shop_price"
android:text="价格:3223.12"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/delete"
android:text="删除"
android:textColor="#fff"
android:background="#ff43"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
先看一下完整的适配器代码,可能还不算完美,求指教
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<GoodsBean> list = new ArrayList<>();
private Context context;
private LayoutInflater inflater;
private double totalPrice=0;
private TextView priceAll;
private CheckBox checkBox;
private Map<Integer,Boolean> map = new HashMap<>();
private boolean tag = true;
public MyAdapter(List<GoodsBean> list, Context context,TextView priceAll,CheckBox checkBox) {
this.checkBox = checkBox;
this.priceAll = priceAll;
this.list = list;
this.context = context;
this.inflater = LayoutInflater.from(context);
changeState(false);
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = inflater.inflate(R.layout.item, parent, false);
ViewHolder holder = new ViewHolder(inflate);
return holder;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, final int position) {
final GoodsBean goodsBean = list.get(position);
holder.name.setText(goodsBean.getName());
holder.xinxi.setText(goodsBean.getJieshao());
holder.price.setText(goodsBean.getPrice()+"");
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(goodsBean);
notifyDataSetChanged();
}
});
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
map.put(position,isChecked);
getPrice();
if (!isChecked){
checkBox.setChecked(false);
}else {
if (setAll()){
checkBox.setChecked(true);
}
}
}
});
holder.cb.setChecked(map.get(position));
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public CheckBox cb;
public TextView name,xinxi,price;
public Button delete;
public ViewHolder(View itemView) {
super(itemView);
cb = (CheckBox)itemView.findViewById(R.id.shop_checkbox);
name = (TextView)itemView.findViewById(R.id.shop_name);
xinxi = (TextView)itemView.findViewById(R.id.shop_content);
price = (TextView)itemView.findViewById(R.id.shop_price);
delete = (Button)itemView.findViewById(R.id.delete);
}
}
public void changeState(boolean isChecked) {
//遍历集合
for (int i=0;i<list.size();i++) {
map.put(i,isChecked);
}
notifyDataSetChanged();
}
public void getPrice(){
totalPrice = 0;
for (int i=0;i<list.size();i++) {
if (map.get(i)){
totalPrice += list.get(i).getPrice();
}
}
priceAll.setText(totalPrice+"");
}
public boolean setAll(){
for (Map.Entry<Integer,Boolean> mMap : map.entrySet()){
Boolean value = mMap.getValue();
if (!value){
return value;
}
}
return true;
}
}
拆分来看的话大概是分为 价格的计算,商品的删除,商品的CheckBox与主界面的CheckBox的联动
价格计算就比较简单了
定义一个全局的变量来记录总价,默认是0
private double totalPrice=0;
然后根据商品CheckBox的选中状态进行计算,这里是封装到了一个方法里面
public void getPrice(){
totalPrice = 0;//初始化价格
for (int i=0;i<list.size();i++) {
if (map.get(i)){//得到集合中选中的商品
totalPrice += list.get(i).getPrice();
}
}
priceAll.setText(totalPrice+"");//把总价格展示到控件
}
商品的删除就比较简单了,只需要把当前要删除的条目从 集合中移除就好了
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(goodsBean);//从集合中移除
notifyDataSetChanged();//记得刷新界面
}
});
CheckBox的联动的话,会有一个冲突,所以一个用setOnCheckedChangeListener,一个用setOnClickListener,这样就不会冲突了
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
map.put(position,isChecked);
getPrice();
if (!isChecked){
checkBox.setChecked(false);
}else {
if (setAll()){
checkBox.setChecked(true);
}
}
}
});
holder.cb.setChecked(map.get(position));
在Acitivity中的代码
allCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.changeState(ischecke);
ischecke = !ischecke;
}
});
Activity中的完整代码
public class MainActivity extends AppCompatActivity {
private RecyclerView my_recycler;
private List<GoodsBean> list = new ArrayList<>();
private CheckBox allCheck ;
private TextView zongjia;
private MyAdapter adapter;
private boolean ischecke = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
allCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ischecke = isChecked;
}
});
allCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.changeState(ischecke);
ischecke = !ischecke;
}
});
}
private void initData() {
for (int i=0;i<15;i++) {
list.add(new GoodsBean("小米手机"+i, "和科拉萨飞机三丰路"+i, 2500.00+i));
}
}
private void initView() {
allCheck = (CheckBox)findViewById(R.id.quanxuan);
zongjia = (TextView)findViewById(R.id.zongjie);
my_recycler = (RecyclerView) findViewById(R.id.my_recycler);
LinearLayoutManager manager = new LinearLayoutManager(this);
my_recycler.setLayoutManager(manager);
adapter = new MyAdapter(list,MainActivity.this,zongjia,allCheck);
my_recycler.setAdapter(adapter);
}
}
主要就是通过adapter对象调用自己定义的一些方法…
逻辑大概就是这样的,但是我的还有一些Bug还需要解决,就不献丑了!!!
希望有比较懂的大神给解决一下