Listvie简单实现购物车功能
时间:2021-07-07 08:50:57|栏目:Android代码|点击: 次
本文实例为大家分享了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(); } }
上一篇:Android编程基于重力传感器实现横竖屏放向切换功能
栏 目:Android代码
下一篇:基于GridView和ActivityGroup实现的TAB分页(附源码)
本文标题:Listvie简单实现购物车功能
本文地址:http://www.codeinn.net/misctech/153715.html