所实现的公务车的功能:对购物车中的商品进行全选、取消,对应的总价改变;选中的商品数量改变的同时,总价改变
导入recyclerview的依赖(根据自己studio自行选择):
compile 'com.android.support:recyclerview-v7:21.+.+'
用到的XML布局
activity_main.xml
item.xml
customview.xml
CustomView.java (一个自定义的view)
public class CustomView extends LinearLayout {
private TextView content;
private int num = 1;
public CustomView(Context context) {
super(context);
}
public CustomView(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.customview,null);// 解析布局
addView(view);
ImageView revserse = (ImageView) view.findViewById(R.id.revserse);
ImageView add = (ImageView) view.findViewById(R.id.add);
content = (TextView) view.findViewById(R.id.content);
revserse.setOnClickListener(new OnClickListener() {// 减少商品数量
@Override
public void onClick(View v) {
String result = content.getText().toString().trim();
int integerResult = Integer.valueOf(result);
if (integerResult > 1){
integerResult = integerResult - 1;
content.setText(integerResult+"");
if (listener!=null){
listener.onChange(integerResult);
}
}
}
});
add.setOnClickListener(new OnClickListener() { // 增加商品数量
@Override
public void onClick(View v) {
String result = content.getText().toString().trim();
int integerResult = Integer.valueOf(result);
if (integerResult < 10){
integerResult = integerResult + 1;
content.setText(integerResult+"");
if (listener!=null){
listener.onChange(integerResult);
}
}
}
});
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// 接口 用来传递商品的数量
public ChangeListener listener;
public void setListener(ChangeListener listener){
this.listener = listener;
}
interface ChangeListener{
public void onChange(int count);
}
}
Shopinfo (一个bean类,定义了商品的名称、价格、数量、选中状态)
public class Shopinfo {
private String name;
private double price;
private boolean b;
private int content;
public Shopinfo(String name, double price, boolean b, int content) {
this.name = name;
this.price = price;
this.b = b;
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public int getContent() {
return content;
}
public void setContent(int content) {
this.content = content;
}
@Override
public String toString() {
return "Shopinfo{" +
"name='" + name + '\'' +
", price=" + price +
", b=" + b +
", content=" + content +
'}';
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CheckBox all;
private TextView totalMoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
recyclerView = (RecyclerView) findViewById(R.id.rlv);
all = (CheckBox) findViewById(R.id.all);
totalMoney = (TextView) findViewById(R.id.totalprices);
// 创建集合,模拟30条数据
final List list = new ArrayList();
for (int i = 0; i < 30; i++) {
Shopinfo shopinfo = new Shopinfo("商品"+i,250+i,false,1);
list.add(shopinfo);
}
LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(manager);
final CarAdapter carAdapter = new CarAdapter(MainActivity.this,list);
recyclerView.setAdapter(carAdapter);
// 对条目的状态进行监听,并设置更新总额
carAdapter.setCheckListener(new CarAdapter.CheckListener() {
@Override
public void check(boolean check,int position) {
boolean allCheck = true ;
int totalprice = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isB()){
totalprice += list.get(i).getPrice()*list.get(i).getContent();
}
}
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).isB()) {
allCheck = false;
break;
}
}
totalMoney.setText("总计:¥ "+totalprice+"元");
if(allCheck){
all.setChecked(true);
}else {
all.setChecked(false);
}
}
});
all.setOnClickListener(new View.OnClickListener() {//商品全选
@Override
public void onClick(View v) {
boolean check = all.isChecked() ;
int totalprice = 0;
for (int i = 0; i < list.size(); i++) {
list.get(i).setB(check);
if (check){
if (list.get(i).isB()){
totalprice += list.get(i).getPrice()*list.get(i).getContent();
}
}
}
carAdapter.notifyDataSetChanged();
totalMoney.setText("总计:¥ "+totalprice+"元");
}
});
}
}
CarAdapter(recyclerview适配器)
public class CarAdapter extends RecyclerView.Adapter {
private Context context;
private List list;
public CarAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolder){
ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.name.setText(list.get(position).getName());
viewHolder.price.setText((int) list.get(position).getPrice() + "");
if (list.get(position).isB()){
if (!viewHolder.checkbox.isChecked()){
viewHolder.checkbox.setChecked(true);
}
}else{
viewHolder.checkbox.setChecked(false);
}
// 选中商品
viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = list.get(position).isB() ;
list.get(position).setB(!isChecked);
notifyDataSetChanged();
if (listener!=null){
listener.check(!isChecked,position);
}
}
});
viewHolder.customViewid.setListener(new CustomView.ChangeListener() {
@Override
public void onChange(int count) {
list.get(position).setContent(count);//将数量设置到控件上
notifyDataSetChanged();// 适配器刷新数据
if (listener!= null){
listener.check(list.get(position).isB(),position);
}
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
// 点击事件回调接口
public CheckListener listener;
public void setCheckListener(CheckListener listener){
this.listener = listener;
}
interface CheckListener {
public void check(boolean check,int position);
}
class ViewHolder extends RecyclerView.ViewHolder {
private final TextView name;
private final TextView price;
private final CheckBox checkbox;
private final CustomView customViewid;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tv_name);
price = (TextView) itemView.findViewById(R.id.price);
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
customViewid = (CustomView) itemView.findViewById(R.id.customviewid);
}
}
}