欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Listvie简单实现购物车功能

程序员文章站 2022-09-06 13:18:58
本文实例为大家分享了listvie简单实现购物车的具体代码,供大家参考,具体内容如下 1.自定义view实现加减 public class myview ext...

本文实例为大家分享了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();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。