安卓1jShopDemo
来源:互联网 发布:域名注册管理机构 编辑:程序博客网 时间:2024/05/29 19:07
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="mvpframework.bwie.com.yijishopdemo.view.view.MainActivity"> <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/elv" 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>//item布局<?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:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="iphone6" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:focusable="false" /> <ImageView android:id="@+id/iv" android:layout_width="60dp" android:layout_height="60dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="什么手机" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" 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_pri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥3000.00" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 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="20dp" android:src="@drawable/shopcart_add_red" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/tv_del" android:layout_marginLeft="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> </LinearLayout></LinearLayout>//net包public class App extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration =new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); }}//net包public class Api { public static final String url = "http://120.27.23.105/product/getCarts?uid=100";}//net包HttpUtilspublic class HttpUtils { private static volatile HttpUtils httpUtils; private final OkHttpClient client; private HttpUtils(){ client= new OkHttpClient.Builder().build(); } public static HttpUtils getHttpUtils(){ if(httpUtils == null){ synchronized (HttpUtils.class){ if(httpUtils == null){ httpUtils=new HttpUtils(); } } } return httpUtils; } public void doGet(String url, Callback callback){ Request request=new Request.Builder().url(url).build(); client.newCall(request).enqueue(callback); }}public interface OnNetListenter<T> {OnNetListenter类
public void onSueecss(T t); public void onFailure(Exception e);}//model接口public interface IMainModel {public void getShop(OnNetListenter<ShopBean> onNetListenter);}//model继承接口public class Mainmodel implements IMainModel { private Handler handler=new Handler(Looper.getMainLooper()); @Override public void getShop(final OnNetListenter<ShopBean> onNetListenter) { HttpUtils.getHttpUtils().doGet(Api.url, new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { onNetListenter.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String string=response.body().string(); final ShopBean shopBean=new Gson().fromJson(string,ShopBean.class); handler.post(new Runnable() { @Override public void run() { onNetListenter.onSueecss(shopBean); } }); } }); }}//presenter层public class MianPresenter { private final IMainModel iMainModel; private IMainActivity iMainActivity; public MianPresenter(IMainActivity iMainActivity) { this.iMainActivity=iMainActivity; iMainModel=new Mainmodel(); } public void getShop(){ iMainModel.getShop(new OnNetListenter<ShopBean>() { @Override public void onSueecss(ShopBean shopBean) { List<ShopBean.DataBean.ListBean> childList= new ArrayList<>(); for(int i=0;i<shopBean.getData().size();i++){ ShopBean.DataBean dataBean = shopBean.getData().get(i); List<ShopBean.DataBean.ListBean> list = dataBean.getList(); for(int j=0;j<list.size();j++){ childList.add(list.get(j)); } } iMainActivity.showList(childList); } @Override public void onFailure(Exception e) { e.printStackTrace(); } }); }}//view层接口public interface IMainActivity { public void showList(List<ShopBean.DataBean.ListBean> lsit); }//view层继承接口public class MainActivity extends AppCompatActivity implements IMainActivity { private CheckBox mCheckbox2; private TextView mPriceTv; private TextView mNumTv; private MianPresenter mianPresenter; private RecyclerView mElv; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); new MianPresenter(this).getShop(); //注册 EventBus.getDefault().register(this); mElv.setLayoutManager(new LinearLayoutManager(this)); mCheckbox2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { adapter.allSelect(mCheckbox2.isChecked()); } }); } private void initView() { mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2); mPriceTv = (TextView) findViewById(R.id.tv_price); mNumTv = (TextView) findViewById(R.id.tv_num); mElv = (RecyclerView) findViewById(R.id.elv); } @Override public void showList(List<ShopBean.DataBean.ListBean> lsit) { adapter=new MyAdapter(this,lsit); mElv.setAdapter(adapter); } @Subscribe public void onMessageEvent(MessageEvent event){ mCheckbox2.setChecked(event.isChecked()); } //接收价格 @Subscribe public void onMessageEvent(PriceAndCountEvent event) { mNumTv.setText("结算(" + event.getCount() + ")"); mPriceTv.setText(event.getPrice() + ""); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}//myadapter类public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<ShopBean.DataBean.ListBean> list; private Context context; public MyAdapter(Context context, List<ShopBean.DataBean.ListBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.activity_second, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { final ShopBean.DataBean.ListBean dataBean = list.get(position); final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.mChildCb.setChecked(dataBean.isChecked()); myViewHolder.mTelTv.setText(dataBean.getTitle()); myViewHolder.mTimeTv.setText(dataBean.getCreatetime()); myViewHolder.mPriTv.setText(dataBean.getPrice() + ""); myViewHolder.mNumTv.setText(dataBean.getNum() + ""); String[] split=dataBean.getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0] ,myViewHolder.mIv); myViewHolder.mChildCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置该条目对象里的checked属性值 dataBean.setChecked(myViewHolder.mChildCb.isChecked()); for (int i=0;i<list.size();i++){ PriceAndCountEvent compute = compute(); EventBus.getDefault().post(compute); if(myViewHolder.mChildCb.isChecked()){ if(isAllCbSelected()){ //改变"全选"状态 changeAllCbState(true); } }else{ changeAllCbState(false); } notifyDataSetChanged(); } } }); //删除 myViewHolder.mDelTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); //加 myViewHolder.mAddIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num=dataBean.getNum(); myViewHolder.mNumTv.setText(++num+""); dataBean.setNum(num); if(myViewHolder.mChildCb.isChecked()){ PriceAndCountEvent priceAndCountEvent=compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); //减 myViewHolder.mDelIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num=dataBean.getNum(); if(num == 1){ return; } myViewHolder.mNumTv.setText(--num+""); dataBean.setNum(num); if(myViewHolder.mChildCb.isChecked()){ PriceAndCountEvent priceAndCountEvent=compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder { private TextView mTelTv; private CheckBox mChildCb; private ImageView mIv; private TextView mContentTv; private TextView mTimeTv; private TextView mPriTv; private ImageView mDelIv; private TextView mNumTv; private ImageView mAddIv; private TextView mDelTv; public MyViewHolder(View itemView) { super(itemView); mTelTv = (TextView) itemView.findViewById(R.id.tv_tel); mChildCb = (CheckBox) itemView.findViewById(R.id.cb_child); mIv = (ImageView) itemView.findViewById(R.id.iv); mContentTv = (TextView) itemView.findViewById(R.id.tv_content); mTimeTv = (TextView) itemView.findViewById(R.id.tv_time); mPriTv = (TextView) itemView.findViewById(R.id.tv_pri); mDelIv = (ImageView) itemView.findViewById(R.id.iv_del); mNumTv = (TextView) itemView.findViewById(R.id.tv_num); mAddIv = (ImageView) itemView.findViewById(R.id.iv_add); mDelTv = (TextView) itemView.findViewById(R.id.tv_del); } } private void changeAllCbState(boolean flag) { MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); } private boolean isAllCbSelected() { for (int i = 0; i < list.size(); i++) { ShopBean.DataBean.ListBean listBean = list.get(i); if (!listBean.isChecked()) { return false; } } return true; } //计算价格private PriceAndCountEvent compute(){ int price=0; int count=0; for(int i=0;i<list.size();i++){ ShopBean.DataBean.ListBean listBean = list.get(i); if(listBean.isChecked()){ price+= listBean.getPrice()* listBean.getNum(); count+=listBean.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++) { ShopBean.DataBean.ListBean beans = list.get(i); beans.setChecked(flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); }}//eventbusevent 购物车连接主页面的功能;//MessageEventpublic class MessageEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; }}//PriceAndCountEventpublic class PriceAndCountEvent { private int price; private int count; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; }}//Bean要加两个方法//private boolean checked;//public boolean isChecked() { // return checked;//}//public void setChecked(boolean checked) { // this.checked = checked;//}
阅读全文
0 0
- 安卓1jShopDemo
- 安卓 ui -1
- 安卓开发1
- 安卓学习心得1
- 安卓学习(1)
- 安卓杂记1
- 安卓手记1
- 安卓-ListView(1)
- 安卓常用1
- 安卓笔记1
- 安卓复习1-安卓基础-单元测试
- 安卓学习:(1)安卓环境搭建
- 安卓开发基础知识1(双摄像头安卓手机)
- 安卓关机(1)
- 安卓基础学习1
- 安卓蓝牙学习1
- 安卓蓝牙API(1)
- 安卓编程问题1
- 运动控制器对比:Windows MR、Rift、Vive、PSVR(译文修正版)
- C# ConcurrentQueue实现
- 【Scikit-Learn 中文文档】高斯过程
- Leetcode665 (Array) Non-decreasing Array +Leetcode674 Longest Continuous Increasing Subsequence
- Job-ES6 语法
- 安卓1jShopDemo
- C++中的向上类型转换和向下类型转换+四种强制类型转换
- 错误笔记:JDBC中Statement和PreparedStatement对于Date类型写入数据库问题
- LM c语言代码
- MyBatis源码(五)之动态Sql解析运行阶段参数处理
- chrome谷歌浏览器开启webgl
- 53.Maximum Subarray
- 使用mongodb的findAndModify命令来进行数据同步
- 【Scikit-Learn 中文文档】交叉分解