时间:2021-05-20
本文实例为大家分享了Listvie简单实现购物车的具体代码,供大家参考,具体内容如下
1.自定义view实现加减
public class MyView extends LinearLayout implements View.OnClickListener { 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 = View.inflate(context, R.layout.add, this); initView(view); } public void setText(int num){ numText.setText(""+num); } public int getText(){ getTextString(); return count; } private void initView(View view) { Button addBtn = view.findViewById(R.id.btn_add); Button jianBtn = view.findViewById(R.id.btn_jian); numText = view.findViewById(R.id.txt_num); getTextString(); addBtn.setOnClickListener(this); jianBtn.setOnClickListener(this); } public void setOnclickNum(OnAmountChangeListener mListener){ this.mListener = mListener; } @Override public void onClick(View view) { getTextString(); switch (view.getId()){ case R.id.btn_add: count++; break; case R.id.btn_jian: if(count>0){ count--; } break; } numText.setText(""+count); if(mListener!=null) { mListener.onAmountChange(view, count); } } public void getTextString() { String numStr = numText.getText().toString().trim(); count = Integer.parseInt(numStr); } public interface OnAmountChangeListener { void onAmountChange(View view, int amount); }}2.适配器接口回调
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) { this.context = context; } @Override public int getCount() { return mList.size(); } public void setData(List<Text> mList){ this.mList = mList; notifyDataSetChanged(); } @Override public Object getItem(int i) { return mList.get(i); } @Override public long getItemId(int i) { return i; } public void setOnCountClick(CountClick click){ this.countClick = click; } public void setOnGroupclick(GaroupClick groupclick){ this.garoupClick = groupclick; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { final ViewHolder holder ; if(view==null){ holder = new ViewHolder(); view = View.inflate(context, R.layout.item, null); holder.childCheck = view.findViewById(R.id.cb_chid_checked); holder.txtPrice = view.findViewById(R.id.txt_price); holder.myView = view.findViewById(R.id.myview); view.setTag(holder); }else{ holder = (ViewHolder)view.getTag(); } final Text text = mList. get(i); holder.childCheck.setChecked(text.isChoose()); holder.txtPrice.setText(""+text.getPrice()); holder.myView.setText(text.getCount()); holder.myView.setOnclickNum(new MyView.OnAmountChangeListener() { @Override public void onAmountChange(View view, int amount) { text.setCount(amount); } }); boolean checked = holder.childCheck.isChecked(); if(checked){ holder.myView.setOnclickNum(new MyView.OnAmountChangeListener() { @Override public void onAmountChange(View view, int amount) { if(countClick!=null){ countClick.onNumClick(i,amount); } } }); } holder.childCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { text.setChoose(((CheckBox)view).isChecked()); if(garoupClick!=null){ garoupClick.onItemClick(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.调用
public class MainActivity extends AppCompatActivity implements MyAdapter.GaroupClick, MyAdapter.CountClick { 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) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { for (int i = 0; i < 30; i++) { Text txt = new Text(); txt.setPrice(20); txt.setCount(1); mList.add(txt); } adapter = new MyAdapter(this); adapter.setData(mList); mListView.setAdapter(adapter); adapter.setOnGroupclick(this); adapter.setOnCountClick(this); } public boolean isAllChecked(){ for (Text text : mList) { if(!text.isChoose()){//遍历集合,有一个不选return false return false; } } return true; } private void initView() { jiesuanBtn = (Button) findViewById(R.id.btn_num); mListView = (ListView) findViewById(R.id.listview); allCheck = (CheckBox) findViewById(R.id.cb_all_checked); priceText = (TextView) findViewById(R.id.txt_price_sum); allCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mList.size()!=0) { if(allCheck.isChecked()) { for (int i = 0; i < mList.size(); i++) { mList.get(i).setChoose(true); adapter.notifyDataSetChanged(); } }else{ for (int i = 0; i < mList.size(); i++) { mList.get(i).setChoose(false); adapter.notifyDataSetChanged(); } } upDate(); } } }); jiesuanBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, PayDemoActivity.class); startActivity(intent); } }); } private void upDate() { numPrice = 0; count = 0; for (int i = 0; i < mList.size(); i++) { Text text = mList.get(i); if(text.isChoose()){ count++; numPrice += text.getPrice()*text.getCount(); } } jiesuanBtn.setText("结算("+count+")"); priceText.setText("合计:"+numPrice); } @Override public void onItemClick(int position, boolean isChoose) { mList.get(position).setChoose(isChoose); if(isAllChecked()){ allCheck.setChecked(true); }else{ allCheck.setChecked(false); } adapter.notifyDataSetChanged(); upDate(); } @Override public void onNumClick(int position, int count) { mList.get(position).setCount(count); adapter.notifyDataSetChanged(); upDate(); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了java实现购物车功能的具体代码,供大家参考,具体内容如下1需要实现1、实现淘淘商城的购物车功能2购物车功能2.1功能说明1、商品加入购物车
PHP+Mysql简单实现了图书购物车本文主要讲述如何通过PHP+HTML简单实现图书购物车的功能,这是提取我们php项目的部分内容。主要内容包括:1.通过Ja
本文实例为大家分享了Servlet实现购物车功能的具体代码,供大家参考,具体内容如下(1)用servlet实现简单的购物车系统,项目结构例如以下:(新建webP
本文实例讲述了PHP实现的购物车类。分享给大家供大家参考。具体分析如下:该购物车类是基于CodeIgniter的购物车类仿写实现的。购物车基本功能如下:1)将物
本文介绍一个php实现的购物车代码,功能实现完整,具有一定的参考价值这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能