今天就跟大家聊聊有關(guān)使用Listvie怎么實現(xiàn)一個購物車功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的茂名網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1.自定義view實現(xiàn)加減
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.適配器接口回調(diào)
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.調(diào)用
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("結(jié)算("+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(); } }
看完上述內(nèi)容,你們對使用Listvie怎么實現(xiàn)一個購物車功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章題目:使用Listvie怎么實現(xiàn)一個購物車功能
當(dāng)前URL:http://jinyejixie.com/article6/iehoog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計、面包屑導(dǎo)航、微信公眾號、、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)