一级列表展示购物车
依赖包
compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'org.greenrobot:eventbus:3.1.1' compile 'com.android.support:recyclerview-v7:24.2.1'
布局///主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#990000ff" android:gravity="center" android:text="购物车" android:textColor="#ff3660" android:textSize="25sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:gravity="center_vertical"> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:focusable="false" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/checkbox2" android:gravity="center_vertical" android:text="全选" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="合计 :" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="10dp" android:paddingRight="10dp" android:text="0" android:textColor="@android:color/holo_red_light" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="50dp" android:background="@android:color/holo_red_dark" android:gravity="center" android:padding="10dp" android:text="结算(0)" android:textColor="@android:color/white" /> </LinearLayout> </RelativeLayout> </LinearLayout>
child.xml注意自定义View
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginLeft="40dp" android:layout_marginTop="30dp" android:focusable="false" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="iphone6" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="什么手机" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="2016-12-10" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥3000.00" /> <bwie.com.gwc2.view.MyView android:id="@+id/mv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/tv_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> </LinearLayout>
myview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/iv_del" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/shopcart_minus_grey" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/shopcart_add_btn" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" android:text="1" /> <ImageView android:id="@+id/iv_add" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="5dp" android:src="@drawable/shopcart_add_red" /> </LinearLayout>
presenter GoodsPresenter
public class GoodsPresenter { private IMainActivity iMainActivity; private IGoodsModel iGoodsModel; public GoodsPresenter(IMainActivity iMainActivity){ iGoodsModel = new GoodsModel(); this.iMainActivity = iMainActivity; } public void getGoods(){ iGoodsModel.getGoods(new OnNetLisenter<GoodsBean>() { @Override public void onSuccess(GoodsBean goodsBean) { List<GoodsBean.DataBean.DatasBean> list = new ArrayList<GoodsBean.DataBean.DatasBean>(); List<GoodsBean.DataBean> data = goodsBean.getData(); for(int i = 0; i < data.size(); i++){ List<GoodsBean.DataBean.DatasBean> datas = data.get(i).getDatas(); list.addAll(datas); } iMainActivity.showList(list); } @Override public void onFailure(Exception e) { } }); } }
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<GoodsBean.DataBean.DatasBean> list; public MyAdapter(Context context, List<GoodsBean.DataBean.DatasBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.child, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { final GoodsBean.DataBean.DatasBean datasBean = list.get(position); final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.cbChild.setChecked(datasBean.isChecked()); myViewHolder.tv_tel.setText(datasBean.getType_name()); myViewHolder.tv_content.setText(datasBean.getMsg()); myViewHolder.tv_time.setText(datasBean.getAdd_time()); myViewHolder.tv_price.setText(datasBean.getPrice()+""); myViewHolder.myView.setNum(datasBean.getNum()+""); myViewHolder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { datasBean.setChecked(myViewHolder.cbChild.isChecked()); PriceAndCountEvent compute = compute(); EventBus.getDefault().post(compute); if(myViewHolder.cbChild.isChecked()){ if(isAllCbSelected()){ changeAllCbState(true); } }else { changeAllCbState(false); } notifyDataSetChanged(); } }); //增加数量 myViewHolder.myView.setAddClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = myViewHolder.myView.getNum(); num++; datasBean.setNum(num); if (myViewHolder.cbChild.isChecked()){ EventBus.getDefault().post(compute()); } notifyDataSetChanged(); } }); //减少数量 myViewHolder.myView.setDelClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = myViewHolder.myView.getNum(); if(num==1){ return; } num--; datasBean.setNum(num); if (myViewHolder.cbChild.isChecked()){ EventBus.getDefault().post(compute()); } notifyDataSetChanged(); } }); //删除 myViewHolder.tv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); } /** * 改变全选的状态 * @param flag */ public void changeAllCbState(boolean flag){ MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); } public boolean isAllCbSelected(){ for (int i = 0;i<list.size();i++){ GoodsBean.DataBean.DatasBean datasBean = list.get(i); if(!datasBean.isChecked()){ return false; } } return true; } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private final CheckBox cbChild; private TextView tv_tel; private TextView tv_content; private TextView tv_time; private TextView tv_price; private MyView myView; private TextView tv_del; public MyViewHolder(View itemView) { super(itemView); cbChild = (CheckBox) itemView.findViewById(R.id.cb_child); tv_tel = (TextView) itemView.findViewById(R.id.tv_tel); tv_content = (TextView) itemView.findViewById(R.id.tv_content); tv_time = (TextView) itemView.findViewById(R.id.tv_time); tv_price = (TextView) itemView.findViewById(R.id.tv_price); tv_del = (TextView) itemView.findViewById(R.id.tv_del); myView = (MyView) itemView.findViewById(R.id.mv); } } /* 计算数量的方法 */ private PriceAndCountEvent compute(){ int price = 0; int count = 0; for (int i = 0;i<list.size();i++){ GoodsBean.DataBean.DatasBean datasBean = list.get(i); if(datasBean.isChecked()){ price+=datasBean.getPrice()*datasBean.getNum(); count+=datasBean.getNum(); } } PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setPrice(price); priceAndCountEvent.setCount(count); return priceAndCountEvent; } /** * 全选 */ public void allSelect(boolean flag){ for (int i=0;i<list.size();i++){ GoodsBean.DataBean.DatasBean datasBean = list.get(i); datasBean.setChecked(flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }
MyView
public class MyView extends LinearLayout{ private ImageView iv_add; private ImageView iv_del; private TextView tv_num; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.myview, this); iv_add = (ImageView) findViewById(R.id.iv_add); iv_del = (ImageView) findViewById(R.id.iv_del); tv_num = (TextView) findViewById(R.id.tv_num); } public void setAddClickListener(OnClickListener onClickListener){ iv_add.setOnClickListener(onClickListener); } public void setDelClickListener(OnClickListener onClickListener){ iv_del.setOnClickListener(onClickListener); } public void setNum(String num){ tv_num.setText(num); } public int getNum(){ String num = tv_num.getText().toString(); return Integer.parseInt(num); } }
MainActivity
public class MainActivity extends AppCompatActivity implements IMainActivity{ private RecyclerView mRv; private CheckBox mCheckbox2; /** * 0 */ private TextView mTvPrice; /** * 结算(0) */ private TextView mTvNum; private GoodsPresenter goodsPresenter; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); goodsPresenter = new GoodsPresenter(this); initView(); mRv.setLayoutManager(new LinearLayoutManager(this)); goodsPresenter.getGoods(); mCheckbox2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAdapter.allSelect(mCheckbox2.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void onMessageEvent(MessageEvent event){ mCheckbox2.setChecked(event.isChecked()); } @Subscribe public void onMessageEvent(PriceAndCountEvent event){ mTvNum.setText("结算("+event.getCount()+")"); mTvPrice.setText(event.getPrice()+""); } private void initView() { mRv = (RecyclerView) findViewById(R.id.rv); mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2); mTvPrice = (TextView) findViewById(R.id.tv_price); mTvNum = (TextView) findViewById(R.id.tv_num); } @Override public void showList(List<GoodsBean.DataBean.DatasBean> list) { myAdapter = new MyAdapter(this, list); mRv.setAdapter(myAdapter); } }
M层 GoodsModel
public class GoodsModel implements IGoodsModel{ private Handler handler = new Handler(Looper.getMainLooper()); public void getGoods(final OnNetLisenter<GoodsBean> onNetLisenter){ Httputils.getHttputils().doGet(Api.url, new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { onNetLisenter.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final GoodsBean goodsBean = new Gson().fromJson(string, GoodsBean.class); handler.post(new Runnable() { @Override public void run() { onNetLisenter.onSuccess(goodsBean); } }); } }); } }