布局:
<?xml version="1.0" encoding="utf-8"?> <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" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="vertical" tools:context="com.example.com.moni.view.Main4Activity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/goods_img" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="200dp" fresco:placeholderImage="@mipmap/ic_launcher" /> <LinearLayout android:orientation="vertical" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:id="@+id/goods_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按时打算打算大撒打算打算打算的" /> <TextView android:id="@+id/goods_prices" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="$199999" /> <TextView android:id="@+id/goods_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是商家" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp"> <Button android:id="@+id/add_btn" android:text="加入购物车" android:background="#f00" android:textColor="#fff" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" /> <Button android:text="立即购买" android:background="#efaf0d" android:layout_weight="1" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
接口回调:
public interface OnitemClickListent { void onItemClick(int position); }
适配器中添加
public void setListener(OnitemClickListent listener){ this.listener=listener; }
viewholder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(position); } });
主页面:
//传值
shousecondAbapter.setListener(new OnitemClickListent() { @Override public void onItemClick(int position) { int pid = list1.get(position).getPid(); Intent intent = new Intent(getActivity(), Main4Activity.class); intent.putExtra("pid",pid); startActivity(intent); } });//存值
shoufirstPresenter = new ShoufirstPresenter(this); shoufirstPresenter.getData();
跳转的页面
//接收传值
Intent intent = getIntent(); pid = intent.getIntExtra("pid", 0); SharedPreferences addShop = getSharedPreferences("addShop", MODE_PRIVATE); uid = addShop.getInt("uid", 0); XiangPresenter xpresenter = new XiangPresenter(this); xpresenter.getData("" + pid);
@Override public void OnSuccess(XiangBean xiangBean) { XiangBean.DataBean data = xiangBean.getData(); Log.i("--", "OnSuccess: " + xiangBean.getCode()); String[] split = data.getImages().split("\\|"); //给控件赋值 goodsImg.setImageURI(split[0]); goodsName.setText(data.getSubhead()); goodsPrices.setText("¥" + data.getPrice()); goodsTitle.setText(data.getTitle()); } @Override public void onSuccess(MessageBean messageBean) { Toast.makeText(Main4Activity.this,messageBean.getMsg(),Toast.LENGTH_SHORT).show(); } @OnClick(R.id.add_btn) public void onViewClicked() { addPresenter.getData(uid+"",pid+""); }
Bean类
public class XiangBean { /** * msg : * seller : {"description":"我是商家17","icon":"http://120.27.23.105/images/icon.png","name":"商家17","productNums":999,"score":5,"sellerid":17} * code : 0 * data : {"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"} */ private String msg; private SellerBean seller; private String code; private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public SellerBean getSeller() { return seller; } public void setSeller(SellerBean seller) { this.seller = seller; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class SellerBean { /** * description : 我是商家17 * icon : http://120.27.23.105/images/icon.png * name : 商家17 * productNums : 999 * score : 5.0 * sellerid : 17 */ private String description; private String icon; private String name; private int productNums; private double score; private int sellerid; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getProductNums() { return productNums; } public void setProductNums(int productNums) { this.productNums = productNums; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } } public static class DataBean { /** * bargainPrice : 111.99 * createtime : 2017-10-14T21:39:05 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * itemtype : 1 * pid : 1 * price : 118.0 * pscid : 1 * salenum : 0 * sellerid : 17 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }