//自定义控件中的加减号
public class CustomView extends LinearLayout {
private EditText editText;
private Button revserse;
private Button add;
private int mCount = 1 ;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.customview,null,false);
revserse = (Button) view.findViewById(R.id.revserse);
add = (Button) view.findViewById(R.id.add);
editText = (EditText) view.findViewById(R.id.content);
revserse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//减号
try {
String content = editText.getText().toString().trim() ;
int count = Integer.valueOf(content)-1;
mCount = count;
if(count > 1){
editText.setText(count+"");
}
if(listener != null){
listener.click(count);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//加号
try {
String content = editText.getText().toString().trim() ;
int count = Integer.valueOf(content)+1;
mCount = count;
editText.setText(count+"");
if(listener != null){
listener.click(count);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
});
addView(view);
}
public int getCurrentCount(){
return mCount ;
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ClickListener listener;
public void setListener(ClickListener listener){
this.listener = listener;
}
/**
* 加减号 点击事件
*/
public interface ClickListener {
public void click(int count);
}
}
//activity
public class ShopActivity extends Activity {
@BindView(R.id.third_recyclerview)
RecyclerView thirdRecyclerview;
@BindView(R.id.third_allselect)
TextView thirdAllselect;
@BindView(R.id.third_totalprice)
TextView thirdTotalprice;
@BindView(R.id.third_totalnum)
TextView thirdTotalnum;
@BindView(R.id.third_submit)
TextView thirdSubmit;
@BindView(R.id.third_pay_linear)
LinearLayout thirdPayLinear;
private List mAllOrderList = new ArrayList<>();
private ShopAdapter adapter;
private LinearLayoutManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
ButterKnife.bind(this);
getData();
// 1 为选中 2 选中
thirdAllselect.setTag(1);
manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
adapter = new ShopAdapter(this);
thirdRecyclerview.setLayoutManager(manager);
thirdRecyclerview.setAdapter(adapter);
adapter.add(mAllOrderList);
adapter.setCheckBoxListener(new ShopAdapter.CheckBoxListener() {
@Override
public void check(int position, int count, boolean check,List list) {
sum(list);
}
});
adapter.setCustomViewListener(new ShopAdapter.CustomViewListener() {
@Override
public void click(int count,List list) {
sum(list);
}
});
adapter.setDelListener(new ShopAdapter.DelListener() {
@Override
public void del(int position,List list) {
sum(list);
}
});
}
float price = 0;
int count;
/**
* 计算总价
* @param mAllOrderList
*/
private void sum(List mAllOrderList) {
price = 0;
count = 0;
boolean allCheck = true ;
for (ShopBean.OrderDataBean.CartlistBean bean : mAllOrderList) {
if (bean.isCheck()) {
//得到总价
price += bean.getPrice() * bean.getCount();
//得到商品个数
count += bean.getCount();
}else {
// 只要有一个商品未选中,全选按钮 应该设置成 为选中
allCheck = false;
}
}
thirdTotalprice.setText("总价: " + price);
thirdTotalnum.setText("共" + count + "件商品");
if(allCheck){
thirdAllselect.setTag(2);
thirdAllselect.setBackgroundResource(R.drawable.shopcart_selected);
}else {
thirdAllselect.setTag(1);
thirdAllselect.setBackgroundResource(R.drawable.shopcart_unselected);
}
}
public void getData() {
try {
//模拟网络请求
InputStream inputStream = getAssets().open("shop.json");
String data = convertStreamToString(inputStream);
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(data, ShopBean.class);
for (int i = 0; i < shopBean.getOrderData().size(); i++) {
int length = shopBean.getOrderData().get(i).getCartlist().size();
for (int j = 0; j < length; j++) {
mAllOrderList.add(shopBean.getOrderData().get(i).getCartlist().get(j));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
boolean select = false ;
@OnClick(R.id.third_allselect)
public void onClick() {
//全选按钮 点击事件
int tag = (Integer) thirdAllselect.getTag() ;
if(tag ==1){
thirdAllselect.setTag(2);
select = true;
} else {
thirdAllselect.setTag(1);
select = false;
}
for (ShopBean.OrderDataBean.CartlistBean bean : mAllOrderList) {
bean.setCheck(select);
}
adapter.notifyDataSetChanged();
sum(adapter.getList());
}
}
//adapter
public class ShopAdapter extends RecyclerView.Adapter {
private Context context;
private List list ;
public ShopAdapter(Context context) {
this.context = context;
}
/**
* 更新数据
* @param list
*/
public void add(List list){
if(this.list == null){
this.list = new ArrayList<>();
}
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public ShopAdapter.IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.shop_adapter, null);
return new IViewHolder(view);
}
@Override
public void onBindViewHolder(final ShopAdapter.IViewHolder holder, final int position) {
//防止checkbox 滑动 错乱
holder.checkbox.setChecked(list.get(position).isCheck());
holder.danjia.setText(list.get(position).getPrice()+"");
ImageLoader.getInstance().displayImage(list.get(position).getDefaultPic(),holder.shopface);
/**
* checkbox 点击事件
*/
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.get(position).setCheck(holder.checkbox.isChecked());
notifyDataSetChanged();
if(checkBoxListener != null){
checkBoxListener.check(position,holder.customviewid.getCurrentCount(),holder.checkbox.isChecked(),list);
}
}
});
/**
* 加减监听
*/
holder.customviewid.setListener(new CustomView.ClickListener() {
@Override
public void click(int count) {
//更新数据源
list.get(position).setCount(count);
notifyDataSetChanged();
if(listener != null){
listener.click(count,list);
}
}
});
/**
* 删除点击事件
*/
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.remove(position);
notifyDataSetChanged();
if(delListener != null){
delListener.del(position,list);
}
}
});
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
static class IViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.checkbox)
CheckBox checkbox;
@BindView(R.id.shopface)
ImageView shopface;
@BindView(R.id.danjia)
TextView danjia;
@BindView(R.id.customviewid)
CustomView customviewid;
@BindView(R.id.shop_btn_del)
Button del ;
IViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
public List getList(){
return list;
}
CheckBoxListener checkBoxListener;
/**
* checkbox 点击事件
* @param listener
*/
public void setCheckBoxListener(CheckBoxListener listener){
this.checkBoxListener = listener;
}
interface CheckBoxListener {
public void check(int position,int count, boolean check,List list);
}
CustomViewListener listener;
/**
* 加减号 点击事件
* @param listener
*/
public void setCustomViewListener(CustomViewListener listener){
this.listener = listener;
}
interface CustomViewListener {
public void click(int count,List list);
}
DelListener delListener ;
/**
* 加减号 删除按钮事件
* @param listener
*/
public void setDelListener(DelListener listener) {
this.delListener = listener ;
}
interface DelListener {
public void del(int position,List list);
}
}