新建class类
package com.bawei.goshop.AddDow;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bawei.goshop.R;
import com.bawei.goshop.adapter.ShopAdapter;
import com.bawei.goshop.bean.ShopBean;
import java.util.List;
public class AddDowView extends LinearLayout {
public TextView textView_num;
public Button textView_add;
public Button textView_jian;
public AddDowView(Context context) {
this(context,null);
}
public AddDowView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public AddDowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs,defStyleAttr);
}
//接口
OnadddeleletClink onadddeleletClink;
public interface OnadddeleletClink{
void onAddClink(View view);
void onDeletClink(View view);
}
public void setOnadddeleletClink(OnadddeleletClink onadddeleletClink) {
this.onadddeleletClink = onadddeleletClink;
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
View view= View.inflate(context,R.layout.add_item,this);
textView_add = view.findViewById(R.id.adder_item);
textView_jian = view.findViewById(R.id.jian_item);
textView_num = view.findViewById(R.id.num_item);
//加
textView_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onadddeleletClink.onAddClink(v);
}
});
//减
textView_jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onadddeleletClink.onDeletClink(v);
}
});
}
public void setCount(ShopAdapter shopAdapter, int num, List<ShopBean.DataBean.ListBean> list1){
textView_num.setText(num+"");
}
public int getCount(){
int i = Integer.parseInt(textView_num.getText().toString());
return i;
}
}
在展示适配器里面写
先获取适配器里面的加载器数据
在上面定义数据/
//加载适配器
num = list1.get(i).getNum();
//加
shopViewHolder.addDowView.textView_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
shopViewHolder.addDowView.textView_num.setText(num+"");
}
});
//减
shopViewHolder.addDowView.textView_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (num>0){
num--;
shopViewHolder.addDowView.textView_num.setText(num+"");
}else{
Toast.makeText(context,"没有商品了",Toast.LENGTH_LONG).show();
}
}
});