This article shares the specific code of Listvie's simple implementation of shopping cart for your reference. The specific content is as follows
1. Custom view to implement addition and subtraction
public class MyView extends LinearLayout implements { private OnAmountChangeListener mListener; private int count; private TextView numText; public MyView(Context context) { this(context,null); } public MyView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = (context, , this); initView(view); } public void setText(int num){ (""+num); } public int getText(){ getTextString(); return count; } private void initView(View view) { Button addBtn = (.btn_add); Button jianBtn = (.btn_jian); numText = (.txt_num); getTextString(); (this); (this); } public void setOnclickNum(OnAmountChangeListener mListener){ = mListener; } @Override public void onClick(View view) { getTextString(); switch (()){ case .btn_add: count++; break; case .btn_jian: if(count>0){ count--; } break; } (""+count); if(mListener!=null) { (view, count); } } public void getTextString() { String numStr = ().toString().trim(); count = (numStr); } public interface OnAmountChangeListener { void onAmountChange(View view, int amount); } }
2. Adapter interface callback
public class MyAdapter extends BaseAdapter { private List<Text> mList; private Context context ; private GaroupClick garoupClick; private int index; private CountClick countClick; public MyAdapter(Context context) { = context; } @Override public int getCount() { return (); } public void setData(List<Text> mList){ = mList; notifyDataSetChanged(); } @Override public Object getItem(int i) { return (i); } @Override public long getItemId(int i) { return i; } public void setOnCountClick(CountClick click){ = click; } public void setOnGroupclick(GaroupClick groupclick){ = groupclick; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { final ViewHolder holder ; if(view==null){ holder = new ViewHolder(); view = (context, , null); = (.cb_chid_checked); = (.txt_price); = (); (holder); }else{ holder = (ViewHolder)(); } final Text text = mList. get(i); (()); (""+()); (()); (new () { @Override public void onAmountChange(View view, int amount) { (amount); } }); boolean checked = (); if(checked){ (new () { @Override public void onAmountChange(View view, int amount) { if(countClick!=null){ (i,amount); } } }); } (new () { @Override public void onClick(View view) { (((CheckBox)view).isChecked()); if(garoupClick!=null){ (i,((CheckBox)view).isChecked()); } } }); return view; } class ViewHolder{ CheckBox childCheck; TextView txtPrice; MyView myView; } public interface GaroupClick{ void onItemClick(int position, boolean isChoose); } public interface CountClick{ void onNumClick(int position, int count); } }
3. Call
public class MainActivity extends AppCompatActivity implements , { private Button jiesuanBtn; private ListView mListView; private CheckBox allCheck; private TextView priceText; private List<Text> mList = new ArrayList<>(); private int numPrice = 0; private int count = 0; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); initData(); } private void initData() { for (int i = 0; i < 30; i++) { Text txt = new Text(); (20); (1); (txt); } adapter = new MyAdapter(this); (mList); (adapter); (this); (this); } public boolean isAllChecked(){ for (Text text : mList) { if(!()){//Transfer the collection, there is a return false that does not select return return false; } } return true; } private void initView() { jiesuanBtn = (Button) findViewById(.btn_num); mListView = (ListView) findViewById(); allCheck = (CheckBox) findViewById(.cb_all_checked); priceText = (TextView) findViewById(.txt_price_sum); (new () { @Override public void onClick(View view) { if(()!=0) { if(()) { for (int i = 0; i < (); i++) { (i).setChoose(true); (); } }else{ for (int i = 0; i < (); i++) { (i).setChoose(false); (); } } upDate(); } } }); (new () { @Override public void onClick(View view) { Intent intent = new Intent(, ); startActivity(intent); } }); } private void upDate() { numPrice = 0; count = 0; for (int i = 0; i < (); i++) { Text text = (i); if(()){ count++; numPrice += ()*(); } } ("Checkout("+count+")"); ("total:"+numPrice); } @Override public void onItemClick(int position, boolean isChoose) { (position).setChoose(isChoose); if(isAllChecked()){ (true); }else{ (false); } (); upDate(); } @Override public void onNumClick(int position, int count) { (position).setCount(count); (); upDate(); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.