今天就跟大家聊聊有关使用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(); } }
看完上述内容,你们对使用Listvie怎么实现一个购物车功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。