andriodjie——mvp展示拦截器传来的数据跳转购物车
来源:互联网 发布:派克中性笔 知乎 编辑:程序博客网 时间:2024/05/23 13:44
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.INTERNET" />//adapter——CarAdapter
public class CarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<CarBean> list; public CarAdapter(Context context, List<CarBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.car_item,parent,false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if(holder instanceof ViewHolder){ ViewHolder viewHolder = (ViewHolder)holder; if(list.get(position).isB()){ viewHolder.item_ck.setChecked(true); }else{ viewHolder.item_ck.setChecked(false); } viewHolder.item_name.setText(list.get(position).getName()); viewHolder.item_price.setText("单价:"+list.get(position).getPrice()+" ¥"); viewHolder.item_des.setText("共计"+list.get(position).getCount()+"件,总价"+list.get(position).getCount()*100+"元"); viewHolder.item_ck.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); } } }); //监听Edtext viewHolder.item_custom.setListener(new CustomView.ChangeListener() { @Override public void onChange(long count) { list.get(position).setCount(count); notifyDataSetChanged(); if(listener != null){ listener.check(list.get(position).isB(),position); } } }); viewHolder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (list.get(position).isB()){ Toast.makeText(context, "不能删除", Toast.LENGTH_SHORT).show(); }else { list.remove(position); notifyDataSetChanged(); if(listener != null){ listener.check(true,position); } } } }); } } @Override public int getItemCount() { return list.size(); } //为check写一个接口 public CheckListener listener; public void setCheckListener(CheckListener listener){ this.listener = listener; } public interface CheckListener{ public void check(boolean check,int position); } class ViewHolder extends RecyclerView.ViewHolder{ private final CustomView item_custom; private final TextView item_name; private final TextView item_price; private final TextView item_des; private final CheckBox item_ck; private final Button del; public ViewHolder(View itemView) { super(itemView); item_custom = (CustomView) itemView.findViewById(R.id.item_custom); item_name = (TextView) itemView.findViewById(R.id.item_name); item_ck = (CheckBox) itemView.findViewById(R.id.item_ck); item_price = itemView.findViewById(R.id.item_price); item_des = itemView.findViewById(R.id.item_des); del = itemView.findViewById(R.id.delete); } }}//adapter——RVAdapter
public class RVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{ private Context context; private List<JavaBean.SongListBean> list; public RVAdapter(Context context, List<JavaBean.SongListBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.rv_item,null); MyHolder myHolder = new MyHolder(view); view.setOnClickListener(this); return myHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyHolder){ MyHolder myHolder = (MyHolder) holder; ImageLoader.getInstance().displayImage(list.get(position).getPic_small(),myHolder.img, MyApplication.getOptons()); myHolder.name.setText(list.get(position).getTitle()); myHolder.author.setText(list.get(position).getAuthor()+" "+list.get(position).getAlbum_title()); myHolder.author.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(); } }); } } @Override public int getItemCount() { return list==null?0:list.size(); } @Override public void onClick(View view) { if (listener!=null){ listener.onItemClick(); } } class MyHolder extends RecyclerView.ViewHolder{ private ImageView img; private TextView name; private TextView author; public MyHolder(View itemView) { super(itemView); //找控件 img = itemView.findViewById(R.id.img); name = itemView.findViewById(R.id.name); author = itemView.findViewById(R.id.author); } } public interface OnClickListener{ void onItemClick(); } public OnClickListener listener; public void setOnItemClickListener(OnClickListener listener){ this.listener = listener; }}//bean——CarBean
public class CarBean { private int pic; private String name; private int price; private boolean b=true; private long count; public CarBean(int pic, String name, int price, boolean isSelect, long count) { this.pic = pic; this.name = name; this.price = price; this.b = isSelect; this.count = count; } public int getPic() { return pic; } public void setPic(int pic) { this.pic = pic; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public boolean isB() { return b; } public void setB(boolean b) { this.b = b; } public long getCount() { return count; } public void setCount(long count) { this.count = count; }}//model——MyModel
public class MyModel { //网络请求 public void login(String path,final MyModelListener listener) { HttpUtls.doGet(path, new Callback() { @Override public void onFailure(okhttp3.Call call, IOException e) { System.out.println("call = " + call); } @Override public void onResponse(okhttp3.Call call, Response response) throws IOException { listener.onSuccess(response.body().string()); } }); }}//model——MyModelListener
public interface MyModelListener { void onSuccess(String s); void onFailed();}//Presenter——MyPresenterpublic class MyPresenter { private MyViewListener listener ; private MyModel model; //构造器 public MyPresenter(MyViewListener loginActivityViewListener){ this.listener = loginActivityViewListener; this.model = new MyModel(); } public void login(final String path){ // 空判断 合法性 model.login(path, new MyModelListener() { @Override public void onSuccess(String s) { listener.onSuccess(s); } @Override public void onFailed() { listener.onFailed(); } }); }}//view——MyViewListenerpublic interface MyViewListener { void onSuccess(String s); void onFailed();}//Activity——CarActivitypublic class CarActivity extends AppCompatActivity { private CheckBox checkBox; private TextView all_price; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_car); checkBox = (CheckBox) findViewById(R.id.ck_all); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_car); all_price = (TextView) findViewById(R.id.all_price); LinearLayoutManager manager = new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); //添加死数据 final List<CarBean> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { list.add(new CarBean(R.mipmap.ic_launcher_round,"商品" + i, 100, true, 1)); } final CarAdapter adapter = new CarAdapter(this, list); recyclerView.setAdapter(adapter); adapter.setCheckListener(new CarAdapter.CheckListener() { @Override public void check(boolean check, int position) { //监听全选 boolean all_check = true; //价格 float price = 0; int count = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).isB()) { price += list.get(i).getPrice() * list.get(i).getCount(); count += list.get(i).getCount(); } } all_price.setText("共计"+count+"件,总共"+price + "元"); for (int i = 0; i < list.size(); i++) { if (!list.get(i).isB()) { all_check = false; break; } } if (all_check) { checkBox.setChecked(true); } else { checkBox.setChecked(false); } } }); //全选和反选 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean check = checkBox.isChecked(); float price = 0; int count = 0; for (int i = 0; i < list.size(); i++) { list.get(i).setB(check); if (list.get(i).isB()) { price += list.get(i).getPrice() * list.get(i).getCount(); count +=list.get(i).getCount(); } } adapter.notifyDataSetChanged(); all_price.setText("共计"+count+"件,总共"+price + "元"); } }); }}//Activity——CarActivitypublic class CustomView extends LinearLayout{ 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.custom_layout,null); addView(view); Button jian = (Button) view.findViewById(R.id.jian); Button jia = (Button) view.findViewById(R.id.jia); final EditText num = (EditText) view.findViewById(R.id.num); //减号 jian.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String result = num.getText().toString().trim(); int integerResult = Integer.valueOf(result); //给做个限制 if(integerResult > 1){ integerResult = integerResult - 1; num.setText(integerResult + ""); }else{ Toast.makeText(context, "最小数量为1", Toast.LENGTH_SHORT).show(); } if(listener != null){ listener.onChange(integerResult); } } }); //加号 jia.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String result = num.getText().toString().trim(); int integerResult = Integer.valueOf(result); if(integerResult < 10000){ integerResult = integerResult + 1; num.setText(integerResult +""); }else{ Toast.makeText(context, "已经超出最大值", Toast.LENGTH_SHORT).show(); } if(listener != null ){ listener.onChange(integerResult); } } }); num.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.i("beforeTextChanged",s+"-----"+start+"-------"+count+"-----"+after);// if(start < 0){// Toast.makeText(context, "000000000000000", Toast.LENGTH_SHORT).show();// s = "1";// }else if(start > 4){// s = "10000";// Toast.makeText(context, "111111111", Toast.LENGTH_SHORT).show();// } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.i("onTextChanged",s+"-----"+start+"-------"+before+"-----"+count); } @Override public void afterTextChanged(Editable s) { if(listener != null ){ listener.onChange(Long.valueOf(s.toString())); } } }); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ChangeListener listener; public void setListener(ChangeListener listener){ this.listener = listener; } public interface ChangeListener{ public void onChange(long count); }}//Activity——MainActivitypublic class MainActivity extends AppCompatActivity implements MyViewListener { private RecyclerView recyclerView; private MyPresenter presenter; private List<JavaBean.SongListBean> listBeen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 recyclerView = (RecyclerView) findViewById(R.id.recyclerview); //实例化presenter presenter = new MyPresenter(this); presenter.login("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=20&offset=0"); } @Override public void onSuccess(final String s) { if (s!=null){ runOnUiThread(new Runnable() { @Override public void run() { System.out.println("s = " + s.toString()); //解析gson串 Gson gson = new Gson(); JavaBean bean = gson.fromJson(s.toString(),JavaBean.class); listBeen = bean.getSong_list(); //设置模式 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this); recyclerView.setLayoutManager(linearLayoutManager); //设置适配器 final RVAdapter adapter = new RVAdapter(MainActivity.this,listBeen); recyclerView.setAdapter(adapter); adapter.setOnItemClickListener(new RVAdapter.OnClickListener() { @Override public void onItemClick() { Intent intent = new Intent(MainActivity.this,CarActivity.class); startActivity(intent); } }); } }); } } @Override public void onFailed() { }}//Activity——MyApplication//android:name=".MyApplication"public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); String path = Environment.getExternalStorageDirectory()+"1507DAccess"; File cacheDir = new File(path); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .threadPriority(100) .threadPoolSize(3) .memoryCacheExtraOptions(200,200) .memoryCacheSize(2 * 1024 * 1024) .diskCache(new UnlimitedDiskCache(cacheDir)) .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .diskCacheSize(50 * 1024 * 1024) .build(); ImageLoader.getInstance().init(config); } public static DisplayImageOptions getOptons(){ DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) //空图片 .showImageForEmptyUri(R.mipmap.ic_launcher_round) //加载失败时图片 .showImageOnFail(R.mipmap.ic_launcher) //加载过程中图片 .showImageOnLoading(R.mipmap.ic_launcher_round) .build(); return options; }}//XML.布局activity_car.xml
<?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" > <TextView android:text="购物车" android:textSize="28sp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_car" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="30dp"> <CheckBox android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="全选" android:checked="true" android:layout_gravity="center" android:id="@+id/ck_all"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="总计5件,总价500元" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="center_vertical" android:id="@+id/all_price" /> </LinearLayout></LinearLayout>
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.theyuekao.MainActivity"> <ImageView android:id="@+id/aa" android:background="#f00" android:layout_width="match_parent" android:layout_height="40dp" android:src="@mipmap/a"/> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_below="@id/aa" android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
car_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="130dp"> <CheckBox android:id="@+id/item_ck" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/car_img" android:layout_toRightOf="@id/item_ck" android:src="@mipmap/ic_launcher" android:layout_margin="5dp" android:layout_width="60dp" android:layout_centerVertical="true" android:layout_height="60dp" /> <TextView android:id="@+id/item_name" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:text="111" android:layout_marginTop="30dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/item_price" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:text="111" android:layout_below="@id/item_name" android:layout_height="wrap_content" /> <com.example.theyuekao.CustomView android:id="@+id/item_custom" android:layout_below="@id/item_price" android:layout_toRightOf="@id/car_img" android:layout_width="70dp" android:layout_height="70dp"/> <Button android:id="@+id/delete" android:layout_alignParentRight="true" android:layout_margin="5dp" android:text="删除" android:layout_centerVertical="true" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/item_des" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
custom_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="30dp" android:layout_height="30dp" android:text="-" android:background="#fff" android:id="@+id/jian" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:text="1" android:id="@+id/num"/> <Button android:layout_width="30dp" android:layout_height="30dp" android:background="#fff" android:text="+" android:id="@+id/jia"/></LinearLayout>
rv_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/img" android:layout_margin="5dp" android:src="@mipmap/ic_launcher_round" android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:id="@+id/name" android:layout_toRightOf="@id/img" android:layout_marginTop="5dp" android:text="111" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/author" android:layout_toRightOf="@id/img" android:layout_below="@id/name" android:text="111" android:textSize="14sp" android:textColor="#ccc" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
阅读全文
0 0
- andriodjie——mvp展示拦截器传来的数据跳转购物车
- Android——MVP模式拦截器加载网络数据,点击跳转购物车
- andriodjie——MVP加RecycleView简单展示
- Andriodjie——简单的购物车全选反选
- Andriodjie——OKhttp拦截器
- RecyclerView条目跳转+SpringView数据刷新加载+MVP+OKhttp+拦截器+自定义view 实现请求网络数据的二级列表购物车
- Andriodjie——二级列表实现购物车
- Andriodjie——二级列表实现购物车
- MVP+OKhttp+拦截器+RecyclerView+自定义view 实现请求网络数据的二级列表购物车
- Andriodjie——登陆注册展示商品
- Andriodjie——画圆3秒跳转
- 二级列表展示购物车MVP
- mvp购物车的实现 网络请求图片实现跳转
- JSON—接收服务器端传来的数据
- andriodjie——recycleview
- Android中利用MVP进行一级购物车的展示(布局篇)
- andriodjie——xRecyclerView网络获取数据简单实现
- 组合属性动画:缩放、渐变 平移和旋转+ImageLoader加载图片+MVP+OKhttp+拦截器+请求网络数据二级列表购物车
- Kafka基本使用介绍
- MyApplication
- angularjs过滤器
- 表格的添加删除加省级联动效果
- 迷茫的时候要做些什么,未来才不会后悔
- andriodjie——mvp展示拦截器传来的数据跳转购物车
- javascript对上传文件的相关操作
- [简单逻辑学]逻辑学的基本原理——从全称到特称
- H5新增特性
- unity3D基础之渲染管线的设计方法学习路线 讲义源码
- messenger进程之间的通讯
- 父级元素的 font-size:0 解决子代的inline或inlien-block的换行或图片空隙问题
- TCP/IP协议族【第8章地址解析协议APR】
- JavaScript 创建、插入和删除元素