Listvie simple implementation of shopping cart function

Time:2020-10-29

This example for you to share the listvie simple implementation of shopping cart specific code, for your reference, the specific content is as follows

1. Customize view to add and subtract


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. Adapter interface callback


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. Call

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 ()) {// traverse the collection, and return false is not selected
        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 (settlement ("+ count +"));
    priceText.setText ("total:" 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();
  }
}

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.